Skip to content

proposal: math: add Float32SignificandBits and Float64SignificantBits #66640

Description

@dsnet

Proposal Details

I propose the addition of the following constants to the math package:

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
)

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 and Float64frombits already exist and are IEEE 754 specific.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions