0

If I input value: 3.123456789012345e-09

I should get back 3.12345678901235e-09

But it will only round up on .6 to .9

ostringstream os;

double d = 3.123456789012345e-09 ;

os << setprecision( 15 );

os << d;

istringstream is ( os.str() );

is >> setprecision( 15) >> d;
std::cout << d << std::endl;

The real bizzare thing is: The rounding behaviour changes depending on the power:

3.123456789012345e-09 = 3.12345678901234e-09

3.123456789012345e-10 = 3.12345678901234e-10

3.123456789012345e-11 = 3.12345678901235e-11

3.123456789012345e-14 = 3.12345678901235e-14

Every 1000th power rounds correctly... I am so confused. I know that rounding is a bit a of a weird subject but I cant make heads or tails of this. on a 64 bit machine btw.

theBigCheese88
  • 442
  • 4
  • 16
  • Simply said: computers cannot work with all real numbers. They can work only with some limited subset of them. If your input does not exactly match a number from this subset, rounding will occur. – Daniel Langr Oct 02 '19 at 09:08
  • @DanielLangr I understand the floating point issue where the bits get cut off leaving some number after the decimal places but my issue is that that rounding end up being different. The number is definitely there but .5 rounds up or down depending on the power – theBigCheese88 Oct 02 '19 at 09:13
  • 3
    Your input is actually 0.00000000312345678901234495971910408852571794380281744452076964080333709716796875. You can use converters like [this one](https://www.exploringbinary.com/floating-point-converter/) and see what your actual values are. – molbdnilo Oct 02 '19 at 09:14
  • Please do read 'Is floating point math broken'. But more detailed summary. Floating point numbers have finite width and are under the hood binary. You may think that changing the exponent is just 'shifting' but x10 and /10 don't necessarily work like that in binary. – Persixty Oct 02 '19 at 09:15
  • @theBigCheese88 FP numbers are represented in a binary base, not decimal one. Try to write your number in a binary base to see what happens – Daniel Langr Oct 02 '19 at 09:17
  • @molbdnilo's link is great. Try it out with such a simple input as 0.1! You'll notice it cannot be represented exactly either, that's because of 0.1 being periodic in base 2, just as 1/3 is in decimal representation. – Aconcagua Oct 02 '19 at 09:33
  • Interestingly, in the past there existed number systems based on 12 as well as on 60 (Babylonians used such one). In both, 1/3 wouldn't have been periodic... – Aconcagua Oct 02 '19 at 09:38
  • Ah I understand, @molbdnilo thanks for the converter link, that made things easier to understand, Thank you all! – theBigCheese88 Oct 02 '19 at 11:16

0 Answers0