Thread: C Float: Should use Division or Multiplication

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question C Float: Should use Division or Multiplication

    I don't know in C, which method is more accurate. For example: I have four float numbers: a,b,c,d. (suppose they are all different to zero).
    and I need to know: a/b (<,>,==) c/d. So, we should compare (a/b,c/d) or compare(a*d,b*c).

    So who know clearly this issue, please tell to me.
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no general answer. The best approach depends on how the numbers are obtained (for example, the result of a long series of computations) and any other information you know about them (for example, can you guarantee they are all of similar magnitudes, or can the magnitudes be markedly different?).

    As a rule of thumb, one should never test equality of floating point values. There are special cases where you can safely do so, but I encounter VERY few C or C++ programmers who can identify those cases, so my policy is to discourage doing it. If you want to do it, YOU can justify a claim it is safe to do so. If you can make your case properly and convincingly, you will be a very rare C or C++ programmer.

    Mathematically, if a,b,c,d are all positive, there is no difference between the comparisons (a/b > c/d) and (a*d > b*c). However, a program can only do the comparison numerically, so you need to ensure that any calculations performed do not underflow or overflow. If b is significantly less than 1, for example, calculation of a/b may overflow. If b is zero, the division is invalid (and you will need to decide if it even makes sense to do a comparison by either method). If a is small (much less than 1) and b is large (much greater than 1) the calculation a/b may underflow (or give a zero result). The problem is, multiplications can give the same problems: a*d can overflow if both values are large (much greater than 1) and underflow if both values are small (much less than 1).

    If you are expecting values of varying signs, there are other factors you need to account for. For example, if a is the only negative value with all others positive, then you know (a/b > c/d) and (a*d > b *c) will both test false.

    It gets even better if any of the values might be invalid (for example, IEEE NaN's).

    If you want a simple answer that you can always use, every time, forget it. Entire textbooks have been written on similar subjects.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Yes, I understand that knowledge about float number is not trivial. About compare float number . I often use those pieces of code:
    Code:
    typedef double cfloat
    const cfloat eps=1e-10;
    inline cmp(cfloat a, cfloat b){
    return (a<b+eps)?(a>b-eps)?0:-1:1;
    }
    But, I don't really know how C processing Real number, so I cannot choose what method is better
    I think someone know about this clearly and teach me
    thanks
    Last edited by hqt; 12-31-2011 at 08:21 AM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    grumpy seems to "know about it clearly" and has attempted to teach you all you're going to learn here. Hit the books if you want more. BTW, I believe floats are essentially the same in any language since it's really a hardware thing.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Typically this kind of comparison is used for detecting which side of a line a point is on and in that case there's usually nothing stopping the line from being horizontal or vertical. For such a thing using the multiplication method prevents either of those from having to be handled as a special case. Multiplication is also much faster that division.

    For these reasons, I thoroughly recommend always doing it via multiplication. In fact I disagree with there not being "a simple answer that you can always use". I understand all about the issues when values are of vastly differing magnitudes, that really only applies to things like addition. When multiplying values vastly differing magnitudes does not matter.
    Finally, if you do ever find a case where the division method gives a different answer to the multiplication method, and all the values are not zero, inf, or NaN, then I doubt you could say that the division one was more correct than the multiplication one.
    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,815
    iMalc, you have made specific assumptions about the type of problem being solved.

    The choice between using multiplication or division to support the comparison is a trade-off. There are circumstances where either technique can fail. And circumstances of failure can result, depending on the host machine (not all machines use IEEE floating point formats), in abnormal program termination.

    I never suggested that "vastly different magnitudes" matters for multiplication. The conditions where the multiplications can fail actually involve comparable magnitudes (for example, when the product of two values exceeds what can be represented in a floating point type).

    Unfortunately, there will always be people who try to use a cookbook approach to these things, and those who encourage them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay thanks, understood.
    I totally missed the obvious case of multiplcation overflow.
    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"

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    For a,b,c,d all positive, you might also wish to ponder when a test of log(a)-log(b) > log(c)-log(d) would be appropriate (and, no, it is not "always").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Wow, I've never considered that option before!
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-04-2011, 04:21 PM
  2. [beginner] float and division
    By codezero in forum C Programming
    Replies: 5
    Last Post: 04-27-2009, 09:32 PM
  3. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  4. issues in float division a/b
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 04-24-2008, 06:15 AM
  5. int division to float
    By rimig88 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2008, 08:48 AM