27

There are mathematical operations that yield real numbers from +/- infinity. For example exp(-infinity) = 0. Is there a standard for mathematical functions in the standard C library that accept IEEE-754 infinities (without throwing, or returning NaN). I am on a linux system and would be interested in such a list for glibc. I could not find such a list in their online manual. For instance their documentation on exp does not mention how it handles the -infinity case. Any help will be much appreciated.

srean
  • 2,578
  • 18
  • 18
  • 1
    Have you tried `exp(-infinity)`? Did it throw? Did it get NaN or 0? – Anders Abel Dec 06 '11 at 12:54
  • 10
    +1 even just for the title :-) – Matteo Italia Dec 06 '11 at 12:55
  • 3
    The language standard does not say anything about whether infinity is a representable value, so surely this is up to the implementation. – Kerrek SB Dec 06 '11 at 12:57
  • @Anders that example worked but want to know how portable it would be. I would be happy with POSIX say – srean Dec 06 '11 at 12:58
  • 9
    @srean: POSIX [`exp`](http://pubs.opengroup.org/onlinepubs/007904975/functions/exp.html) guarantees +0 for -Inf. – Mat Dec 06 '11 at 13:03
  • @Mat oh thanks. I will need a few other functions would it be possible for you to point me to a list. And if you can add it as an answer so that we can vote it up – srean Dec 06 '11 at 13:05
  • 3
    ¤ There are two aspects to IEEE 754: binary representation, and semantics. If `std::numeric_limits::is_iec559` yields `true`, then the intended meaning is that you can assume both representation and semantics. But in practice you can only assume the representation (e.g. consider g++ option `--fastmath`, IIRC). So as @KerrekSB notes, you'll have to check the implementation's documentation. Or just try out things. Cheers & hth., – Cheers and hth. - Alf Dec 06 '11 at 13:06
  • @srean: I don't know of a list - that's a rather strange request you have. Just look up the docs for either POSIX or your implementation (you'll need to check on each platform you're targetting anyway) – Mat Dec 06 '11 at 13:20
  • @Mat I did not realize that you already pointed me to a link. GCC's texinfo manual did not have these details so was asking for a place to look those up. Now I have it. Thanks – srean Dec 06 '11 at 13:28

2 Answers2

13

The See Also section of POSIX' math.h definition links to the POSIX definitions of acceptable domains.

E.g. fabs():

If x is ±0, +0 shall be returned.
If x is ±Inf, +Inf shall be returned.

I converted mentioned See Also-section to StackOverflow-Markdown:

acos(), acosh(), asin(), atan(), atan2(), cbrt(), ceil(), cos(), cosh(), erf(), exp(), expm1(), fabs(), floor(), fmod(), frexp(), hypot(), ilogb(), isnan(), j0(), ldexp(), lgamma(), log(), log10(), log1p(), logb(), modf(), nextafter(), pow(), remainder(), rint(), scalb(), sin(), sinh(), sqrt(), tan(), tanh(), y0(),

I contributed search/replace/regex-fu. We now just need someone with cURL-fu.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • Oh man ! I guess I should have said "now that I have the url I dont need the list. I can look up the functions I use". But thanks a lot, feeling bad that I made you look all that stuff up. – srean Dec 06 '11 at 13:54
  • @srean: That's okay, it's useful for myself :) – Sebastian Mach Dec 06 '11 at 13:57
  • 3
    If you want these all in one tidy place, the posix edge cases are (almost) all inherited from Annex F of the C standard. – Stephen Canon Dec 06 '11 at 15:32
  • @Stephen: Cool, I didn't know that. It is over my fu though to convert them to Markdown in a lazy manner. – Sebastian Mach Dec 06 '11 at 15:43
9

In C99 it's on Appendix F:

F.9.3.1 The exp functions
-- exp(±0) returns 1.
-- exp(-∞) returns +0.
-- exp(+∞) returns +∞.

Appendix F is normative and:

An implementation that defines __STDC_IEC_559__ shall conform to the specifications in this annex.

ninjalj
  • 42,493
  • 9
  • 106
  • 148