Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang - How to turn an exponent and mantissa to a float64 or float32

Tags:

go

I have a library that gives floats in mantissa and exponent values for no loss of precision.

Mantissa: 375400000000000
Exponent: -9

How do I turn this into a float32 or float64 in golang?

I gave it a shot here but the number is definitely not coming out correctly: https://go.dev/play/p/8wMxhq11qwS

package main

import (
    "fmt"
    "math/big"
)

func main() {
    mantissa := 375600000000000
    exp := int8(-9)

    var price big.Float
    mant := big.NewFloat(float64(mantissa))
    price.SetMantExp(mant, int(exp))

    fmt.Println("price: ", price.String())
    fmt.Println("mant: ", mantissa)
    fmt.Println("exp: ", exp)
}

Apparently this doesn't work because big.Float is represented by sign × mantissa × 2**exponent when I need sign x mantissa x 10 ^ exp

like image 652
BAR Avatar asked Oct 18 '25 09:10

BAR


1 Answers

As someone who's had to deal with precision issues in Go quite a bit, the math/big package is an excellent alternative to using float32 or float64. However, it does not offer an exponentiation function. One package that does is ericlagergren/decimal:

package main

import (
    "fmt"

    "github.com/ericlagergren/decimal"
)

func main() {
    mantissa := decimal.New(375600000000000, 0)
    exp := decimal.New(-9, 0)

    ctx := decimal.Context {
        Precision: 9, // Set this to determine the number of expected decimal places
    }

    price := new(decimal.Big)
    ctx.Pow(price, decimal.New(10, 0), exp).Mul(price, mantissa)

    fmt.Println("price: ", price)
    fmt.Println("mant: ", mantissa)
    fmt.Println("exp: ", exp)
}

The upside to this package is that the result is guaranteed to the set precision. The downside is that you have to set that precision explicitly. In this example, I set a precision of 9 digits because an interim calculation would result in an output of 1e-9, which requires 9 digits of precision.

like image 132
Woody1193 Avatar answered Oct 20 '25 03:10

Woody1193



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!