1

for double or float,

1. -inf < inf == true

2. inf + inf == inf

3. -inf - inf == -inf

4. positive / 0.0 == inf

5. negative / 0.0 == -inf

6. any real number < inf == true

7. -inf < any real number == true

Is this statement true?

We don't know (inf < inf), but we can sure it doesn't throw any exceptions.

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
  • Isn't it easier to write this to an IDE and press build? `static_assert` might be useful... – Jaa-c Dec 31 '14 at 13:26
  • What makes you think `num / 0.0 == inf`? It is mathematically illegal to divide by zero. – Cory Kramer Dec 31 '14 at 13:28
  • Just try it with an online compiler, it shouldn't take more than 5 minutes :) – Dimitri Mockelyn Dec 31 '14 at 13:31
  • 3
    It works in my compiler, but I want to know which of those are standard. – Snow Innocent Dec 31 '14 at 13:41
  • If divide by zero is invalid, why IEEE-754 has expressions for infinite and negative infinite? – Snow Innocent Dec 31 '14 at 13:46
  • Don't mix limits with actual values. Division with 0 isn't defined, even though the limit of C/n as n --> 0 is infinite. It would make more sense if those are something like NaN. (Furthermore, don't `int` have limits? How would you be able to perform operations with infinity if there's a maximum integer value in programming?) – Max Dec 31 '14 at 13:59
  • 1
    @SnowInnocent: If `std::numeric_limits::is_iec`something is true then you should ideally have IIEE 754 rules. However, especially with g++ that's those rules are broken when you ask it to optimize. So for the in-practice you do not have guarantees regardless of the formal, but I think not even in the formal. – Cheers and hth. - Alf Dec 31 '14 at 14:00
  • @Max IEEE 754 has `+inf`, `-inf` and `NaN`. You are free to invent your own floating-point system in which it doesn't make sense to use `+inf` and `-inf` and `1.0/0.0` is `NaN`, but the OP is not asking about your system. The OP is asking about IEEE 754. – Pascal Cuoq Dec 31 '14 at 14:26
  • @Pascal Cuoq I'm not that experienced with IEEE 754, so I just looked at it from a mathematical perspective (I didn't see him mentioning that in the question, so I just went with my experience). Googled around a bit and found what you mean. Thanks for answering my parenthesis question though. – Max Dec 31 '14 at 14:31
  • When asking about standard, are you aware C++ standard does not require IEEE 754 conformant floating point? See also http://stackoverflow.com/questions/9418505/double-ieee-754-alternatives and http://stackoverflow.com/questions/5777484/how-to-check-if-c-compiler-uses-ieee-754-floating-point-standard – Suma Dec 31 '14 at 15:01

1 Answers1

5

All the expressions 1-7 in your question evaluate to true when compiled by a compiler that implements IEEE 754 rules. None of them depend on the rounding mode. For many, this is simply because < and == are exact operations. In addition, -inf - inf is always -inf (that is, in all rounding modes), positive / 0.0 is always +inf, and negative / 0.0 is always -inf.

In 6. and 7. below, it is usual to call the values you refer to “finite” rather than “real”.

  1. any real number < inf == true

  2. -inf < any real number == true

inf < inf always evaluates to false. This, like all the properties 1-7 in your question, should be considered a matter of convention. It was decided to make inf equal to itself, so that it is not strictly less than itself. All these choices are intended to make IEEE 754 arithmetic as useful as possible in practice. In other words, the intention was to have as many algorithms as possible being implementable without having to check explicitly for these special values, and the computation continuing to make sense (when applicable) when they occur. When the special values do not make sense, of course there is always the possibility to test for them.

Community
  • 1
  • 1
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281