Module: Kernel

Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#BigDecimal(*args) ⇒ Object

BigDecimal provides arbitrary-precision floating point decimal arithmetic.

Copyright © 2002 by Shigeo Kobayashi <[email protected]>. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the README file of the BigDecimal distribution.

Documented by mathew <[email protected]>.

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
for i in (1..10000)
  sum = sum + 0.0001
end
print sum

and contrast with the output from:

require 'bigdecimal'

sum = BigDecimal.new("0")
for i in (1..10000)
  sum = sum + BigDecimal.new("0.0001")
end
print sum

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 -> nil

n == n -> nil

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. If the value is positive, a value of positive zero is returned.

BigDecimal.new(“1.0”) / BigDecimal.new(“-Infinity”) -> -0.0

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.



2302
2303
2304
2305
2306
# File 'ext/rubysl/bigdecimal/bigdecimal.c', line 2302

static VALUE
BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
{
    return BigDecimal_new(argc, argv, rb_cBigDecimal);
}