简体   繁体   中英

c++ the value of a double

Given the following declaration:

double x;

What is the value of x ?

Is my answer correct?

The value of x is: plus or minus 10 to 308th (limited to ~12 significant digits)

No, your answer is not correct. What thought process led you to that answer? Where might you have gone wrong?


Given the proliferation of "answers" on this question, I'm just going to come out and state it. The answer is that the value of x is undefined . It's an uninitialized value. If you try to read the value, in practice you'll get garbage (ie you'll get whatever bit pattern was in that memory location, reinterpreted as a double ). But it's not as simple as that. Since the value is undefined, the optimization pass of the compiler is actually free to make choices based on the undefined value that it could not have made if the variable had any defined value. Any attempts to use an uninitialized variable can produce unexpected results, and is certainly a programming error.


There is one caveat, as I alluded to in my comment. If this declaration happens at the top level (or is modified with the static keyword) then the value becomes simply 0.0 .

The answer depends on where the variable is defined.

#include <iostream>

double x;
int main(){
    double y;

    std::cout << x << " " << y << std::endl;
    return 0;
}

Different rules apply to global and local variables.

x will be initialized to zero. y , as everyone else has stated, could be anything.

When you say T x; you default-initialize the variable x , and for a fundamental type T such as double this means that no initialization happens at all, the variable has indeterminate value (cf. 8.5), and reading the variable x before writing to it is simply undefined behaviour (cf. the note in 17.6.3.3/2).

So it's much worse than just getting an unknown value - rather, your entire program becomes non-deterministic at the point of invoking undefined behaviour.

The value of x could be anything. It's undefined behavior to access the value, and so you could even get a value double can't contain. If x has static storage then it is defined, and will be initialized to 0.0 .

In practice it will be a pseudo random assortment of bits. Not really random as it's left over from what was previously in the same location in memory. So yes, it will be within your range, along with infinities and NaN as those are also doubles.

You are correct.

The value is uninitialized and may contain anything resident in memory. Double precision is 53 effective bits, so your range is approximately right. I didn't check your precision but it's not exact since the level of accuracy is compressed around zero.

It's worth mentioning that there's also positive and negative infinity, plus NaN , and finally negative zero (yes, negative zero!). There are bitstrings which aren't even IEEE floating-point numbers which x could contain too :-).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM