Is there a mistake in numeric_limits or am I just confused?

I came across some weird behavior, at least in my own mind, debugging some code related to determining if the addition operation would be double. Here is an example of a program demonstrating what I found.

#include <iostream> #include <limits> using std::cout; using std::endl; using std::numeric_limits; int main() { double lowest = numeric_limits<double>::lowest(); bool truth = (lowest + 10000) == lowest; cout << truth << endl; } 

When I execute this code, I believe in the result. Is this a mistake or am I just sleep deprived?

+5
source share
1 answer

The smallest double:

 -1.7976931348623157e+308 

Adding 10,000, or 1e4, to this could only have a noticeable effect if two-room numbers had more than 300 digits of accuracy, which they definitely do not have. Parties can contain only 15-17 significant digits.

The difference in magnitude between the two numbers is so great that adding 10,000 does not produce a new number. In fact, the minimum double is such a huge amount (so to speak) that you can add googol to it, followed by 100 zeros, and that will not change.

+15
source

Source: https://habr.com/ru/post/1202306/


All Articles