Skip to content

Commit 48b489c

Browse files
authored
Merge pull request #656 from devlights:add-float-result-orderofcomputation
Add examples/basic/floatop
2 parents 956c87a + 3d44e74 commit 48b489c

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/devlights/try-golang/examples/basic/errs"
2222
"github.com/devlights/try-golang/examples/basic/fileio"
2323
"github.com/devlights/try-golang/examples/basic/filepaths"
24+
"github.com/devlights/try-golang/examples/basic/floatop"
2425
"github.com/devlights/try-golang/examples/basic/formatting"
2526
"github.com/devlights/try-golang/examples/basic/functions"
2627
"github.com/devlights/try-golang/examples/basic/goroutines"
@@ -105,6 +106,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
105106
errs.NewRegister().Regist(m)
106107
fileio.NewRegister().Regist(m)
107108
filepaths.NewRegister().Regist(m)
109+
floatop.NewRegister().Regist(m)
108110
formatting.NewRegister().Regist(m)
109111
functions.NewRegister().Regist(m)
110112
goroutines.NewRegister().Regist(m)

examples/basic/floatop/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
|file|example name|note|
6+
|----|------------|----|
7+
|order\_of\_computation.go|floatop\_order\_of\_computation|浮動小数点は計算の順序によって結果が変わることのサンプルです|
8+

examples/basic/floatop/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package floatop -- 浮動小数点の計算に関するサンプルが配置されているパッケージです。
3+
*/
4+
package floatop

examples/basic/floatop/examples.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package floatop
2+
3+
import "github.com/devlights/try-golang/mapping"
4+
5+
type (
6+
register struct{}
7+
)
8+
9+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
10+
func NewRegister() mapping.Register {
11+
return &register{}
12+
}
13+
14+
// Regist -- 登録します.
15+
func (r *register) Regist(m mapping.ExampleMapping) {
16+
m["floatop_order_of_computation"] = OrderOfComputation
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package floatop
2+
3+
import (
4+
"math/rand"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// OrderOfComputation -- 浮動小数点は計算の順序によって結果が変わることのサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://zenn.dev/kumackey/articles/d20230708-a7c195db087338
13+
// - https://wp.jmuk.org/2023/06/21/%e6%b5%ae%e5%8b%95%e5%b0%8f%e6%95%b0%e7%82%b9%e6%95%b0%e3%81%ae%e5%8a%a0%e7%ae%97%e3%81%ae%e9%a0%86%e5%ba%8f%e3%81%ab%e3%83%8f%e3%83%9e%e3%81%a3%e3%81%9f%e8%a9%b1/
14+
func OrderOfComputation() error {
15+
//
16+
// 浮動小数点は計算の順序によって結果が変わる
17+
// ただし、float32の場合は計算結果をfloat64で受ければ大丈夫
18+
//
19+
var (
20+
m = make(map[int]float32)
21+
)
22+
23+
for i := 0; i < 1000; i++ {
24+
v := rand.Float32()
25+
m[i] = v
26+
}
27+
28+
for i := 0; i < 10; i++ {
29+
var (
30+
total32 float32
31+
total64 float64
32+
)
33+
34+
for _, v := range m {
35+
total32 += v
36+
total64 += float64(v)
37+
}
38+
39+
output.Stdoutf("[Total (float32, float64)]", "%v\t%v\n", total32, total64)
40+
}
41+
42+
return nil
43+
}

0 commit comments

Comments
 (0)