Thread: 0 float

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    0 float

    Hi there,

    i've been doing some coding and i came across a difficulty
    that (V?)C++ seems to turn a defined float 0 into very small
    float numbers (order 10e-8, 10e-7) later on. I spoke
    to someone who said that it is an inherent problem in VC++.

    so i guess when i want to test a float variable x < 0 i should
    be testing it against a very small float x < 10e-8 instead.
    but how do i know that x i defined 0 will not become for
    example of the order 10e-6 so it will return FALSE when i
    test it against x < 10e-8???

    thanx for your contributions...


  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    The precision for floating point type variables in C/C++ is :
    float : 7 digits
    double : 15 digits
    long double : 18 digits

    The reason for this is that with a finite amount of bits, it is impossible to represent every floating point value.

    Code:
    float epsilon = 1e-7;
    float a = 123.4567890123456789;
    float b = 123.4567890123456789;
    if (fabs(b-a) < epsilon)
       cout << "equal";
    else
      cout << "definately not equal";

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    This is not, perhaps, exactly on line with your specific question, but it does go to 'floats' and their representation.

    When a 'float' can be represented exactly by the computer, it's referred to as a model number.

    For example, let's say that our computer will hold the value 0.4512 as an exact, real value (assuming four-digit precision). The value 0.45119999, (a real number), is represented by the "model".

    In effect, the computer holds 0.45119999 as 0.4512. This is a representational error referred to as the "rounding error".

    (Oh, it gets better! )

    There are two ways of measuring this type of error. They are absolute error and relative error.

    "Absolute error" is the difference between the model and the real number. Pretty straightforward.

    "Relative error" is the absolute error divided by the real number, often represented as a percentage.

    Before I put you to sleep completely, - it's been known to happen - recognize that MSVC++ may well be representing zero as a "representation" of the model versus the model itself.

    Why this should take place is beyond me, but "equivalence" does seem to play a part here.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    it's not c++ only. it has to do with the way floats are stored. try casting to an int, or measuring against a very low number. (i think c++ defines one called ESP somewhere...?)

  5. #5
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Don't use :
    Code:
    float f = 0;
    use :

    Code:
    float f = 0.0f;

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    this has all been really helpfull!!!!!!!!!!!

    thanks

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