Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
【Go言語】浮動小数点
Search
Tomoki Ota
July 20, 2024
Programming
0
56
【Go言語】浮動小数点
Go言語の浮動小数点について、解説しています。
【Qiita】
【Go言語】浮動小数点
Tomoki Ota
July 20, 2024
Tweet
Share
More Decks by Tomoki Ota
See All by Tomoki Ota
アーキテクチャの価値
tomo1227
0
68
アーキテクチャとビジネスロジック
tomo1227
0
73
【Go言語】テスト
tomo1227
0
42
【Go言語】クロージャ
tomo1227
0
230
【Go言語】イテレータ
tomo1227
0
53
【Go言語】エラーハンドリング
tomo1227
0
50
AIに淘汰されないようにするには?
tomo1227
0
44
【Go言語】range
tomo1227
0
45
【Go言語】break
tomo1227
0
52
Other Decks in Programming
See All in Programming
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
320
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
110
Is Xcode slowly dying out in 2025?
uetyo
1
180
Datadog RUM 本番導入までの道
shinter61
1
310
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
250
GoのGenericsによるslice操作との付き合い方
syumai
2
680
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
3
650
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
970
XP, Testing and ninja testing
m_seki
2
140
GraphRAGの仕組みまるわかり
tosuri13
7
460
CursorはMCPを使った方が良いぞ
taigakono
0
150
Java on Azure で LangGraph!
kohei3110
0
170
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
It's Worth the Effort
3n
184
28k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
How to train your dragon (web standard)
notwaldorf
92
6.1k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Designing Experiences People Love
moore
142
24k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
Music & Morning Musume
bryan
46
6.6k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Transcript
Tomoki Ota ʲGoݴޠʳුಈখ
f loat32 f loat64 Goͷුಈখܕ 2
Goͷුಈখܕ 3
Goͷුಈখܕ 1.0001 * 1.0001 = 1.00020001 ؙΊޡࠩͰग़ྗ݁Ռ1.0002ʹͳΔ 4
খ : ແݶݸଘࡏ͢Δ f loat64 : ༗ݶɻ64bit Goͷුಈখܕ 5
ූ߸s, ԾF, ࢦeͷͱ͖ͷ10ਐЋ Goͷුಈখܕ α = (−1)s × F ×
2e 6
Goͷුಈখܕ 1.0001 = 1 × 1.000100016593933 × 20 ූ߸s, ԾF,
ࢦeͷͱ͖ͷ10ਐЋ 7
୯ਫ਼ුಈখ( f loat32) : ࢦ8ϏοτɺԾ23Ϗοτ ഒਫ਼ුಈখܕ( f loat64) : ࢦ11ϏοτɺԾ52Ϗοτ
Goͷුಈখܕ 8
Goͷුಈখܕ ූ߸ ࢦ(8bit) Ծ(23bit) 0 1111111 00000000000001101000111 9
ൺֱ ϓϩηοαʹґଘ͢Δ ಛघͳුಈখ ޡࠩͷੵ ۙࣅΛ͏͜ͱʹΑΔฐ 10
ۙࣅΛ͏ͱɺ==Λ༻ͨ͠ൺֱ͕ෆਖ਼֬ʹͳΔ߹ ͕͋Δɻ goͷςετϥΠϒϥϦtestify ʹൺֱ͍ͯ͠Δ͕Ўͷ ൣғͰ͋Δ͜ͱΛݕࠪ͢ΔͨΊͷInDelta͕ଘࡏ͢Δɻ ൺֱ 11
ϓϩηοαʹɺුಈখԋࢉΛॲཧ͢Δුಈখԋࢉ Ϣχοτ(Floating Point Unit, FPU)͕͋Δɻ ॲཧ݁ՌFPUʹґଘ͢ΔͷͰɺFPU͕ҟͳΔผͷϚγϯͰ ಉ݁͡ՌʹͳΔอূͳ͍ɻ ͪ͜Β 𝛿 Λߟ͑Δ͜ͱͰղܾ͢Δɻ
ϓϩηοαʹґଘ͢Δ 12
ϓϩηοαʹɺුಈখԋࢉΛॲཧ͢Δුಈখԋࢉ Ϣχοτ(Floating Point Unit, FPU)͕͋Δɻ ॲཧ݁ՌFPUʹґଘ͢ΔͷͰɺFPU͕ҟͳΔผͷϚγϯͰ ಉ݁͡ՌʹͳΔอূͳ͍ɻ ͪ͜Β 𝛿 Λߟ͑Δ͜ͱͰղܾ͢Δɻ
ಛघͳුಈখ 13
• ਖ਼ͷແݶେ • ෛͷແݶେ •NaN(Not-a-Number) ಛघͳුಈখ 14
NaNf != fΛຬͨ͢།Ұͷුಈখ ಛघͳුಈখ 15
ಛघͳුಈখ 16
ಛघͳුಈখ ͜ΕΒ3ͭͷුಈখʹ͍ͭͯͷൺֱɺ math.IsInf(ແݶ)Ͱɺmath.IsNan(NaN)ͰൺֱͰ͖ Δɻ 17
ޡࠩͷੵ(Ճࢉɾݮࢉ) 10000ʹ1.0001ΛnճՃࢉ͢ΔϓϩάϥϜ 18
ޡࠩͷੵ(Ճࢉɾݮࢉ) 1.0001ΛnճՃࢉͯ͠ɺ࠷ޙʹ10000ΛՃࢉ͢ΔϓϩάϥϜ 19
ޡࠩͷੵ(Ճࢉɾݮࢉ) n ਖ਼֬ͳ f1 f2 10 10010.001 10010.001 10010.001 1K
11000.1 11000.0999999993 11000.1 1M 1.0101E+06 1.01009999997614E+06 1.01009999997668E+06 ՃࢉݮࢉΛ࿈ଓͯ͠ߦ͏߹ɺେ͖͞ͷ͍ۙΛ·ͱΊͯܭࢉ͔ͯ͠Β େ͖ͳΛՃࢉɾݮࢉ͢Δͱɺޡ͕ࠩখ͘͞ͳΔɻ 20
ޡࠩͷੵ(ࢉɾআࢉ) a × (b + c) a × b +
a × c v.s. 21
ޡࠩͷੵ(ࢉɾআࢉ) a × (b + c) a × b +
a × c v.s. 22
ޡࠩͷੵ(ࢉɾআࢉ) a × b + a × c ͷํ͕ޡ͕ࠩগͳ͍ ՃࢉɾݮࢉΑΓࢉɾՃࢉΛઌʹߦ͏ͱ
ޡࠩΛখ͘͢͞Δ͜ͱ͕Ͱ͖Δ 23
Goͷ f loat32, f loat64ۙࣅΛͱΔͷͰɺޡ͕ࠩൃੜ͢Δ Ճࢉɾݮࢉಉܻ͡ͷԋࢉΛ·ͱΊͯߦ͏͜ͱͰɺਫ਼্͕͕Δ ࢛ଇԋࢉ͕ࠞ߹͢Δ߹ɺࢉͱআࢉΛઌʹߦ͏͜ͱͰɺਫ਼্͕͕Δ ·ͱΊ 24