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
えび
October 21, 2024
160
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
64
インタプリタ言語が 実行環境の差異を 吸収する仕組みを あさーく理解する
ebibibibibi
0
88
CocoaPodsはなぜRuby製か
ebibibibibi
1
210
通勤をゆたかにする技術 ~通勤中に耳でSwiftを学んだら5kg痩せて精神が安定した話~
ebibibibibi
0
240
巨大リポジトリはパーシャルクローンしようね。
ebibibibibi
0
33
バブルソートでPHPに入門する
ebibibibibi
0
190
影響範囲調査をする技術
ebibibibibi
0
220
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
67k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Music & Morning Musume
bryan
48
7.4k
Optimising Largest Contentful Paint
csswizardry
37
4k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
400
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
760
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Leo the Paperboy
mayatellez
10
2.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.9k
sira's awesome portfolio website redesign presentation
elsirapls
0
420
Navigating Weather and Climate Data
rabernat
0
520
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
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標準について