-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
double.IsPow2
reports incorrectly for subnormal numbers
#88023
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescription
Reproduction StepsExecute the following: double.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 16); //not subnormal, = 2^-1022
double.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 8); //is subnormal, = 2^-1023
double.IsPow2(double.Epsilon); //is subnormal, = 2^-1074 Expected behaviordouble.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 16); //true
double.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 8); //true
double.IsPow2(double.Epsilon); //true Actual behaviordouble.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 16); //true
double.IsPow2(double.Epsilon * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 8); //false
double.IsPow2(double.Epsilon); //false Regression?No response Known WorkaroundsNo response ConfigurationReproduces on: Other informationThe simplest correct check I can think of for this would be: check the negative bit is not set, check the exponent is not the max exponent (can be combined with previous check), if the exponent is not zero: check the mantissa is 0, if the exponent is zero: check the PopCount is 1. It seems to also reproduce with
|
Good catch @hamarb123 |
Description
double.IsPow2
should returntrue
for for all doubles which are a power of 2, and false otherwise. It doesn't appear to work correctly for subnormal numbers.Reproduction Steps
Execute the following:
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
Reproduces on:
.NET 8.0.0-preview.5.23280.8, 7.0.5.
Windows 10 Pro 10.0.19045.3086 x64
No, it probably isn't specific to this configuration.
Other information
The simplest correct check I can think of for this would be: check the negative bit is not set, check the exponent is not the max exponent (can be combined with previous check), if the exponent is not zero: check the mantissa is 0, if the exponent is zero: check the PopCount is 1.
It seems to also reproduce with
float
andHalf
, I only checkedT.IsPow2(T.Epsilon)
, but these should both returntrue
also.The text was updated successfully, but these errors were encountered: