-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: math: add Float32SignificandBits and Float64SignificantBits #66640
Comments
Make sense,however I feel like the scope of this constant is quite narrow, and it might only be applicable in specific cases. I think it's more suitable to define such constants within one's own package. Also, the constant name does seem a bit lengthy. |
In your title, s/Float64SignificantBits/Float64SignificandBits/. |
I would interpret |
You can already do: import (
"math"
"math/big"
)
var (
Float32SignificandBits = big.NewFloat(math.MaxFloat32).MinPrec() - 1
Float64SignificandBits = big.NewFloat(math.MaxFloat64).MinPrec() - 1
) |
This is hard to parse relative to the simple result it gives and it's not
is way better advice imo. |
What about this instead: const (
// float32SignificandBits is the number of bits in the significand
// for a IEEE 754 binary32 value.
// Integers within [-2<<float32SignificandBits, +2<<float32SignificandBits]
// can be exactly represented within a float32.
float32SignificandBits = 23
// float64SignificandBits is the number of bits in the significand
// for a IEEE 754 binary64 value.
// Integers within [-2<<float64SignificandBits, +2<<float64SignificandBits]
// can be exactly represented within a float64.
float64SignificandBits = 52
)
const (
// Float32SafeInteger represent the highest magnitude continuous integer based on IEEE 754, after which only less than half of the integers are available.
Float32SafeInteger = 2<<float32SignificandBits
// Float64SafeInteger represent the highest magnitude continuous integer based on IEEE 754, after which only less than half of the integers are available.
Float64SafeInteger = 2<<float64SignificandBits
) It seems to me this is the value we are after and it communicates the problem more clearly if you don't already know why javascript's integers are related to 52. I can see this being worst than |
@Jorropo Your descriptions for |
As a bikeshed, perhaps call it
The documentation can be adjusted. The goal is to document what range of integers is precise. Other than saying that integers outside this range are imprecise, I don't think we need to document exactly what the behavior is. |
The challenge with this is that I can't search across a code base for situations that might care about this type of conversion since logic does it slightly differently. Searching for the constants |
Proposal Details
I propose the addition of the following constants to the math package:
When inter-operating with JavaScript, it's common to clamp floating point integers to a certain range to ensure precise representation of integers. However, logic doing this often uses a hardcoded 52 or 53 constant, making it hard to discover where such logic may be occurring. By declaring this as a constant, we can check for all references to
math.Float64SignificandBits
within a codebase to see what logic may be concerned with integer to float64 conversions.These constants are very IEEE 754 centered, but I argue that is okay since
Float64bits
andFloat64frombits
already exist and are IEEE 754 specific.The text was updated successfully, but these errors were encountered: