A doubt about precision in Floats

This is a discussion on A doubt about precision in Floats within the C Programming forums, part of the General Programming Boards category; Hi, I ve a nagging question that I have been unable to figure out. From what I understand, floats have ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    A doubt about precision in Floats

    Hi,

    I ve a nagging question that I have been unable to figure out. From what I understand,
    floats have a precision of 6 digits. And this is supposed to be 6 digits in total, not just 6 digits after the decimal point.

    So the values that could be stored with precision in a floating point number are as follows,
    0.123456
    1.23456
    12.3456
    123.456
    1234.56
    12345.6 etc.. (i.e.) as the whole number part gets more and more digits, the number of digits that you can store precisely after the decimal point correspondingly decreases. Am I correct in my undestanding till here??

    So my issue is, I have an array of floats with values ranging from One's to the thousands. However, when I print out the floats I see that all of the floats are printed out with 6 digits after the decimal point.

    For instance a number such as 1000.82 would only be stored with 2 digits of precision right?. But while printing out I get something like 1000.824092. Is this some quirk of the printf statement that it automatically fills in junk numbers to fill up to 6 decimal points? Or is it that any number declared as a float must always have 6 digits after the decimal point?

    I also tried rounding off all of the floats to 4 decimal digits. But that seems to have no effect. All floats are always printed out with 6 decimal digits.

    If it is only at the printf that this filling out to 6 digits is taking place, I can go ahead and use the floats for further calculations, but if the numbers are stored in the memory itself like this, using them for calculations would mess up a lot of things. Thats why trying to get this cleared out.

    thanks,

    Avinash

    P.S..
    I know using 'double' instead would save me a lot of trouble. But I am actually supposed to take this float array and transfer onto Graphics cards for parallel processing. And double precision functions are either not available or incredibly slow on most Graphics cards. Thats why I am trying to see if I can work it out with floats itself..

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A floating point number can have very many decimal digits. More correctly, its base10 decimal representation can have more than 6 decimal digits. The floating point is a representation different from the normal representation.

    The printf just prints the 6 first decimal digits. If a number is 0.099931231 for example it prints 0.099931. The rest the printf assumes are not significant.

    Note that you cannot represent 0.01 precisely with a float. It will be something like 0.009993... But that is close enought. It can be a problem if you are not careful and make the float a decimal (with an int), then keep only the first two digits. So you would get 0 instead of .01

    If you want 99.999% precise calculations then you should use another format. Something like
    Code:
    typedef struct temp
    {
        int num;
        unsigned int dec;
    } decimal;
    But a divition is never precise anyway. If you do 10/3 you cannot get a precise number even in decimal. So you always get an approximatation. The above code would just have a far greater one than a "float" or a "double". But it will be slower on its calculation as well.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Hi guys,

    Thank you, both of you for your answers.
    @iMalc: I actually did find this paper. But I guess I was just too intimidated by the size of it and hoped I would get a simpler answer here... Sorry bout that . I ll try to plough my way through it again though.

    While I do that, I noticed something that I wanted to run past everyone. I was wrong when I said that rounding off the floats didnt work and everything still printed out 6 digits. Turns out some of the numbers do get rounded off.. But some of them don't.

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main()
    {
      float num1;
    
      num1 = 16.822599; /* or 17.320499, 45.519199, 75.179802 */
    
                                      /*32.324577 on the other hand gets rounded off as expected */
      printf("Num1=%f\n",num1);
     
      num1 = (float)( (int)(num1*10000 + 0.5) )/ 10000;
    
      printf("Num1=%f\n",num1);
    
      return (0);
    }
    I ve posted the snippet of test code that I used.. U ll see that with any of the numbers that I ve used, (alternative numbers in comments) the number doesn't get rounded off to 4 digits. But most numbers with 6 decimal places do get rounded off though..

    I guess that once I ve read through the literature, I ll see that all of these numbers have something common in their binary representation that doesn't allow them to be rounded off any more than this. Am i right?

    So is there any standard method for dealing with such numbers..? Or pointers to reading material that I could go through would be great too..

    thanks,

    Avinash

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  2. IEEE Representation of Floats
    By Pnevma in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 09:06 AM
  3. Precision with floats
    By ramparts in forum C Programming
    Replies: 4
    Last Post: 11-04-2006, 02:31 PM
  4. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM
  5. % and floats
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2003, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21