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

Underflow should occur when evaluating exp for very small numbers #17893

Open
younisshah opened this issue Aug 8, 2016 · 4 comments
Open

Underflow should occur when evaluating exp for very small numbers #17893

younisshah opened this issue Aug 8, 2016 · 4 comments
Labels
domain:bignums BigInt and BigFloat domain:maths Mathematical functions

Comments

@younisshah
Copy link

When using exp function for very small numbers, instead of just returning 0, an underflow should occur. Consider the following snippet:

set_bigfloat_precision(100)
C = 5
m = BigFloat(9.10938356e-31)
ℏ = BigFloat(1.054571800e-34)
t = exp(-(sqrt(C * m) / ℏ))

In the last line, t evalutes to 0.

The reason for this behavior was explained to me very elegantly here on Stack Overflow by Oxinabox

@oxinabox
Copy link
Contributor

oxinabox commented Aug 8, 2016

Just taking a quick look at anther programming language I have with MPFR functionality

gAwk

awk -M '{ print exp(-1e8); print exp(-1e9);  };'  /dev/stdin #Press enter a few times
6.45171e-43429449
0

and unlike julia awk, does normally throw an exception on a underflow

awk '{print exp(-1e8);};'  /dev/stdin #Press enter a few times

awk: cmd. line:1: (FILENAME=/dev/stdin FNR=1) warning: exp: argument -1e+08 is out of range

So perhaps this suggests that it is accept-able to not raise exception on MFPR underflow.

(Cos gAwk is a well known numerical programming language, right?)

@oxinabox
Copy link
Contributor

oxinabox commented Aug 8, 2016

gmpy2, which I believe is the main python binding for mpfr (and GMP), does present an configurable option to throw an UnderflowError.

Which seem like generally a good idea.

@simonbyrne
Copy link
Contributor

There is a longstanding open issue for accessing floating point exceptions (#2976), but in the general case that is going to require work on LLVM itself. However we could expose some of the MPFR functionality; we probably don't want to throw an error by default (as silent underflow is the default behaviour for most floating point numbers), but we could enable a switch (similar to gmpy2), or have something like:

check_exception(BigFloat, Underflow) do
    t = exp(-(sqrt(C * m) / ℏ))
end

which would throw an error if any underflows occur inside.

@oxinabox
Copy link
Contributor

oxinabox commented Aug 9, 2016

This can probably be closed as a being a subset of #2976

Since one of its points is:

Expose similar flag-handling behavior from MPFR

@oscardssmith oscardssmith added domain:bignums BigInt and BigFloat domain:maths Mathematical functions labels Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:bignums BigInt and BigFloat domain:maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

4 participants