strange double to int conversion behavior in c++ -


The following program displays the full conversion behavior strange double am what I see in c ++:

< Pre> # include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; Int main () {double D = 33222.221; Printf ("d =% 9.9 g \ n", d); D * = 1000; Int i = (int) d; Printf ("d =% 9.9g | I =% d \ n", d, i); Return 0; }

When I compile the program and runs me, then I see:

  g ++ test.cpp ./a.out d = 33222.221 D = 33222221 | I = 33222220  

Why am I not equal to 33222221? Compiler version GCC 4.3.0

Floating Point presentation is not nearly as accurate (only in special cases). Every programmer should read it:

In a nutshell - your number may be 33222220 after 33222220k999999999999999999999999999999999999999999999999999999999999999999999999999999998 (or something like that), which is insignificant.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -