# C Float: Should use Division or Multiplication

This is a discussion on C Float: Should use Division or Multiplication within the C Programming forums, part of the General Programming Boards category; I don't know in C, which method is more accurate. For example: I have four float numbers: a,b,c,d. (suppose they ...

1. ## 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. 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.

3. 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
thanks

4. 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. 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.

6. 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.

7. Okay thanks, understood.
I totally missed the obvious case of multiplcation overflow.

8. 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").

9. Wow, I've never considered that option before!