Commit 02917d07 by yemin

重新实现了浮点数精度算法

parent 6ad607ab
......@@ -37,6 +37,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/swaggo/swag v1.8.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
......
......@@ -129,6 +129,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
......
package util
import (
"math/big"
"github.com/shopspring/decimal"
"strconv"
)
......@@ -17,24 +17,28 @@ func FormatFloat32(f float32) string {
// AddFloat64 精度加法 a+b
func AddFloat64(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Add(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
//r, _ := new(big.Float).Add(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
r, _ := decimal.NewFromFloat(*a).Add(decimal.NewFromFloat(*b)).Float64()
return &r
}
// SubtractFloat64 精度减法 a-b
func SubtractFloat64(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Sub(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
//r, _ := new(big.Float).Sub(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
r, _ := decimal.NewFromFloat(*a).Sub(decimal.NewFromFloat(*b)).Float64()
return &r
}
// MultiplyFloat64 精度乘法 axb
func MultiplyFloat64(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Mul(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
//r, _ := new(big.Float).Mul(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
r, _ := decimal.NewFromFloat(*a).Mul(decimal.NewFromFloat(*b)).Float64()
return &r
}
// DivideFloat64 精度除法 a/b
func DivideFloat64(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Quo(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
//r, _ := new(big.Float).Quo(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
r, _ := decimal.NewFromFloat(*a).Div(decimal.NewFromFloat(*b)).Float64()
return &r
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment