0

I'm trying to retrieve any digits that come after a decimal point in any given number (below an example number is represented by "in").

double in = 1.2;
double out = in - (int)in;

Here I expect/want "out" to equate to 0.2 but the number comes out as 0.19999999999999996. Why is this and how can I get the result I want?

Jason Monsalve
  • 37
  • 1
  • 1
  • 5
  • 1
    Read up on floating point representation of numbers - 0.2 is irrational in binary, you'll need to use something that works precisely with decimals to get the result you want. – Evan Knowles Feb 23 '15 at 06:10
  • read this http://floating-point-gui.de/ then read this http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – phuclv Feb 23 '15 at 06:16
  • Hey, I get to plug my own web site! Go to http://www.adambeneschan.com/How-Does-Floating-Point-Work/ and enter 1.2 in the box and click on Double. It should help explain what's going on. – ajb Feb 23 '15 at 06:21
  • Your algorithm is not working quite right. The closest double to 1.2 is 1999999999999999555910790149937383830547332763671875 – Patricia Shanahan Feb 23 '15 at 09:19
  • 2
    @EvanKnowles Technically, 0.2 is rational, rather than irrational, regardless of the number radix. It has a non-terminating binary expansion. – Patricia Shanahan Feb 23 '15 at 09:20
  • 1
    @Patricia Shanahan: Somehow you dropped the leading '1.' from 1.1999999999999999555910790149937383830547332763671875 (and that is what Adam's tool gives me, which is correct). – Rick Regan Feb 23 '15 at 15:49
  • @RickRegan You are right - a copy-paste error. – Patricia Shanahan Feb 23 '15 at 16:07

0 Answers0