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
51
【Go言語】浮動小数点
Go言語の浮動小数点について、解説しています。
【Qiita】
【Go言語】浮動小数点
Tomoki Ota
July 20, 2024
Tweet
Share
More Decks by Tomoki Ota
See All by Tomoki Ota
アーキテクチャの価値
tomo1227
0
53
アーキテクチャとビジネスロジック
tomo1227
0
58
【Go言語】テスト
tomo1227
0
32
【Go言語】クロージャ
tomo1227
0
220
【Go言語】イテレータ
tomo1227
0
39
【Go言語】エラーハンドリング
tomo1227
0
41
AIに淘汰されないようにするには?
tomo1227
0
37
【Go言語】range
tomo1227
0
31
【Go言語】break
tomo1227
0
39
Other Decks in Programming
See All in Programming
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
240
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
750
Cursorを活用したAIプログラミングについて 入門
rect
0
150
Golangci-lint v2爆誕: 君たちはどうすべきか
logica0419
1
230
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
1
4.7k
eBPF超入門「o11yに使える」とは (20250424_eBPF_o11y)
thousanda
1
110
KANNA Android の技術的課題と取り組み
watabee
0
180
Cursor/Devin全社導入の理想と現実
saitoryc
28
21k
サービスレベルを管理してアジャイルを加速しよう!! / slm-accelerate-agility
tomoyakitaura
1
200
Making TCPSocket.new "Happy"!
coe401_
1
3k
AIコーディングの理想と現実
tomohisa
35
37k
ニーリーQAのこれまでとこれから
nealle
2
160
Featured
See All Featured
A designer walks into a library…
pauljervisheath
205
24k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
How to Think Like a Performance Engineer
csswizardry
23
1.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
KATA
mclloyd
29
14k
Designing for Performance
lara
608
69k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
How GitHub (no longer) Works
holman
314
140k
The Invisible Side of Design
smashingmag
299
50k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Visualization
eitanlees
146
16k
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