float representation

This is a discussion on float representation within the C# Programming forums, part of the General Programming Boards category; Hello everyone, In my knowledge, float could only be represented by 7 decimal bits. But why in debugger, I can ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    float representation

    Hello everyone,


    In my knowledge, float could only be represented by 7 decimal bits. But why in debugger, I can see more than 7 decimal bits?

    (in my sample, Result2 is of 9 bits, other than 7, why?)

    Code:
                float TotalBonus = 199.321F;
                float Worker1 = 100F;
                float Worker2 = 300F;
    
                // 		Result1	49.83025	float
                float Result1 = TotalBonus * Worker1 / (Worker1 + Worker2);
    
                // 		Result2	149.490753	float
                float Result2 = TotalBonus * Worker2 / (Worker1 + Worker2);

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The float format is architecture dependant, but on x86, it is a 23(+1)-bit mantissa, which means that you get approximately 24/log2(10) -> 24/3.3 -> 7.2 digits. This means that you have AROUND 7 digits, but the precise number of digits depends on the value in the float itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    1.

    I do not think 7.2 digits is near 9 digits. :-)

    Any ideas? Maybe debugger has a special representation for float number?

    2.

    I think the calculation formular for how many digits in base 10 should be log(10 base) (2 ^ 24 - 1), approximately to log(10 base) (2 ^ 24 ), could you explain your calculation formular please?

    Quote Originally Posted by matsp View Post
    The float format is architecture dependant, but on x86, it is a 23(+1)-bit mantissa, which means that you get approximately 24/log2(10) -> 24/3.3 -> 7.2 digits. This means that you have AROUND 7 digits, but the precise number of digits depends on the value in the float itself.

    --
    Mats

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Hi Mats,


    1.

    I do not think 7.2 digits is near 9 digits. :-)
    No, but it's only 1 digit more - in the sense that the 0.2 is either present or not present, which makes the nominal number either 7 or 8 digits. But that's on average. Since a binary number is a precise representation of a decimal number, you also get artifacts, so I suspect that you may have a "endless number", e.g 0.1, that would come out as 0.0999999999[keep going with nines until the end of the universe (time or space, whichever you reach first)] if there is no rounding involved. Other numbers may also "leave some leftover" after it's been translated to decimal.

    Any ideas? Maybe debugger has a special representation for float number?
    It probably uses something like "fit as much as possible in ten places with either %f or %e", but I'm not sure.
    2.

    I think the calculation formular for how many digits in base 10 should be log(10 base) (2 ^ 24 - 1), approximately to log(10 base) (2 ^ 24 ), could you explain your calculation formular please?
    To form one decimal digit, we need log2(10) bits. Since we have 24 bits, we can produce 24/3.3 digits from that. Of course, a digit is either present or not present. Similarly, a 32-bit binary number will take up 32/log2(16) digits in hex -> so 8 digits, or 32/log2(8) in octal. All these numbers would require rounding up to the nearest integer. Floating point numbers may or may not fill all the positions in the number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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, 01: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, 04:26 PM

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