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
No Error Conversion of Floating Points
Search
K Yamaguchi
November 22, 2008
Technology
0
19
No Error Conversion of Floating Points
K Yamaguchi
November 22, 2008
Tweet
Share
More Decks by K Yamaguchi
See All by K Yamaguchi
Coding in Color
quintia
0
2
Python 実行環境の話 2024
quintia
0
19
Python 実行環境の話
quintia
0
30
裏切られる直感と、確率と精度のはなし
quintia
0
67
Python Type Annotation
quintia
0
34
Apache Spark Tutorial
quintia
0
14
Other Decks in Technology
See All in Technology
様々なファイルシステム
sat
PRO
0
260
オブザーバビリティが育むシステム理解と好奇心
maruloop
2
1.3k
CNCFの視点で捉えるPlatform Engineering - 最新動向と展望 / Platform Engineering from the CNCF Perspective
hhiroshell
0
140
AIの個性を理解し、指揮する
shoota
1
130
20251029_Cursor Meetup Tokyo #02_MK_「あなたのAI、私のシェル」 - プロンプトインジェクションによるエージェントのハイジャック
mk0721
PRO
0
370
RemoteFunctionを使ったコロケーション
mkazutaka
1
120
Azure Well-Architected Framework入門
tomokusaba
1
130
Dify on AWS 環境構築手順
yosse95ai
0
140
AWS DMS で SQL Server を移行してみた/aws-dms-sql-server-migration
emiki
0
250
Okta Identity Governanceで実現する最小権限の原則 / Implementing the Principle of Least Privilege with Okta Identity Governance
tatsumin39
0
180
What's new in OpenShift 4.20
redhatlivestreaming
0
290
[re:Inent2025事前勉強会(有志で開催)] re:Inventで見つけた人生をちょっと変えるコツ
sh_fk2
1
460
Featured
See All Featured
Speed Design
sergeychernyshev
32
1.2k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Building Applications with DynamoDB
mza
96
6.7k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Optimizing for Happiness
mojombo
379
70k
Gamification - CAS2011
davidbonilla
81
5.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Side Projects
sachag
455
43k
Facilitating Awesome Meetings
lara
57
6.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
A designer walks into a library…
pauljervisheath
209
24k
Transcript
浮動⼩数点演算の無誤差変換 と ⾼精度計算 のおはなし quintia 2008/11/22
今回のお題 このつぶやきで決めました
元ネタ p数学セミナー 2008年11⽉号 特集記事 (⽇本評論社) pD.E.Knuth: The Art of Computer
Programming, Vol.2, 1969.
浮動⼩数点演算について p浮動⼩数点表現の限界 pどういう意味なのか? pKnuth先⽣のお⾔葉
浮動⼩数点表現の限界 p (1+260) - 260 正解は 1 p浮動⼩数点(単精度)で演算すると…… fl( 1
+260 ) = 260 fl( 260-260 ) = 0 p 0 になってしまう! fl( ) は 「実数での演算じゃないよ 浮動⼩数点での演算だよ」 という印
どういう意味なのか? p浮動⼩数点演算では「結合法則」が 成⽴しない場合がある、ということ fl( (a + b) + c )
≠ fl( a + (b + c) ) p fl( ) の中⾝は 「括弧を外して書いてはいけない」 のだ!
Knuth先⽣のお⾔葉 p「a1 +a2 +a3 」(略)などの数学的記法は, もともと結合法則が成り⽴つことを前 提として作ってある.プログラマは, 結合法則が成り⽴つ暗黙の前提で考え ないように,特に注意しなければなら ない.
The Art of Computer Programming 2 第3版 ⽇本語版 p217
無誤差変換 p無誤差変換の概念 p浮動⼩数点演算の誤差を計算する p誤差計算式の妙 pKnuth先⽣のお⾔葉 ふたたび
無誤差変換の概念 p無誤差変換というよりは、実数演算の解と、 浮動⼩数点演算の解との誤差という感覚 a + b = fl( a +
b ) + x p x の部分が計算によって⽣じる誤差 pしかし、これを計算できるのか……?
浮動⼩数点演算の誤差を計算する p計算できる! (D.E.Knuth, 1969) fl( (a - ((a + b)
- (a - b))) + (b - (a - b)) ) p乗算については T.J.Dekker, (1971) によって ⽰された式がある
誤差計算式の妙(1) fl( (a - ((a + b) - (a -
b))) + (b - (a - b)) ) p実数計算ならば括弧をはらえるので ちょっと試してみる a-a-b+a-b+b-a+b = 0 p実数計算では必ず0になる式!
誤差計算式の妙(2) fl( (a - ((a + b) - (a -
b))) + (b - (a - b)) ) 結合法則が成⽴ 値が0 誤差が⽣じなかった 結合法則が成⽴しない 値が0以外 誤差が⽣じた
Knuth先⽣のお⾔葉 ふたたび p「a1 +a2 +a3 」(略)などの数学的記法は, もともと結合法則が成り⽴つことを前 提として作ってある.プログラマは, 結合法則が成り⽴つ暗黙の前提で考え ないように,特に注意しなければなら
ない. The Art of Computer Programming 2 第3版 ⽇本語版 p217
⾼精度計算の例を簡単に (1+260) - 260 1+260 解 260 誤差 1 260
- 260 解 0 誤差 0 解が0 誤差の総和が1 p概念としては、解が1ということ
実際にコードを書いてみた pJavaで書いてみた pJavaの⼩数計算 pDelphiで書いてみた
Javaで書いてみた public static void main(String[] args) { float a =
1; float b = (float)Math.pow(2, 127); System.out.println("b: "+b); float u = a + b; float c = a - b; float v = (a-(u-c)) + (b-c); System.out.println("u: "+u); System.out.println("v: "+v); } 結果 b: 1.7014118E38 u: 1.7014118E38 v: NaN NaNが出てきた!
Javaの⼩数計算 pFloat や Double に以下の様な定義がある p POSITIVE_INFINITY (正の無限⼤ ) p
NEGATIVE_INIFINITY(負の無限⼤) p NaN(⾮数) p+0.0 と -0.0 が区別されている p1.0 / +0.0 > POSITIVE_INFINITY p1.0 / -0.0 > NEGATIVE_INFINITY p0.0 / 0.0 > NaN
Delphiで書いてみた p計画通り!! var a,b,c,u,v: Single; begin a:=1.0; b:=Power(2.0, 60.0); u:=a+b;
c:=a-b; v:=(a-(u-c))+(b-c); ShowMessage(Format('%f , %f', [u, v])); end; 結果: 1.15292150460684698E18 , 1.00 誤差 1 近似解 260
ありがとうございました