GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Feb-2007, 13:08
feicobain feicobain is offline
New Member
 
Join Date: Feb 2007
Posts: 10
feicobain is an unknown quantity at this point

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  
Old 07-Feb-2007, 14:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Decimal places. Weird...


Quote:
Originally Posted by feicobain
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

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:
#include <stdio.h>

int main()
{
    double dx = 20.3;
    float  fx = 20.3;

    printf("with %%.16f: dx = %.16f\n", dx);
    printf("with %%.16f: fx = %.16f\n", fx);

    printf("with %%f:    dx = %f\n", dx);
    printf("with %%f:    fx = %f\n", fx);

    printf("with %%.4f:  dx = %.4f\n", dx);
    printf("with %%.4f:  fx = %.4f\n", fx);

    return 0;
}

Output from my Linux system with the GNU gcc compiler (same with Borland and GNU compilers on my Windows XP platform).

Code:
with %.16f: dx = 20.3000000000000007 with %.16f: fx = 20.2999992370605469 with %f: dx = 20.300000 with %f: fx = 20.299999 with %.4f: dx = 20.3000 with %.4f: fx = 20.3000

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 GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.