+ All Categories
Home > Documents > Rubykaigi2010mrkn bigdecimal

Rubykaigi2010mrkn bigdecimal

Date post: 15-Jan-2015
Category:
Upload: kenta-murata
View: 1,384 times
Download: 0 times
Share this document with a friend
Description:
 
Popular Tags:
52
bigdecimal ライブラリとRuby の数値系の未来 The future of the bigdecimal library and the number system of Ruby mrkn, Kenta Murata (Genetic Lab Co., Ltd)
Transcript
Page 1: Rubykaigi2010mrkn bigdecimal

bigdecimal ライブラリとRuby の数値系の未来

The future of the bigdecimal library and the number system

of Rubymrkn, Kenta Murata (Genetic Lab Co., Ltd)

Page 2: Rubykaigi2010mrkn bigdecimal

村田 賢太Kenta Murata

twitter: @mrknSkype: mrkn22

Photo by takai

Page 3: Rubykaigi2010mrkn bigdecimal

Sapporo is a beautiful provincial city of Japan.

Photo by enggul

Page 5: Rubykaigi2010mrkn bigdecimal

Ruby Committer

Page 6: Rubykaigi2010mrkn bigdecimal

kosenconf -­010hokkaido

高専カンファレンス

Page 7: Rubykaigi2010mrkn bigdecimal
Page 8: Rubykaigi2010mrkn bigdecimal
Page 9: Rubykaigi2010mrkn bigdecimal

e "rst editionstill in Junkudo.

Page 10: Rubykaigi2010mrkn bigdecimal

緊急告知

Page 11: Rubykaigi2010mrkn bigdecimal

ジュンク堂Ruby会議支店で『Ruby 逆引きレシピ』を購入し、著者5名のサインをすべて集めた方、先着5名様に書籍代をキャッシュバックします!

Page 12: Rubykaigi2010mrkn bigdecimal

もう1日しか無い!急げ!

Page 13: Rubykaigi2010mrkn bigdecimal

Sapporo Kaigi 0303

メディア MIX ホール札幌市白石区菊水1条3丁目1番5号

Page 14: Rubykaigi2010mrkn bigdecimal

2010.12.04メディアMIXホールSapporo, Japan

Page 15: Rubykaigi2010mrkn bigdecimal

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

The bigdecimal library

✓BigDecimal class

✓BigMath module

16

Page 17: Rubykaigi2010mrkn bigdecimal

BigDecimal class

✓ require ‘bigdecimal’

✓多倍長浮動小数点数Multiprecision !oating point numbers

✓10n 進法10n-adic representation (modi"ed BCD)

17

Page 18: Rubykaigi2010mrkn bigdecimal

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

Problems

✓ グローバルに管理される動作モードBehavior modes maintained by global variable

✓ 精度の扱いPrecision handlings

✓ インスタンス生成Instance generation

✓ 計算速度Calculation speed

19

Page 20: Rubykaigi2010mrkn bigdecimal

Modes of BigDecimal

✓BigDecimal クラスの挙動を制御Controlling the behaviors of the system of BigDecimal class

✓例外モードException handling mode

✓丸め (端数処理) モードRounding mode

20

Page 21: Rubykaigi2010mrkn bigdecimal

Exception handling mode

✓下記について例外を発生させるかどうか

✓ In"nity

✓NaN

✓Under!ow

✓Over!ow

✓Division by zero

21

Page 22: Rubykaigi2010mrkn bigdecimal

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

BigDecimal.mode

✓モードを取得/指定するためのクラスメソッドThe class method for getting or setting modes

✓プロセス単位で管理されるMaintained per-process

✓つまりグローバル変数That is a global variable

23

Page 24: Rubykaigi2010mrkn bigdecimal

Global modes

✓ スレッド非セーフThread unsafe

✓ 異なるモードを使用する2スレッドを同時に起動できないCannot simultaneously start two threads which use different modes

✓ ファイバ非セーフFiber unsafe

24

Page 25: Rubykaigi2010mrkn bigdecimal

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

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

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

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

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

Effective digits

✓ 小数点以下第何桁まで意味があるかThe number of effective digits

✓ 例: 有効桁数3桁 (three digits are effective) 3.141592653589792...×100

↓0.314 1592653589792...×101

✓ 有効桁数は数自身が知っているべき属性

29

有効 ゴミ

Page 31: Rubykaigi2010mrkn bigdecimal

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

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

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

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

Instance generation

✓文字列から生成するGenerate from Strings

✓他から生成できない ><Cannot generate from others X(

34

Page 36: Rubykaigi2010mrkn bigdecimal

That is...

a = BigDecimal(“3.141592653589”) # OKb = BigDecimal(42) # NGc = BigDecimal(Rational(355, 113)) # NGd = BigDecimal(3.141592653589) # NG

35

Page 37: Rubykaigi2010mrkn bigdecimal

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

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

Calculation speeds

✓乗算が筆算方式Implemented only schoolbook multiplication

✓除算も筆算方式Implemented only schoolbook division

✓もっと速くなれるCan get more high speed

37

Page 40: Rubykaigi2010mrkn bigdecimal

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

Other algorithms

✓Toom-Cook method

✓Schönhage-Strassen method

✓Fürer method

✓Neuton method (for reciprocal)

39

Page 42: Rubykaigi2010mrkn bigdecimal

The Future

Page 43: Rubykaigi2010mrkn bigdecimal

Number System of Ruby

41

実数 (Float)(BigDecimal)

整数 Integer 加減乗算の答え

有理数 Rational 割り算の答え

無理数 N/A 数直線上の残りの点

複素数 Complex 代数方程式の答え

Page 44: Rubykaigi2010mrkn bigdecimal

Number System of Ruby

41

実数 (Float)(BigDecimal)

整数 Integer 加減乗算の答え

有理数 Rational 割り算の答え

無理数 N/A 数直線上の残りの点

複素数 Complex 代数方程式の答え

Page 45: Rubykaigi2010mrkn bigdecimal

Computable Real

42

✓無理数をアルゴリズムとして表現Represents irrational numbers as algorithms which generates them

✓小数表現は必要なときだけ生成Decimal representations should be generated only if needed

Page 46: Rubykaigi2010mrkn bigdecimal

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

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

Sapporo Kaigi 0303

メディア MIX ホール札幌市白石区菊水1条3丁目1番5号

Page 49: Rubykaigi2010mrkn bigdecimal

2010.12.04メディアMIXホールSapporo, Japan

Page 50: Rubykaigi2010mrkn bigdecimal
Page 51: Rubykaigi2010mrkn bigdecimal

e "rst editionstill in Junkudo.

Page 52: Rubykaigi2010mrkn bigdecimal

ジュンク堂Ruby会議支店で『Ruby 逆引きレシピ』を購入し、著者5名のサインをすべて集めた方、先着5名様に書籍代をキャッシュバックします!


Recommended