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
41
【Go言語】浮動小数点
Go言語の浮動小数点について、解説しています。
【Qiita】
【Go言語】浮動小数点
Tomoki Ota
July 20, 2024
Tweet
Share
More Decks by Tomoki Ota
See All by Tomoki Ota
アーキテクチャの価値
tomo1227
0
40
アーキテクチャとビジネスロジック
tomo1227
0
44
【Go言語】テスト
tomo1227
0
22
【Go言語】クロージャ
tomo1227
0
210
【Go言語】イテレータ
tomo1227
0
30
【Go言語】エラーハンドリング
tomo1227
0
33
AIに淘汰されないようにするには?
tomo1227
0
30
【Go言語】range
tomo1227
0
21
【Go言語】break
tomo1227
0
33
Other Decks in Programming
See All in Programming
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
560
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
710
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
DROBEの生成AI活用事例 with AWS
ippey
0
130
チームリードになって変わったこと
isaka1022
0
200
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
840
WebDriver BiDiとは何なのか
yotahada3
1
140
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
120
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
120
Spring gRPC について / About Spring gRPC
mackey0225
0
220
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
110
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
250
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
A Philosophy of Restraint
colly
203
16k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
It's Worth the Effort
3n
184
28k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Automating Front-end Workflow
addyosmani
1368
200k
Documentation Writing (for coders)
carmenintech
67
4.6k
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