Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rym-util
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengqiuyun
rym-util
Commits
02917d07
Commit
02917d07
authored
Nov 17, 2022
by
yemin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重新实现了浮点数精度算法
parent
6ad607ab
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
5 deletions
+12
-5
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
FloatUtil.go
util/FloatUtil.go
+9
-5
No files found.
go.mod
View file @
02917d07
...
...
@@ -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
...
...
go.sum
View file @
02917d07
...
...
@@ -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=
...
...
util/FloatUtil.go
View file @
02917d07
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment