Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: exp returns 0 instead of infinity for large values #14932

Closed
btracey opened this issue Mar 23, 2016 · 11 comments
Closed

math: exp returns 0 instead of infinity for large values #14932

btracey opened this issue Mar 23, 2016 · 11 comments
Milestone

Comments

@btracey
Copy link
Contributor

btracey commented Mar 23, 2016

http://play.golang.org/p/2uZRtZI_aH

fmt.Println(math.Exp(3.3709016683849786e+235))

return 0 instead of math.Inf(1)

@griesemer
Copy link
Contributor

Simpler test case: http://play.golang.org/p/oqww8UyFBQ

@griesemer griesemer self-assigned this Mar 23, 2016
@cespare
Copy link
Contributor

cespare commented Mar 23, 2016

The docs do say

Very large values overflow to 0 or +Inf.

@griesemer
Copy link
Contributor

@cespare Yes, just noticed as well. That said, this can probably be fixed w/o costing too much in the general case: If the result is 0 and the input is positive, the result should be +Inf.

It would be a backward compatible change that would make Exp behave less surprising.

@siritinga
Copy link

The threshold for returning +Inf is in exp.go (Overflow = 7.09782712893383973096e+02). Shouldn't it better to check this value in every implementation? This way all architectures should return the same, as currently arm (Raspberry Pi) returns +Inf

@griesemer
Copy link
Contributor

@siritinga Ideally, all platforms behave the same. On x86, the code is in assembly for speed. We want to make sure to not impact the performance significantly in the common case.

Either way, the current behavior is documented and thus this is technically not a bug. Leaving open for now because it would be nicer to behave exp in less surprising ways.

@btracey
Copy link
Contributor Author

btracey commented Mar 23, 2016

I hadn't noticed that documentation either. It is surprising, and as far as I know not a well known possible behavior of exp. For example, Matlab, Octave, Fortran, Julia, numpy.Exp, C, and C++ all return inf for the test case I posted.

@bradfitz bradfitz added this to the Unplanned milestone Apr 9, 2016
@0xmohit
Copy link
Contributor

0xmohit commented Sep 17, 2016

Related #7394

@xiachun
Copy link

xiachun commented Feb 3, 2017

I do notice that the document says with very large values math.Exp will overflow to 0 or +Inf. But I found it will overflow to unforeseen numbers, not just zero or +Inf, for example:

fmt.Println(math.Exp(1.48852223e+09))

return is 0.0027127673070219977, rather than 0 or +Inf.

In fact, with almost identical numbers, ouput may vary huge. eg. https://play.golang.org/p/D-XAs6nXN2

This may cause trouble. First I assume the zero return with positive value is +Inf, according to the document. The behavior of my program is strange, it cost me a full day to find why.

@ALTree
Copy link
Member

ALTree commented Feb 3, 2017

(math.Exp(1.48852223e+09) returning 0.0027127673070219977 is definitely a problem, the documentation says

Very large values overflow to 0 or +Inf

@ALTree ALTree modified the milestones: Go1.9, Unplanned Feb 3, 2017
@ALTree ALTree added the NeedsFix The path to resolution is known, but the work has not been done. label Feb 3, 2017
@ALTree
Copy link
Member

ALTree commented Feb 3, 2017

On second thought, let's keep this issue about the "exp returning 0 instead of +Inf" problem. I've filed #18912 for the wrong values problem.

@ALTree ALTree removed the NeedsFix The path to resolution is known, but the work has not been done. label Feb 3, 2017
@gopherbot
Copy link

CL https://golang.org/cl/36271 mentions this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants