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

f32/f64 MIN_EXP is either wrong or badly documented. #43583

Closed
eddyb opened this issue Aug 1, 2017 · 5 comments
Closed

f32/f64 MIN_EXP is either wrong or badly documented. #43583

eddyb opened this issue Aug 1, 2017 · 5 comments

Comments

@eddyb
Copy link
Member

eddyb commented Aug 1, 2017

They are both -MAX_EXP + 3 while core::num::dec2flt::RawFloat uses -MAX_EXP + 1.
Also, APFloat (being ported to Rust in #43554) uses the same -MAX_EXP + 1 value.
That is the value of the exponent for the smallest normal, AFAIK.

cc @rust-lang/libs @est31 @nagisa @lifthrasiir @rkruppe

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Aug 1, 2017

I chased the git blame chain and apparently -MAX_EXP + 3 matches, and historically stems from C's (or at least GCC's) {FLT,DBL}_MIN_EXP 🤔

@kennytm
Copy link
Member

kennytm commented Aug 1, 2017

MIN_EXP is "One greater than the minimum possible normal power of 2 exponent". For f64, the smallest normal number is 2-1022, so MIN_EXP = -1022 + 1 = -1021 as expected.

I don't know why such a strange definition is picked, but this follows how ***_MIN_EXP is defined in C and C++.

C11 (n1570) §5.2.4.2.2/11

minimum negative integer such that FLT_RADIX raised to one less than that power is a normalized floating-point number, emin

FLT_MIN_EXP
DBL_MIN_EXP
LDBL_MIN_EXP

C++14 (n3936) §18.3.2.4/27:

static constexpr int  min_exponent;

Minimum negative integer such that radix raised to the power of one less than that integer is a normalized floating point number.

@eddyb
Copy link
Member Author

eddyb commented Aug 1, 2017

@kennytm That doesn't add up. APFloat has 2MIN_EXP as the smallest normal number.
So 2-1023 for f64, which would make std::f64::MIN_EXP -1022. What am I missing?

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Aug 1, 2017

Uhh, the smallest normal f64 is indeed 2-1022, not 2-1023. The latter is a subnormal with a significand of 0b1000....

@eddyb
Copy link
Member Author

eddyb commented Aug 1, 2017

Oh, figured it out. APFloat MIN_EXP is indeed -1022. What I had in mind was that it was defined as -MAX_EXP + 1 which is true but MAX_EXP also is off-by-one from libstd.
rawfp uses the same values as APFloat, so this is only some C nonsense. My bad.

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

No branches or pull requests

3 participants