Type:
Class
Constants:
BASE : INT2FIX((SIGNED_VALUE)VpBaseVal())

Base value used in internal calculations. On a 32 bit system, BASE is 10000, indicating that calculation is done in groups of 4 digits. (If it were larger, BASE**2 wouldn't fit in 32 bits, so you couldn't guarantee that two groups could always be multiplied together without overflow.)

EXCEPTION_ALL : 0xff

Determines whether overflow, underflow or zero divide result in an exception being thrown. See ::mode.

EXCEPTION_NaN : 0x02

Determines what happens when the result of a computation is not a number (NaN). See ::mode.

EXCEPTION_INFINITY : 0x01

Determines what happens when the result of a computation is infinity. See ::mode.

EXCEPTION_UNDERFLOW : 0x04

Determines what happens when the result of a computation is an underflow (a result too small to be represented). See ::mode.

EXCEPTION_OVERFLOW : 0x01

Determines what happens when the result of a computation is an overflow (a result too large to be represented). See ::mode.

EXCEPTION_ZERODIVIDE : 0x01

Determines what happens when a division by zero is performed. See ::mode.

ROUND_MODE : 0x100

Determines what happens when a result must be rounded in order to fit in the appropriate number of significant digits. See ::mode.

ROUND_UP : 1

Indicates that values should be rounded away from zero. See ::mode.

ROUND_DOWN : 2

Indicates that values should be rounded towards zero. See ::mode.

ROUND_HALF_UP : 3

Indicates that digits >= 5 should be rounded up, others rounded down. See ::mode.

ROUND_HALF_DOWN : 4

Indicates that digits >= 6 should be rounded up, others rounded down. See ::mode.

ROUND_CEILING : 5

Round towards +infinity. See ::mode.

ROUND_FLOOR : 6

Round towards -infinity. See ::mode.

ROUND_HALF_EVEN : 7

Round towards the even neighbor. See ::mode.

SIGN_NaN : 0

Indicates that a value is not a number. See #sign.

SIGN_POSITIVE_ZERO : 1

Indicates that a value is +0. See #sign.

SIGN_NEGATIVE_ZERO : -1

Indicates that a value is -0. See #sign.

SIGN_POSITIVE_FINITE : 2

Indicates that a value is positive and finite. See #sign.

SIGN_NEGATIVE_FINITE : -2

Indicates that a value is negative and finite. See #sign.

SIGN_POSITIVE_INFINITE : 3

Indicates that a value is positive and infinite. See #sign.

SIGN_NEGATIVE_INFINITE : -3

Indicates that a value is negative and infinite. See #sign.

INFINITY : BigDecimal_global_new(1, &arg, rb_cBigDecimal)

Positive infinity value.

NAN : BigDecimal_global_new(1, &arg, rb_cBigDecimal)

'Not a Number' value.

BigDecimal provides arbitrary-precision floating point decimal arithmetic.

Copyright (C) 2002 by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>.

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 <meta@pobox.com>.

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 #=> 0.9999999999999062

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 #=> 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 #=> 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.

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 ::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.

to_r

to_r() Instance Public methods Converts a

2015-03-31 09:04:25
to_digits

a.to_digits â string Instance Public methods Converts a

2015-03-31 08:41:14
save_limit

BigDecimal.save_limit { ... } Class Public methods Excecute the provided block

2015-03-31 05:44:53
frac

frac() Instance Public methods Return the fractional part of the number.

2015-03-31 07:37:02
truncate

truncate(n) Instance Public methods Truncate to the nearest 1, returning the

2015-03-31 09:15:43
quo

quo(value) Instance Public methods Divide by the specified value.

2015-03-31 08:11:26
modulo

a.modulo(b) Instance Public methods Returns the modulus from dividing by b.

2015-03-31 07:56:08
-

sub(value, digits) Instance Public methods Subtract the specified value.

2015-03-31 06:20:18
*

mult(value, digits) Instance Public methods Multiply by the specified value

2015-03-31 06:02:22
as_json

as_json(*) Instance Public methods

2015-03-31 06:54:10