Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double comparison - numeric limits

Tags:

c++

I have some trouble understanding the following code:

double a = -1000;
double b = numeric_limits<double>::min();

if (a < b)
{
   cout << "why?";
}

And the output is:

why?

How can -1000 be lower than numeric_limits<double>::min()?

like image 746
Nick Avatar asked Nov 20 '15 20:11

Nick


People also ask

What is the limit of double in C++?

The C++ double can hold floating-point values of up to 15 digits taking up a space of 8 bytes in the memory. The range of the values that can be stored in a double type variable is 1.7E - 308 to 1.7E + 308.

What is a numeric limit?

Numeric limits type. Provides information about the properties of arithmetic types (either integral or floating-point) in the specific platform for which the library compiles. This class template is specialized for every fundamental arithmetic type, with its members describing the properties of type T .

Which data type is accepted by numeric limit function?

Data types that supports std::numeric_limits() in C++ std::numeric_limits<int>::max() gives the maximum possible value we can store in type int. std::numeric_limits<unsigned int>::max()) gives the maximum possible value we can store in type unsigned int.

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T. Header File: #include<limits>


1 Answers

It is because numeric_limits<double>::min(); is the lowest positive number expressible in double floating-point precision, not the most negative number.

like image 153
Eugene Avatar answered Sep 30 '22 19:09

Eugene