Skip to content

math/big: missing Rat.Float32 implementation #8065

@griesemer

Description

@griesemer
float32(Rat.Float64(x)) is not the same as Rat.Float32(x) in general (adjusted for
correct arguments, etc.):

package main

import (
    "fmt"
    "math/big"
)

func main() {
    const x = 340282356779733661637539395458142568447
    fmt.Println(float64(x), float32(x))
    z, ok := new(big.Rat).SetString("340282356779733661637539395458142568447")
    if !ok {
        panic("SetString failed")
    }
    t, _ := z.Float64()
    fmt.Println(t, float32(t))
}

produces:

3.4028235677973366e+38 3.4028235e+38
3.4028235677973366e+38 +Inf

i.e., we need a Rat.Float32 implementation with correct rounding for 32bit floats.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions