- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (0)
- 1_9_3_125 (0)
- 1_9_3_392 (0)
- 2_1_10 (38)
- 2_2_9 (0)
- 2_4_6 (5)
- 2_5_5 (-5)
- 2_6_3 (0)
- What's this?
BigDecimal provides arbitrary-precision floating point decimal arithmetic.
Introduction
Ruby provides built-in support for arbitrary precision integer arithmetic.
For example:
42**13 #=> 1265437718438866624512
BigDecimal provides similar support for very large or very accurate floating point numbers.
Decimal arithmetic is also useful for general calculation, because it provides the correct answers people expect–whereas normal binary floating point arithmetic often introduces subtle errors because of the conversion between base 10 and base 2.
For example, try:
sum = 0 10_000.times do sum = sum + 0.0001 end print sum #=> 0.9999999999999062
and contrast with the output from:
require 'bigdecimal' sum = BigDecimal.new("0") 10_000.times do sum = sum + BigDecimal.new("0.0001") end print sum #=> 0.1E1
Similarly:
(BigDecimal.new("1.2") - BigDecimal("1.0")) == BigDecimal("0.2") #=> true (1.2 - 1.0) == 0.2 #=> false
Special features of accurate decimal arithmetic
Because BigDecimal is more accurate than normal binary floating point arithmetic, it requires some special values.
Infinity
BigDecimal sometimes needs to return infinity, for example if you divide a value by zero.
BigDecimal.new("1.0") / BigDecimal.new("0.0") #=> Infinity BigDecimal.new("-1.0") / BigDecimal.new("0.0") #=> -Infinity
You can represent infinite numbers to BigDecimal using the strings ‘Infinity’, ‘+Infinity’ and ‘-Infinity’ (case-sensitive)
Not a Number
When a computation results in an undefined value, the special value NaN (for ‘not a number’) is returned.
Example:
BigDecimal.new("0.0") / BigDecimal.new("0.0") #=> NaN
You can also create undefined values.
NaN is never considered to be the same as any other value, even NaN itself:
n = BigDecimal.new('NaN') n == 0.0 #=> false n == n #=> false
Positive and negative zero
If a computation results in a value which is too small to be represented as a BigDecimal within the currently specified limits of precision, zero must be returned.
If the value which is too small to be represented is negative, a BigDecimal value of negative zero is returned.
BigDecimal.new("1.0") / BigDecimal.new("-Infinity") #=> -0.0
If the value is positive, a value of positive zero is returned.
BigDecimal.new("1.0") / BigDecimal.new("Infinity") #=> 0.0
(See BigDecimal.mode for how to specify limits of precision.)
Note that -0.0 and 0.0 are considered to be the same for the purposes of comparison.
Note also that in mathematics, there is no particular concept of negative or positive zero; true mathematical zero has no sign.
bigdecimal/util
When you require bigdecimal/util, the #to_d method will be available on BigDecimal and the native Integer, Float, Rational, and String classes:
require 'bigdecimal/util' 42.to_d # => 0.42e2 0.5.to_d # => 0.5e0 (2/3r).to_d(3) # => 0.667e0 "0.5".to_d # => 0.5e0
License
Copyright (C) 2002 by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>.
BigDecimal is released under the Ruby and 2-clause BSD licenses. See LICENSE.txt for details.
Maintained by mrkn <mrkn@mrkn.jp> and ruby-core members.
Documented by zzak <zachary@zacharyscott.net>, mathew <meta@pobox.com>, and many other contributors.
Constants
NAN = BigDecimal_global_new(1, &arg, rb_cBigDecimal)
INFINITY = BigDecimal_global_new(1, &arg, rb_cBigDecimal)
SIGN_NEGATIVE_INFINITE = -3
SIGN_POSITIVE_INFINITE = 3
SIGN_NEGATIVE_FINITE = -2
SIGN_POSITIVE_FINITE = 2
SIGN_NEGATIVE_ZERO = -1
SIGN_POSITIVE_ZERO = 1
SIGN_NaN = 0
ROUND_HALF_EVEN = 7
ROUND_FLOOR = 6
ROUND_CEILING = 5
ROUND_HALF_DOWN = 4
ROUND_HALF_UP = 3
ROUND_DOWN = 2
ROUND_UP = 1
ROUND_MODE = 0x100
EXCEPTION_ZERODIVIDE = 0x10
EXCEPTION_OVERFLOW = 0x01
EXCEPTION_UNDERFLOW = 0x04
EXCEPTION_INFINITY = 0x01
EXCEPTION_NaN = 0x02
EXCEPTION_ALL = 0xff
BASE = INT2FIX((SIGNED_VALUE)VpBaseVal())