rubykaigi2010mrkn bigdecimal

Post on 15-Jan-2015

1.384 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

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

The future of the bigdecimal library and the number system

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

村田 賢太Kenta Murata

twitter: @mrknSkype: mrkn22

Photo by takai

Sapporo is a beautiful provincial city of Japan.

Photo by enggul

Ruby Committer

kosenconf -­010hokkaido

高専カンファレンス

e "rst editionstill in Junkudo.

緊急告知

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

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

Sapporo Kaigi 0303

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

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

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

The future of the bigdecimal library and the number system

of Ruby

mrkn, Kenta Murata (Genetic Lab Co., Ltd)

The bigdecimal library

✓BigDecimal class

✓BigMath module

16

BigDecimal class

✓ require ‘bigdecimal’

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

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

17

BigMath module

✓BigDecimal 用の Math モジュールThe Math module for BigDecimals

✓For examples:

✓Math::PI → BigMath.PI(n)

✓Math.cos(x) → BigMath.cos(x, n)

18

Problems

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

✓ 精度の扱いPrecision handlings

✓ インスタンス生成Instance generation

✓ 計算速度Calculation speed

19

Modes of BigDecimal

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

✓例外モードException handling mode

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

20

Exception handling mode

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

✓ In"nity

✓NaN

✓Under!ow

✓Over!ow

✓Division by zero

21

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

BigDecimal.mode

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

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

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

23

Global modes

✓ スレッド非セーフThread unsafe

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

✓ ファイバ非セーフFiber unsafe

24

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

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(

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

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

Mode conserving block

✓一時的なモード変更を容易にIt makes temporary-mode-change easy

✓BigDecimal.save_exception_mode { ... }

✓BigDecimal.save_rounding_mode { ... }

✓ Introduced at r29127 (yesterday)

✓スレッドセーフになったから導入できた

28

Effective digits

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

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

↓0.314 1592653589792...×101

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

29

有効 ゴミ

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

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

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

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 桁

Instance generation

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

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

34

That is...

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

35

That is...

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

e = BigDecimal(a) # NG!!

35

Float is difficult

✓ Float::RADIX != 10

✓基数が異なるため、きれいに変換できないCannot convert exactly due to different radix

✓桁数を明示させるExplicitly specifying the number of effective digits

36

Calculation speeds

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

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

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

37

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

Other algorithms

✓Toom-Cook method

✓Schönhage-Strassen method

✓Fürer method

✓Neuton method (for reciprocal)

39

The Future

Number System of Ruby

41

実数 (Float)(BigDecimal)

整数 Integer 加減乗算の答え

有理数 Rational 割り算の答え

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

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

Number System of Ruby

41

実数 (Float)(BigDecimal)

整数 Integer 加減乗算の答え

有理数 Rational 割り算の答え

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

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

Computable Real

42

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

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

e.g.) e–iπ == –1

✓Present:CMath.exp(–Math::PI.i)#=> (–1.0–1.2246467991473532e–16i)

✓ Ideal:Math.exp(–Math::PI.i)#=> –1

43

Summary

✓BigDecimal は問題児BigDecimal has some problems

✓いくつかは最近修正されたSome of these has been "xed recently

✓計算可能実数クラスが必要We need a class for computable real

44

Sapporo Kaigi 0303

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

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

e "rst editionstill in Junkudo.

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

top related