0

The earlier posts refer to the problem of storing numbers that are non-terminating or with a precision greater than can be stored in a double. The numbers in my example are terminating decimals that terminate at the 2nd significant digit.

I have the following C code which I have tested in IDEONE.com and in NetBeans and both give me the same output. I have also tested it on three different computers: a Dell laptop, an HP laptop, and a Dell desktop. All three were Windows 10 so I can't say the operating system isn't a factor.

When I multiply the double 2.03 by 100 and store it in an int, the value is stored as 202 instead of 203.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
   double s, t, u;
   int a, b, c;
   s = .99;
   t = 2.03;
   u = 5.00;
   a = 100 * s;
   b = 100 * t;
   c = 100 * u;
   printf("\n%.12f\t%.12f\t%.12f", s, t, u);
   printf("\n%d\t%d\t%d", a, b, c);
   return (EXIT_SUCCESS);
}

My output is:

0.990000000000  2.030000000000  5.000000000000
99      202    500

I'm fairly sure it has to do with the storing of the doubles and if I display to 16 decimal places, I do see a difference but I'm not confident it is reasonable to go out so many decimal places.

Does it make sense that working with such common numbers I would see this difference when I force a double into an int and if so, what is the recommended coding to use instead?

  • *"The earlier posts refer to the problem of storing numbers that are non-terminating or with a precision greater than can be stored in a double."* -- Not so. They deal with the inexactness of floating-point numbers, which is the reason you're getting rounding to what you perceive to be the wrong direction when assigning the result to an `int`. – Dolda2000 Mar 25 '17 at 03:37
  • The problem comes from treating a `double` as though it were exact, so that all numbers even the slightest bit less than 203 should be treated differently from 203 and numbers even slightly larger. The closest IEEE 754 64-bit binary to 2.03 is 2.029999999999999804600747665972448885440826416015625. Multiplying it by 100 in floating point round to nearest arithmetic gives 202.999999999999971578290569595992565155029296875. Converting to `int` by truncation, not rounding, treats that as being significantly less than 203, which it isn't. – Patricia Shanahan Mar 25 '17 at 18:41
  • I have voted to reopen in order to turn my comment answering the specific question into an answer. – Patricia Shanahan Mar 25 '17 at 18:43

0 Answers0