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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
K Yamaguchi
November 22, 2008
Technology
0
20
No Error Conversion of Floating Points
K Yamaguchi
November 22, 2008
Tweet
Share
Other Decks in Technology
See All in Technology
【Oracle Cloud ウェビナー】[Oracle AI Database + AWS] Oracle Database@AWSで広がるクラウドの新たな選択肢とAI時代のデータ戦略
oracle4engineer
PRO
2
170
生成AIを活用した音声文字起こしシステムの2つの構築パターンについて
miu_crescent
PRO
2
210
10Xにおける品質保証活動の全体像と改善 #no_more_wait_for_test
nihonbuson
PRO
2
310
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
13k
Bill One急成長の舞台裏 開発組織が直面した失敗と教訓
sansantech
PRO
2
380
コスト削減から「セキュリティと利便性」を担うプラットフォームへ
sansantech
PRO
3
1.5k
コミュニティが変えるキャリアの地平線:コロナ禍新卒入社のエンジニアがAWSコミュニティで見つけた成長の羅針盤
kentosuzuki
0
120
AzureでのIaC - Bicep? Terraform? それ早く言ってよ会議
torumakabe
1
570
小さく始めるBCP ― 多プロダクト環境で始める最初の一歩
kekke_n
1
440
Tebiki Engineering Team Deck
tebiki
0
24k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
【Ubie】AIを活用した広告アセット「爆速」生成事例 | AI_Ops_Community_Vol.2
yoshiki_0316
1
110
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
300
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
170
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
Un-Boring Meetings
codingconduct
0
200
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
62
Building the Perfect Custom Keyboard
takai
2
690
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
440
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
54
From π to Pie charts
rasagy
0
120
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
260
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
ありがとうございました