Home > Net >  How long for DBL_MAX - DBL_MIN to equal DBL_MIN
How long for DBL_MAX - DBL_MIN to equal DBL_MIN

Time:04-22

I left out reading in the libraries and some other stuff, but I was wondering how long the code below would take to run, I stopped it after like 15 mins because my computer thought it was a virus.

int main(){

double min = __DBL_MIN__;
double current = __DBL_MAX__;

while ( current > min ){

    current = current - min;
}

CodePudding user response:

Forever.
current - min rounds to current.

Floating point numbers store value with a floating significand. Do not expect values with an extreme difference in exponential value to perform a subtraction exactly.

CodePudding user response:

To expand a bit on @chux's (correct) answer:

A typical double will have a range of positive numbers from approximately 10-308 to 10 308. But it will only have around 16 significant digits.

That means for a number near 10 308, the smallest number you can subtract that will change the value is approximately 10(308-16) = 10292.

That's only a rough approximation, but somewhere in that vicinity is a lower limit, and any number smaller than that limit can be subtracted from 10308 as often as you like, and the result will remain unchanged.

  • Related