Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
丸め誤差発生の仕組みと向き合い方
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
えび
October 21, 2024
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
丸め誤差 発生の仕組みと向き合い方
https://xvi-pea.hatenablog.com/entry/2024/10/19/211254
えび
October 21, 2024
More Decks by えび
See All by えび
XcodeのLLDB(ブレークポイント)に入門する
ebibibibibi
0
66
インタプリタ言語が 実行環境の差異を 吸収する仕組みを あさーく理解する
ebibibibibi
0
93
CocoaPodsはなぜRuby製か
ebibibibibi
1
210
通勤をゆたかにする技術 ~通勤中に耳でSwiftを学んだら5kg痩せて精神が安定した話~
ebibibibibi
0
250
巨大リポジトリはパーシャルクローンしようね。
ebibibibibi
0
34
バブルソートでPHPに入門する
ebibibibibi
0
190
影響範囲調査をする技術
ebibibibibi
0
290
Featured
See All Featured
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
The Cult of Friendly URLs
andyhume
79
7k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Accessibility Awareness
sabderemane
1
250
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
750
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
A Modern Web Designer's Workflow
chriscoyier
699
190k
We Are The Robots
honzajavorek
0
400
Into the Great Unknown - MozCon
thekraken
41
2.8k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Transcript
丸め誤差 発⽣の仕組みと向き合い⽅ =千歳ゆるい勉強会vol.4=
⾃⼰紹介 • えび🦐 • エンジニア2年⽣ • Swiftが好き…最近触らせてもらえていない… 最近の関⼼ • TDD
: Swiftで⾊々いじっています。まだ腹落ちしていない。 • コンピュータはなぜ動いているのか
•以下の計算を試してください:
0.3 ?
答え: 0.30000000000000004
これが丸め誤差
今⽇は、丸め誤差発⽣の仕 組みと 向き合い⽅について お話しします。
⽬次 •浮動⼩数点数とはなにか •基数変換の⽅法をおさらい •浮動⼩数点数の優位性 •丸め誤差と付き合っていく •まとめ
Double = 浮動⼩数点数型の⼀種
浮動⼩数点数とは
浮動⼩数点数とは: 10進数の実数を、有限桁の2進数 の近似値で表現する⽅法
基数変換の⽅法をおさらい
整数部:整数を2で割り、余りを下から上へ並べる
⼩数部:⼩数部分に2を掛け、積の整数部分 を順に取り出す
0.1を⼆進数にする:
循環している…?
浮動⼩数点数: 10進数の実数を、有限桁の2進数 の近似値で表現する⽅法
丸め誤差は、⼩数を有限桁の⼆進 数で表現できない時に発⽣する
浮動⼩数点数の優位性
浮動⼩数点数の優位性: 効率的なメモリ利⽤と ⾼速な演算が可能
ベンチマークを取ってみる
None
•Double: 0.00000003327274322509766秒 •Decimal: 0.000000044722795486450195秒 確かに、 Doubleのほうが(若⼲)早い
丸め誤差と付き合う
丸め誤差を回避する⽅法: •整数で計算した後、10で割る •Decimalを使⽤する
整数で計算した後、10で割る: •10進数の整数を2進数に変換する際は常に正 確に基数変換できる •なぜなら、任意の値を2の累乗の組み合わせ として表現するだけで済むから
(補⾜) 2の累乗:
Decimalを使⽤する: •10進数で計算すれば、何も問題ないよね!!
丸め誤差を許容する: •数値の正確さが必須ではない時は、許容する
まとめ: •丸め誤差は、10進数を2進数で表現する際に 発⽣する計算誤差です。 •浮動⼩数点数は、誤差を伴うものの、⾼速で メモリ効率が良いため、広く使⽤されていま す。 •丸め誤差の回避⽅法として、整数で計算後に 10で割る⽅法や、Decimal型を使⽤する⼿段 があります。
None
②以降で話したいこと •なぜ浮動⼩数点数は速いのか •丸め誤差を許容する 内部の動き •IEEE 754標準について