Rubykaigi2010mrkn bigdecimal

1,799 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,799
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rubykaigi2010mrkn bigdecimal

  1. 1. bigdecimal Ruby The future of the bigdecimal library and the number system of Ruby mrkn, Kenta Murata (Genetic Lab Co., Ltd)
  2. 2. twitter: Skype:
  3. 3. Sapporo is a beautiful provincial city of Japan. Photo by enggul
  4. 4. from Sapporo, with Love for Ruby Photo by darashi
  5. 5. kosenconf -­010hokkaido
  6. 6. e rst edition still in Junkudo.
  7. 7. Sapporo Kaigi 03 03 MIX 1 3 1 5
  8. 8. bigdecimal Ruby The future of the bigdecimal library and the number system of Ruby mrkn, Kenta Murata (Genetic Lab Co., Ltd)
  9. 9. The bigdecimal library ✓ BigDecimal class ✓ BigMath module
  10. 10. BigDecimal class ✓ require ‘bigdecimal’ ✓ Multiprecision oating point numbers ✓ 10n 10n-adic representation (modi ed BCD)
  11. 11. BigMath module ✓ BigDecimal Math The Math module for BigDecimals ✓ For examples: ✓ Math::PI → BigMath.PI(n) ✓ Math.cos(x) → BigMath.cos(x, n)
  12. 12. Problems ✓ Behavior modes maintained by global variable ✓ Precision handlings ✓ Instance generation ✓ Calculation speed
  13. 13. Modes of BigDecimal ✓ BigDecimal Controlling the behaviors of the system of BigDecimal class ✓ Exception handling mode ✓ ( ) Rounding mode
  14. 14. Exception handling mode ✓ ✓ In nity ✓ NaN ✓ Under ow ✓ Over ow ✓ Division by zero
  15. 15. 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
  16. 16. BigDecimal.mode ✓ / The class method for getting or setting modes ✓ Maintained per-process ✓ That is a global variable
  17. 17. Global modes ✓ Thread unsafe ✓ 2 Cannot simultaneously start two threads which use different modes ✓ Fiber unsafe
  18. 18. 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
  19. 19. 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 It is inde nite X(
  20. 20. Fiber unsafe BD = BigDecimal fa = 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
  21. 21. Modes in thread-local storages ✓ Introduced at r29099 (3 days ago) ✓ Modes are maintained per-thread ✓ Threads are initialized with default modes ✓ Fiber safe
  22. 22. Mode conserving block ✓ It makes temporary-mode-change easy ✓ BigDecimal.save_exception_mode { ... } ✓ BigDecimal.save_rounding_mode { ... } ✓ Introduced at r29127 (yesterday) ✓
  23. 23. Effective digits ✓ The number of effective digits ✓ : 3 (three digits are effective) 3.141592653589792...×100 ↓ 0.314 1592653589792...×101 ✓
  24. 24. BigDecimal#precs ✓ prec[1] The allocated length of the digit array ✓ prec[2] The used length of the digit array ✓ Not the number of effective digits ✓ Do you use them?
  25. 25. 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.
  26. 26. 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!
  27. 27. Code examples ### (1) ### BigDecimal("3602879701896397.1") / 36028797018963968 #=> #<BigDecimal:10086d8e0,'0.1000000000 0000000555 1115123125 782702E0',36(54)> Float::DIG ### (2) ### BigDecimal("3602879701896397.1") / 36028797018963968.0 #=> 1.0 ↑
  28. 28. Instance generation ✓ Generate from Strings ✓ Cannot generate from others X(
  29. 29. That is... a = BigDecimal(“3.141592653589”) # OK b = BigDecimal(42) # NG c = BigDecimal(Rational(355, 113)) # NG d = BigDecimal(3.141592653589) # NG
  30. 30. That is... a = BigDecimal(“3.141592653589”) # OK b = BigDecimal(42) # NG c = BigDecimal(Rational(355, 113)) # NG d = BigDecimal(3.141592653589) # NG e = BigDecimal(a) # NG!!
  31. 31. Float is difficult ✓ Float::RADIX != 10 ✓ Cannot convert exactly due to different radix ✓ Explicitly specifying the number of effective digits
  32. 32. Calculation speeds ✓ Implemented only schoolbook multiplication ✓ Implemented only schoolbook division ✓ Can get more high speed
  33. 33. 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
  34. 34. Other algorithms ✓ Toom-Cook method ✓ Schönhage-Strassen method ✓ Fürer method ✓ Neuton method (for reciprocal)
  35. 35. Number System of Ruby (Float) (BigDecimal) Integer Rational N/A Complex
  36. 36. Number System of Ruby (Float) (BigDecimal) Integer Rational N/A Complex
  37. 37. Computable Real ✓ Represents irrational numbers as algorithms which generates them ✓ Decimal representations should be generated only if needed
  38. 38. e.g.) e–iπ == –1 ✓ Present: CMath.exp(–Math::PI.i) #=> (–1.0–1.2246467991473532e–16i) ✓ Ideal: Math.exp(–Math::PI.i) #=> –1
  39. 39. Summary ✓ BigDecimal BigDecimal has some problems ✓ Some of these has been xed recently ✓ We need a class for computable real
  40. 40. Sapporo Kaigi 03 03 MIX 1 3 1 5
  41. 41. e rst edition still in Junkudo.

×