0

I'm learning C language, I read about exp function at: https://linux.cn/man3/exp.3.html

At RETURN VALUE, There are 2 cases:

  • If the result underflows, a range error occurs, and zero is returned.
  • If the result overflows, a range error occurs, and the functions return +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL, respectively.

Could anybody please help me to explain these cases?

I wanna know that what input's value I have to use for exp function to occur these cases above.

Thanks for reading!

NovaStorm
  • 37
  • 2
  • 4
    What exactly is unclear for you in these two cases? – Gerhardh May 29 '18 at 11:02
  • It depends on type you use. [Float and double have the following range](https://stackoverflow.com/questions/10108053/ranges-of-floating-point-datatype-in-c), long double is more platform-dependent. [See this](http://en.cppreference.com/w/c/numeric/math/exp#Notes) for more info about error handling for these functions and acceptable values. – Yksisarvinen May 29 '18 at 11:03
  • 1
    Floating point variables (`float`, `double`, etc) cannot represent just any value. For each type, there are limits on how small (close to zero) and on how large (far from zero) a floating point value can be. A calculation that would (mathematically) be smaller than the minimum possible is said to underflow - and `exp()` returns a zero value. Conversely, a calculation that gives a result that is (mathematically) larger than the maximum allowed is said to overflow - in that case, `exp()` returns the largest value that can be represented in the return type. [Note: I'm oversimplifying] – Peter May 29 '18 at 11:05
  • 2
    I recomend doing your homework yourself. SO isn't a homework service. You can test ALL things by using trial and error. In this case it seems fairly safe to do. Alternatively use an analytic approach, meaning you should do the math. It's very easy. – Gladaed May 29 '18 at 11:07
  • Thanks all ! I'm using float type, I wanna know what input's value to result is underflow and overflow, please help me – NovaStorm May 29 '18 at 11:18
  • If we start with `exp(log(x)) == x` (unbounded infinite precision), then `expf(logf(FLT_MAX))` would ideally be `FLT_MAX`. Consequently, `expf(nextafterf(logf(FLT_MAX), INFINITY)` would be *greater than* `FLT_MAX` in unbounded arithmetic. Consequently, the result would be `INFINITY`. – EOF May 29 '18 at 12:02
  • "I wanna know what input's value to result is underflow and overflow," --> try all `float`, there is only 4,000,000,000 of them It should a few seconds to a few minutes on a common computer. – chux - Reinstate Monica May 29 '18 at 14:10
  • i tried with Infinity + 1 and -Infinity - 1, but it look like incorrect. – NovaStorm May 30 '18 at 02:29
  • I also try with value that out of float's range but it same as above – NovaStorm May 30 '18 at 02:31
  • [32bit float](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) has finite exponent <-126,127> and with denormalized values `-149` so `exp(-200)` should underflow and `exp(+200)` should overflow `float` with ease ... – Spektre May 30 '18 at 08:00
  • Thanks all!, can anyone help me to explain the "result", what does it mean? – NovaStorm May 30 '18 at 08:53

0 Answers0