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
52
【Go言語】浮動小数点
Go言語の浮動小数点について、解説しています。
【Qiita】
【Go言語】浮動小数点
Tomoki Ota
July 20, 2024
Tweet
Share
More Decks by Tomoki Ota
See All by Tomoki Ota
アーキテクチャの価値
tomo1227
0
58
アーキテクチャとビジネスロジック
tomo1227
0
66
【Go言語】テスト
tomo1227
0
37
【Go言語】クロージャ
tomo1227
0
220
【Go言語】イテレータ
tomo1227
0
44
【Go言語】エラーハンドリング
tomo1227
0
44
AIに淘汰されないようにするには?
tomo1227
0
39
【Go言語】range
tomo1227
0
36
【Go言語】break
tomo1227
0
45
Other Decks in Programming
See All in Programming
「MCPを使ってる人」が より詳しくなるための解説
yamaguchidesu
0
580
Interface vs Types ~型推論が過多推論~
hirokiomote
1
230
推論された型の移植性エラーTS2742に挑む
teamlab
PRO
0
150
💎 My RubyKaigi Effect in 2025: Top Ruby Companies 🌐
yasulab
PRO
1
130
"使いづらい" をリバースエンジニアリングする UI の読み解き方
rebase_engineering
0
110
Language Server と喋ろう – TSKaigi 2025
pizzacat83
2
660
TypeScript製IaCツールのAWS CDKが様々な言語で実装できる理由 ~他言語変換の仕組み~ / cdk-language-transformation
gotok365
7
370
バランスを見極めよう!実装の意味を明示するための型定義 TSKaigi 2025 Day2 (5/24)
whatasoda
2
770
複雑なフォームを継続的に開発していくための技術選定・設計・実装 #tskaigi / #tskaigi2025
izumin5210
12
6.3k
「兵法」から見る質とスピード
ickx
0
150
Babylon.js 8.0のアプデ情報を 軽率にキャッチアップ / catch-up-babylonjs-8
drumath2237
0
110
『Python → TypeScript』オンボーディング奮闘記
takumi_tatsuno
1
130
Featured
See All Featured
Unsuck your backbone
ammeep
671
58k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.6k
The Pragmatic Product Professional
lauravandoore
35
6.7k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Building Adaptive Systems
keathley
41
2.6k
Done Done
chrislema
184
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.3k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
19k
Practical Orchestrator
shlominoach
188
11k
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