-3

I am new to C++ and have the following simple code snippet exploring C++ limitations:

int main() {
  
  float x = 2147483000;
  int y = static_cast<int>(x);
  cout << "x: " << x << ", y: " << y;
}

Why is the output showing me different values for x & y even though the float value is within int limit which is 2147483647

Code Results

x: 2.14748e+09, y: 2147483008

Why is it showing different values for x & y?

juco
  • 57
  • 6
  • 2
    They're not different values, they're different representations of (mostly) the same value. – sweenish Dec 14 '22 at 03:54
  • float only gives you about 7 decimal digits of precision. You've got about 10 decimal digits between your most significant digit and your least significant digit. float just isn't that precise. – Wyck Dec 14 '22 at 03:55
  • If it were dealing with decimal digits, that would make sense. Although, I suppose it plays a role in why it opted for scientific. A little iomanip and you can get the same value back. – sweenish Dec 14 '22 at 03:57
  • 1
    Useful reference [Which is the first integer that an IEEE 754 float is incapable of representing exactly?](https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-e) – Wyck Dec 14 '22 at 03:58
  • Good link, got me to double check my test code. My printouts do indeed have a different last digit. Another reason for me to generally hate the `float` type and the fact that new learners still insist on using it. So, disregard my comments. I'll leave them up because the discussion is worth other people seeing. – sweenish Dec 14 '22 at 04:01

2 Answers2

1

I have read your question carefully. There is a misconception, not mistake.

That is happening because the float have a certain capacity to store its precision up to 7 decimal places if the digits exceeds than the 7th digit, It would loss its precision after the 7th digit for beyond to it. Due to this reason the output is not accurate or same.

Asjid Ali
  • 57
  • 4
  • The format commonly used for `float` does not “store its precision up to 7 decimal places” because it does not store decimal at all. Further, there are decimal numerals with seven significant digits that will not survive a “round trip” conversion from decimal to `float` and back to a seven-digit decimal. Six is the most that the format can guarantee. – Eric Postpischil Dec 14 '22 at 21:45
1

Why is it showing different values for x & y?

The default conversion for displaying float that are in the range for which an exponent is used for display is to show six significant digits.

In the format commonly used for float, IEEE-754 binary32, the two representable values nearest 2,147,483,000 are 2,147,482,880 and 2,147,483,008. So, for float x = 2147483000;, a good C++ implementation will convert 2,147,483,000 to the closest float value, 2,147,483,008.

Then int y = static_cast<int>(x); sets y to this same value, 2,147,483,008.

When the float x is inserted into the cout stream with default formatting, six significant digits are used, producing 2.14748•109.

When the int y is inserted into the stream, its full value is shown, 2,147,483,008.

You can see the full value by requesting more precision. std::cout << std::setprecision(99) << "x: " << x << ", y: " << y << '\n'; produces “x: 2147483008, y: 2147483008”.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312