![]() |
|
#1
|
|||
|
|||
Decimal places. Weird...Hey guys. I wrote a program that sum up 10 float values. It suppose to be 20.3 but it came out as 20.29999999 how come? how can I round it up to 2 decimal places? Thx
|
|
#2
|
|||
|
|||
Re: Decimal places. Weird...Quote:
It turns out that the decimal number 20.3 can not be stored exactly as a binary floating point number. No matter how carefully you perform your calculations, the binary floating point result will not be exactly 20.3. In "normal" C (and C++) implementations, a float variable is good for about 6 siginificant decimal digits (note that this is not the same as six decimal places). A double is good for about 15 significant decimal digits. You can use the printf %f format specifier (used for floats and doubles) to round off the display to any given number of decimal places. Note that this affects the display. Nothing on this green earth can make a "normal" binary floating point number (that is: one that stores the significand in a finite number of binary digits) store the decimal number 20.3 exactly. Can't be done. Period. Full stop. A mathematically perfect compiler and its libraries (and program) will create code that stores whatever floating point binary value is closest to the desired value. Example: 20.3000 shows six significant decimal digits, but has four decimal places. You can tell printf to print more decimal places, and it will do the best that it can, but it can't display more correct significant digits than what was stored. (Note that "%f" by default prints six decimal places.) Here's an example that doesn't do any calculations. It just declares a constant and stores it as a floating point number. (I show both float and double variables, and print with different numbers of decimal places.) CPP / C++ / C Code:
Output from my Linux system with the GNU gcc compiler (same with Borland and GNU compilers on my Windows XP platform). Code:
Note that the first two show 18 significant digits. (See footnote). The next two show eight significant digits. The last two show six significant digits. Note also that different compilers may display slight differences in the non-correct significant digit positions, but all should show the same output up to the number of correct significant digits for each representation. Regards, Dave "There is a difference between 18 significant digits and 18 correct significant digits." ---davekw7x |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary to Decimal Conversion | Saberwing | C Programming Language | 5 | 28-Dec-2006 15:57 |
| different results when calling function | mirizar | C Programming Language | 3 | 15-Dec-2005 15:14 |
| Roman to decimal to roman | SpudNuts | C++ Forum | 2 | 16-Feb-2005 20:44 |
| Hex Result giving strange answers. | Rosdahale | C Programming Language | 6 | 07-Dec-2004 21:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The