Download - Rubykaigi2010mrkn bigdecimal
![Page 1: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/1.jpg)
bigdecimal ライブラリとRuby の数値系の未来
The future of the bigdecimal library and the number system
of Rubymrkn, Kenta Murata (Genetic Lab Co., Ltd)
![Page 2: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/2.jpg)
村田 賢太Kenta Murata
twitter: @mrknSkype: mrkn22
Photo by takai
![Page 3: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/3.jpg)
Sapporo is a beautiful provincial city of Japan.
Photo by enggul
![Page 4: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/4.jpg)
from Sapporo,with Love for Ruby
Photo by darashi
![Page 5: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/5.jpg)
Ruby Committer
![Page 6: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/6.jpg)
kosenconf -010hokkaido
高専カンファレンス
![Page 7: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/7.jpg)
![Page 8: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/8.jpg)
![Page 9: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/9.jpg)
e "rst editionstill in Junkudo.
![Page 10: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/10.jpg)
緊急告知
![Page 11: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/11.jpg)
ジュンク堂Ruby会議支店で『Ruby 逆引きレシピ』を購入し、著者5名のサインをすべて集めた方、先着5名様に書籍代をキャッシュバックします!
![Page 12: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/12.jpg)
もう1日しか無い!急げ!
![Page 13: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/13.jpg)
Sapporo Kaigi 0303
メディア MIX ホール札幌市白石区菊水1条3丁目1番5号
![Page 14: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/14.jpg)
2010.12.04メディアMIXホールSapporo, Japan
![Page 15: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/15.jpg)
bigdecimal ライブラリとRuby の数値系の未来
The future of the bigdecimal library and the number system
of Ruby
mrkn, Kenta Murata (Genetic Lab Co., Ltd)
![Page 16: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/16.jpg)
The bigdecimal library
✓BigDecimal class
✓BigMath module
16
![Page 17: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/17.jpg)
BigDecimal class
✓ require ‘bigdecimal’
✓多倍長浮動小数点数Multiprecision !oating point numbers
✓10n 進法10n-adic representation (modi"ed BCD)
17
![Page 18: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/18.jpg)
BigMath module
✓BigDecimal 用の Math モジュールThe Math module for BigDecimals
✓For examples:
✓Math::PI → BigMath.PI(n)
✓Math.cos(x) → BigMath.cos(x, n)
18
![Page 19: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/19.jpg)
Problems
✓ グローバルに管理される動作モードBehavior modes maintained by global variable
✓ 精度の扱いPrecision handlings
✓ インスタンス生成Instance generation
✓ 計算速度Calculation speed
19
![Page 20: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/20.jpg)
Modes of BigDecimal
✓BigDecimal クラスの挙動を制御Controlling the behaviors of the system of BigDecimal class
✓例外モードException handling mode
✓丸め (端数処理) モードRounding mode
20
![Page 21: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/21.jpg)
Exception handling mode
✓下記について例外を発生させるかどうか
✓ In"nity
✓NaN
✓Under!ow
✓Over!ow
✓Division by zero
21
![Page 22: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/22.jpg)
Rounding modes
✓ 切り上げ Round up
✓ 切り捨て Round down (toward zero) ←IEEE754
✓ 四捨五入 Round half up
✓ 五捨六入 Round half down
✓ 偶数丸め Banker’s rounding ←IEEE754
✓ 床 Floor (toward +∞) ←IEEE754
✓ 天井 Ceiling (toward –∞) ←IEEE754
22
![Page 23: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/23.jpg)
BigDecimal.mode
✓モードを取得/指定するためのクラスメソッドThe class method for getting or setting modes
✓プロセス単位で管理されるMaintained per-process
✓つまりグローバル変数That is a global variable
23
![Page 24: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/24.jpg)
Global modes
✓ スレッド非セーフThread unsafe
✓ 異なるモードを使用する2スレッドを同時に起動できないCannot simultaneously start two threads which use different modes
✓ ファイバ非セーフFiber unsafe
24
![Page 25: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/25.jpg)
Which BD.mode(BD::EXCEPTION_NAN) is?
BD = BigDecimal
th = Thread.start { BD.mode(BD::EXCEPTION_NAN, true) ...(A)...}
BD.mode(BD::EXCEPTION_NAN, false)...(B)...th.join
25
![Page 26: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/26.jpg)
Which BD.mode(BD::EXCEPTION_NAN) is?
BD = BigDecimal
th = Thread.start { BD.mode(BD::EXCEPTION_NAN, true) ...(A)...}
BD.mode(BD::EXCEPTION_NAN, false)...(B)...th.join
25
定まらない ><It is inde"nite X(
![Page 27: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/27.jpg)
Fiber unsafe
BD = BigDecimalfa = Fiber.new { BD.mode(BD::ROUND_MODE, BD::ROUND_UP)}Fiber.new { BD.mode(BD::ROUND_MODE, BD::ROUND_DOWN) fa.resume BD.mode(BD::ROUND_MODE) #=> BD::ROUND_UP}.resume
26
![Page 28: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/28.jpg)
Modes in thread-local storages
✓ Introduced at r29099 (3 days ago)
✓スレッド毎に独立したモードを持つModes are maintained per-thread
✓生成直後のスレッドはデフォルトモードThreads are initialized with default modes
✓ファイバセーフFiber safe
27
![Page 29: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/29.jpg)
Mode conserving block
✓一時的なモード変更を容易にIt makes temporary-mode-change easy
✓BigDecimal.save_exception_mode { ... }
✓BigDecimal.save_rounding_mode { ... }
✓ Introduced at r29127 (yesterday)
✓スレッドセーフになったから導入できた
28
![Page 30: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/30.jpg)
Effective digits
✓ 小数点以下第何桁まで意味があるかThe number of effective digits
✓ 例: 有効桁数3桁 (three digits are effective) 3.141592653589792...×100
↓0.314 1592653589792...×101
✓ 有効桁数は数自身が知っているべき属性
29
有効 ゴミ
![Page 31: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/31.jpg)
BigDecimal#precs
✓ prec[1] はメモリ確保済み桁数The allocated length of the digit array
✓ prec[2] は使用済み桁数The used length of the digit array
✓ 有効桁数じゃないwwwNot the number of effective digits
✓ 使う?Do you use them?
30
![Page 32: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/32.jpg)
BigDecimals don’t know their own effective digits
✓ (複数の) 有効桁数を数と別に管理する必要があるWe must maintain (multiple) the number of effective digits
✓ 実は Float も自分の有効桁数を知らないAs well as Floats
✓ 超不便。勝手にやって欲しい。It is too much convenient.Should be maintained automatically.
31
![Page 33: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/33.jpg)
Collaborate with Floats
✓強制的に Float へ変換されるForce converted into Floats
✓桁数が Float::DIG に強制されるThe number of digits is forced to Float::DIG
✓危険、混ぜるな!It is dangerous, don’t mix them!
32
![Page 34: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/34.jpg)
Code examples
### (1) ###BigDecimal("3602879701896397.1") / 36028797018963968#=> #<BigDecimal:10086d8e0,'0.1000000000 0000000555 1115123125 782702E0',36(54)>
### (2) ###BigDecimal("3602879701896397.1") / 36028797018963968.0#=> 1.0
33
Float::DIG 桁
↑
![Page 35: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/35.jpg)
Instance generation
✓文字列から生成するGenerate from Strings
✓他から生成できない ><Cannot generate from others X(
34
![Page 36: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/36.jpg)
That is...
a = BigDecimal(“3.141592653589”) # OKb = BigDecimal(42) # NGc = BigDecimal(Rational(355, 113)) # NGd = BigDecimal(3.141592653589) # NG
35
![Page 37: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/37.jpg)
That is...
a = BigDecimal(“3.141592653589”) # OKb = BigDecimal(42) # NGc = BigDecimal(Rational(355, 113)) # NGd = BigDecimal(3.141592653589) # NG
e = BigDecimal(a) # NG!!
35
![Page 38: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/38.jpg)
Float is difficult
✓ Float::RADIX != 10
✓基数が異なるため、きれいに変換できないCannot convert exactly due to different radix
✓桁数を明示させるExplicitly specifying the number of effective digits
36
![Page 39: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/39.jpg)
Calculation speeds
✓乗算が筆算方式Implemented only schoolbook multiplication
✓除算も筆算方式Implemented only schoolbook division
✓もっと速くなれるCan get more high speed
37
![Page 40: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/40.jpg)
e.g.) Karatsuba method
a = a0 × 10n + a1
b = b0 × 10n + b1
c = ab
= (a0 × 10n + a1)(b0 × 10n + b1)
= a0b0 × 102n + (a0b1 + a1b0) × 10n + a1b1
= a0b0 × 102n + [a0b0 + (a0 – a1)(b1 – b0) + a1b1] × 10n + a1b1
38
![Page 41: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/41.jpg)
Other algorithms
✓Toom-Cook method
✓Schönhage-Strassen method
✓Fürer method
✓Neuton method (for reciprocal)
39
![Page 42: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/42.jpg)
The Future
![Page 43: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/43.jpg)
Number System of Ruby
41
実数 (Float)(BigDecimal)
整数 Integer 加減乗算の答え
有理数 Rational 割り算の答え
無理数 N/A 数直線上の残りの点
複素数 Complex 代数方程式の答え
![Page 44: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/44.jpg)
Number System of Ruby
41
実数 (Float)(BigDecimal)
整数 Integer 加減乗算の答え
有理数 Rational 割り算の答え
無理数 N/A 数直線上の残りの点
複素数 Complex 代数方程式の答え
![Page 45: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/45.jpg)
Computable Real
42
✓無理数をアルゴリズムとして表現Represents irrational numbers as algorithms which generates them
✓小数表現は必要なときだけ生成Decimal representations should be generated only if needed
![Page 46: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/46.jpg)
e.g.) e–iπ == –1
✓Present:CMath.exp(–Math::PI.i)#=> (–1.0–1.2246467991473532e–16i)
✓ Ideal:Math.exp(–Math::PI.i)#=> –1
43
![Page 47: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/47.jpg)
Summary
✓BigDecimal は問題児BigDecimal has some problems
✓いくつかは最近修正されたSome of these has been "xed recently
✓計算可能実数クラスが必要We need a class for computable real
44
![Page 48: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/48.jpg)
Sapporo Kaigi 0303
メディア MIX ホール札幌市白石区菊水1条3丁目1番5号
![Page 49: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/49.jpg)
2010.12.04メディアMIXホールSapporo, Japan
![Page 50: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/50.jpg)
![Page 51: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/51.jpg)
e "rst editionstill in Junkudo.
![Page 52: Rubykaigi2010mrkn bigdecimal](https://reader033.vdocument.in/reader033/viewer/2022052618/54b7794a4a7959e6038b461d/html5/thumbnails/52.jpg)
ジュンク堂Ruby会議支店で『Ruby 逆引きレシピ』を購入し、著者5名のサインをすべて集めた方、先着5名様に書籍代をキャッシュバックします!