Thread: float inaccuracy?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    float inaccuracy?

    hello,

    i have a program that calculates data using floats and i noticed that if i run it more then once with the same values, i get different answers...

    Code:
    float size = 0.015f, max = 0.3f / size;
    
    char sTemp[256];
    sprintf(sTemp, "%f\n", max);
    // display sTemp in a messagebox or something
    when i ran the program 5 times, i got the following answers:

    1) 19.999979
    2) 20.000021
    3) 19.999941
    4) 19.999941
    5) 20.000021

    these small inaccuracies lead to alot of problems in the program.
    Last edited by X PaYnE X; 10-31-2005 at 08:52 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep. Floats, and doubles, are not accurate datatypes.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    If you want more accuracy, maybe use a different data type instead of float?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    could you recommend what i should use, i already tried both floats and doubles..

    edit:

    i dont see why this is affected by how inaccurate floats and doubles are, the answer should be 20, not 19.99999 or 20.00001.

    and floats are used throughout the dx sdk, why would they use inaccurate data types in 3d calculations? surely a 0.00001 inaccuracy would cause a noticable effect.
    Last edited by X PaYnE X; 10-31-2005 at 07:58 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consult the FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    could be your compiler -- VC++ 6.0 always produces the same results every time I run the program.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	float fSize = 0.015f;
    float size = 0.015f, max = 0.3f / fSize;
    
    char sTemp[256];
    sprintf(sTemp, "%f\n", max);
    printf("%s\n", sTemp);
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    im using vc++ 6.0 as well.. i tried that code in a new console project and it gave me the same answer 20.00002 5 times, but when i tried changing the values:

    0.015f and 0.3f gave me 20.00002
    0.010f and 0.2f gave me 20.00000

    i removed the 'f' and tried 0.015 and 0.3 and that gave me 20.00000

    i tried converting all the datatypes i used in the formula to doubles and removed all the 'f's and i still got inaccurate results..

    the thing is, ive been using floats all over my program and i havent suspected anything until now.. rewriting everything with doubles or custom/special data types will be tedious..

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If its just the display that bothers you, then you could display fewer decimal places. For example, for 3 decimal places
    Code:
    sprintf(sTemp, "%0.3f\n", max);

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    its not the display.. i use that formula in my text display routine to calculate the width / height and # of characters to display..

    sometimes i get 19.9999 instead of 20, and that single 0.0001 causes the routine to draw 1 less character. the reason i always round down is so that it does not exceed the given width, and that is why i cannot round up either.

    if anyone has any idea as to how i can avoid this inaccuracy, it would be greatly appreciated

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    i guess that works, anyone have any idea what a suitable epsilon is?

    i set it to 0.0001f for the time being.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by X PaYnE X
    i guess that works, anyone have any idea what a suitable epsilon is?
    FLT_EPSILON?
    #include <float.h>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    that value seemed way too low.. i tried 0.0001 and it works fine, i just add or subtract epsilon from the value and use that instead..

    im assuming floats have an inaccuracy range of epsilon? im thinking of switching to a high precision library or something similar.. anyone have any suggestions?

  14. #14
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    cat foo.c | sed s/float/long long/g
    :P

    long or long long will most likely be precise enough for you, but if you're not satisfied with that, then I suppose you'll have to use a library (or write your own functions). Also, just be careful with your calculations. The inaccuracies in your output may well be due to not coding carefully enough - are you comparing floats just by using ==? Do you understand why floating point numbers have these issues? If not, maybe it would be a good idea to read up on it, as you will then be better able to compensate for their limitations in your code.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    actually, what im doing basically is, running that calculation using floats, then running it using integers.

    if the value of the floats > integers, ill take the float value otherwise the integer value..
    i added epsilon to the float value and did the > comparison and it works fine so far..

    but honestly, i have no idea why this is happening.. its not like the calculation needs decimal places.. its a simple calculation that can be done using integers.. 0.3 / 0.015 = 20 not 19.99999 or 20.00001

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM