Skip to content

Pow() Returns Incorrect Result #394

@n3pheddy

Description

@n3pheddy

Pow() returns different values for certain exponents across different releases.

In Go Playground, try the following code:

package main

import (
	"fmt"

	"github.com/shopspring/decimal"
)

func main() {
	x := decimal.NewFromInt(10).Pow(decimal.NewFromInt(-18))
	fmt.Printf("10 to the power of -18 is %s", x)
}
-- go.mod --
module play.ground

require github.com/shopspring/decimal v1.4.0

//require github.com/shopspring/decimal v1.3.1

v1.4.0 returns 0 while v1.3.1 returns 0.000000000000000001

After a bit of testing, it looks like for v1.4.0, with base 10 and any integer exponent <= -17 will return 0
For v1.3.1, with base 10 and any odd integer exponent <= -17 will return 0, while even integer exponents return the correct result.

The expectation is for all negative integer exponents to return the correct result (0.1, 0.001, 0.0001, etc)

Tested on go version go1.23.4 darwin/arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions