File tree Expand file tree Collapse file tree 5 files changed +33
-0
lines changed Expand file tree Collapse file tree 5 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 66| ----| ------------| ----|
77| order\_ of\_ computation.go| floatop\_ order\_ of\_ computation| 浮動小数点は計算の順序によって結果が変わることのサンプルです|
88| rounding\_ error.go| floatop\_ rounding\_ error| 小数点計算において近似値が利用され丸め誤差が出るサンプルです|
9+ | using\_ decimal\_ pkg.go| floatop\_ using\_ decimal\_ pkg| 小数点計算を github.com/shopspring/decimal パッケージを利用して処理するサンプルです|
Original file line number Diff line number Diff line change @@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
1515func (r * register ) Regist (m mapping.ExampleMapping ) {
1616 m ["floatop_order_of_computation" ] = OrderOfComputation
1717 m ["floatop_rounding_error" ] = RoundingError
18+ m ["floatop_using_decimal_pkg" ] = UsingDecimalPkg
1819}
Original file line number Diff line number Diff line change 1+ package floatop
2+
3+ import (
4+ "github.com/devlights/gomy/output"
5+ "github.com/shopspring/decimal"
6+ )
7+
8+ // UsingDecimalPkg は、小数点計算を github.com/shopspring/decimal パッケージを利用して処理するサンプルです。
9+ //
10+ // # REFERENCES
11+ // - https://engineering.mercari.com/blog/entry/20201203-basis-point/
12+ // - https://github.com/shopspring/decimal
13+ //
14+ // # SEE ALSO
15+ // - examples/basic/floatop/rounding_error.go
16+ func UsingDecimalPkg () error {
17+ var (
18+ v = decimal .RequireFromString ("0" )
19+ )
20+
21+ for i := 0 ; i < 1000 ; i ++ {
22+ v = v .Add (decimal .RequireFromString (".01" ))
23+ }
24+
25+ output .Stdoutl ("[result]" , v .StringFixed (1 ))
26+
27+ return nil
28+ }
Original file line number Diff line number Diff line change 55require (
66 github.com/devlights/gomy v0.6.0
77 github.com/pelletier/go-toml/v2 v2.0.9
8+ github.com/shopspring/decimal v1.3.1
89 golang.org/x/crypto v0.11.0
910 golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
1011 golang.org/x/sync v0.3.0
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N
77github.com/pelletier/go-toml/v2 v2.0.9 /go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc =
88github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
99github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
10+ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8 =
11+ github.com/shopspring/decimal v1.3.1 /go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o =
1012github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
1113github.com/stretchr/objx v0.4.0 /go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw =
1214github.com/stretchr/objx v0.5.0 /go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo =
You can’t perform that action at this time.
0 commit comments