Converting float to integer... without rounding!

This is a discussion on Converting float to integer... without rounding! within the C Programming forums, part of the General Programming Boards category; In the following code: Code: float FOO; FOO = 1.79; int BAR = FOO * 100; the value for integer ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    Converting float to integer... without rounding!

    In the following code:

    Code:
    float FOO;
    FOO = 1.79;
    
    int BAR = FOO * 100;
    the value for integer BAR is set to 178 rather than 179. I imagine this is an issue of rounding. How do I fix this?

    Thanks so much!

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Ahhh. why is it rounding?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since 1.79 is not exactly representable inside the machine, you probably want to add a fudge factor. If you know it's supposed to be two decimal points, for instance, you could add 0.001 before multiplying by 100.

    If you are trying to do exact arithmetic with decimal values (as in, say, money) that you really want to store the value in the correct units (pennies, instead of dollars) in an integer form.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Excellent, thank you

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by hebali View Post
    Ahhh. why is it rounding?
    Actually the problem is that it's not rounding, it's truncating. If it were rounding then it would work. In fact, rounding is what you want to do here. Rather than adding an arbitrary fudge factor that you simply hope is big enough, I'd just add 0.5 before truncating to an integer.

    Better yet as advised store the number in a kind of fixed point where the value is always multiplied by 100.
    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"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    Quote Originally Posted by tabstop View Post
    Since 1.79 is not exactly representable inside the machine, you probably want to add a fudge factor. If you know it's supposed to be two decimal points, for instance, you could add 0.001 before multiplying by 100.
    That will work for this particular example; it will give unwanted results for different values of FOO (or different multiplication factors, for that matter).

    Your other suggestion (eg dollars and cents as both integers, if working with currency) and iMalc's suggestions are more like it.

    Another way, if rounding is wanted rather than truncating towards zero (which is what happens when FOO*100 is converted implicitly to an int) is to compute the nearest integer.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by iMalc View Post
    Actually the problem is that it's not rounding, it's truncating.
    True. Technically. But the general problem is most often referred to as "round-off error".

    http://en.wikipedia.org/wiki/Round-off_error

    Interesting thought I just had. Instead of chopping the continuous binary representation, why wasn't it rounded - i.e. added a '1' to the last bit if the following to-be-chopped bit is a '1'. Would that have made the conversion back to base-10 any better? More accurate? I'll have to play with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding numbers
    By maple23 in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 12:33 AM
  2. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 12:47 AM
  3. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 08:07 PM
  4. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM

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