3

In C, can you ever exceed 1.0 if you have a number divided by itself? Basically, can x/x (e.g. 5.1/5.1) ever end up being greater than 1.0? x could be a float, double, or long double. Note that x/x wouldn't be literal code, like variable x over itself.

I tried searching for this answer but it was hard coming up with good search terms.

Ryan
  • 85
  • 6
  • There are probably 1000 existing questions here about floating point accuracy. There's also the obligatory link to [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) – Ken White Aug 25 '14 at 23:20
  • I am quite aware of the problems with floating point, thus the particular question. The problem with searching for an answer to this particular question is that I get tons of "don't do (5/2 == 2.5)". I know that, but it's very hard to figure out if a number over itself can ever not equal 1.0 (maybe due to difference in type promotion or who knows what). I'm reading over the link you sent but it's not looking promising. – Ryan Aug 25 '14 at 23:34
  • 3
    @KenWhite The document you link to is not relevant to the **specific** question asked here. It describes how x/x is exactly 1.0 in IEEE 754 or in IEEE 754-like floating-point arithmetic. The question is about what can happen in C. The entire question rests on how well a C compiler has to implement IEEE 754 arithmetic, and the document you link to says nothing about that. A 100ish pages link providing only relevant information that can be summarized in one sentence is not helpful. – Pascal Cuoq Aug 26 '14 at 06:47
  • What exactly are you asking here? What does "Note that x/x wouldn't be literal code, like variable x over itself" mean? – tmyklebu Aug 26 '14 at 09:19
  • @KenWhite: There are also probably several thousand questions here about Delphi. What's your point? – tmyklebu Aug 26 '14 at 11:03
  • @tmyklebu That was poor wording. I was trying to say that x/x should not be interpreted as "double d = 5.0/5.0;". Rather, x/x merely means any number divided by itself. So it could be z=x/y where a human thinks x==y. – Ryan Aug 26 '14 at 13:00
  • 1
    @Ryan: What sort of human? Does the human know how floating-point arithmetic works? – tmyklebu Aug 26 '14 at 17:21

1 Answers1

7

Assuming IEEE-754 conformance, x/x is always exactly 1.0*, because division is a correctly-rounded basic operation, meaning it returns the floating-point number closest to the "infinitely precise" mathematical value.

However, there are a few gotchas. For example, if FLT_EVAL_METHOD is non-zero in your C implementation, and x is actually an expression like (a+b)/(a+b), then it is conceivable (unlikely, but it has been observed) that under certain optimization settings the result might not be exactly equal to 1.0. And of course, if your compiler does not conform to IEEE-754, all bets are off.

[*] or NaN if x is zero or infinity or NaN.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • `x/x` can be `1.0` or NaN. (Neither of which is greater than `1.0`.) – tmyklebu Aug 26 '14 at 09:21
  • I am doing linear interpolation: `(x - from_min) / (from_max - from_min) * (to_max - to_min) + to_min`; your answer seems to indicate that I could end up with a result slightly larger than `to_max`, and perhaps smaller than `to_min`. Is that correct, as far as you know? – Levi Morrison Aug 26 '14 at 15:45
  • @LeviMorrison Your comment is not a comment at all but a new question (the answer is yes, the result of your computation can be larger than `to_max`, not so much because of FLT_EVAL_METHOD, which would in fact help if known to be non-zero, but because of floating-point arithmetic in general. See http://stackoverflow.com/questions/13725802/properties-of-80-bit-extended-precision-computations-starting-from-double-precis for a similar example) – Pascal Cuoq Sep 06 '14 at 14:08