Usage and methods
Suppose you already know Ruby programming,
to create BigDecimal objects,the program would like:
require 'bigdecimal'
a=BigDecimal::new("0.123456789123456789")
b=BigDecimal("123456.78912345678",40)
c=a+b
List of methods
In 32 bits integer system,every 4 digits(in decimal) are computed simultaneously.
This means the number of significant digits in BigDecimal is always a multiple of 4.
Some more methods are available in Ruby code (not C code).
Functions such as sin,cos ...,are in math.rb in bigdecimal directory.
To use them,require math.rb as:
require "bigdecimal/math.rb"
For details,see the math.rb code and comments.
Other utility methods are in util.rb.
To use util.rb, require it as:
require "bigdecimal/util.rb"
For details,see the util.rb code.
Class methods
Instance methods
- +
addition(c = a + b)
For the resulting number of significant digits of c,see Resulting number of significant digits.
- -
subtraction (c = a - b) or negation (c = -a)
For the resulting number of significant digits of c,see Resulting number of significant digits.
- *
multiplication(c = a * b)
For the resulting number of significant digits of c,see Resulting number of significant digits.
- /
division(c = a / b)
For the resulting number of significant digits of c,see Resulting number of significant digits.
- add(b,n)
c = a.add(b,n)
c = a.add(b,n) performs c = a + b.
If n is less than the actual significant digits of a + b,
then c is rounded properly according to the BigDecimal.limit.
If n is zero,then the result is the same as +'s.
- sub(b,n)
c = a.sub(b,n)
c = a.sub(b,n) performs c = a - b.
If n is less than the actual significant digits of a - b,
then c is rounded properly according to the BigDecimal.limit.
If n is zero,then the result is the same as -'s.
- mult(b,n)
c = a.mult(b,n)
c = a.mult(b,n) performs c = a * b.
If n is less than the actual significant digits of a * b,
then c is rounded properly according to the BigDecimal.limit.
If n is zero,then the result is the same as *'s.
- div(b[,n])
c = a.div(b,n)
c = a.div(b,n) performs c = a / b.
If n is less than the actual significant digits of a / b,
then c is rounded properly according to the BigDecimal.limit.
If n is zero,then the result is the same as /'s.
If n is not given,then the result will be an integer(BigDecimal) like Float#div.
- fix
c = a.fix
returns integer part of a.
- frac
c = a.frac
returns fraction part of a.
- floor[(n)]
c = a.floor
returns the maximum integer value (in BigDecimal) which is less than or equal to a.
c = BigDecimal("1.23456").floor # ==> 1
c = BigDecimal("-1.23456").floor # ==> -2
As shown in the following example,an optional integer argument (n) specifying the position
of the target digit can be given.
If n> 0,then the (n+1)th digit counted from the decimal point in fraction part is processed(resulting number of fraction part digits is less than or equal to n).
If n<0,then the n-th digit counted from the decimal point in integer part is processed(at least n 0's are placed from the decimal point to left).
c = BigDecimal("1.23456").floor(4) # ==> 1.2345
c = BigDecimal("15.23456").floor(-1) # ==> 10.0
- ceil[(n)]
c = a.ceil
returns the minimum integer value (in BigDecimal) which is greater than or equal to a.
c = BigDecimal("1.23456").ceil # ==> 2
c = BigDecimal("-1.23456").ceil # ==> -1
As shown in the following example,an optional integer argument (n) specifying the position
of the target digit can be given.
If n>0,then the (n+1)th digit counted from the decimal point in fraction part is processed(resulting number of fraction part digits is less than or equal to n).
If n<0,then the n-th digit counted from the decimal point in integer part is processed(at least n 0's are placed from the decimal point to left).
c = BigDecimal("1.23456").ceil(4) # ==> 1.2346
c = BigDecimal("15.23456").ceil(-1) # ==> 20.0
- round[(n[,b])]
c = a.round
round a to the nearest 1(default)ÅD
c = BigDecimal("1.23456").round # ==> 1
c = BigDecimal("-1.23456").round # ==> -1
The rounding operation changes according to BigDecimal::mode(BigDecimal::ROUND_MODE,flag) if specified.
As shown in the following example,an optional integer argument (n) specifying the position
of the target digit can be given.
If n>0,then the (n+1)th digit counted from the decimal point in fraction part is processed(resulting number of fraction part digits is less than or equal to n).
If n<0,then the n-th digit counted from the decimal point in integer part is processed(at least n 0's are placed from the decimal point to left).
c = BigDecimal::new("1.23456").round(4) # ==> 1.2346
c = BigDecimal::new("15.23456").round(-1) # ==> 20.0
Rounding operation can be specified by setting the second optional argument b with the valid ROUND_MODE.
c = BigDecimal::new("1.23456").round(3,BigDecimal::ROUND_HALF_EVEN) # ==> 1.234
c = BigDecimal::new("1.23356").round(3,BigDecimal::ROUND_HALF_EVEN) # ==> 1.234
- truncate[(n)]
c = a.truncate
truncate a to the nearest 1ÅD
As shown in the following example,an optional integer argument (n) specifying the position
of the target digit can be given.
If n>0,then the (n+1)th digit counted from the decimal point in fraction part is processed(resulting number of fraction part digits is less than or equal to n).
If n<0,then the n-th digit counted from the decimal point in integer part is processed(at least n 0's are placed from the decimal point to left).
c = BigDecimal::new("1.23456").truncate(4) # ==> 1.2345
c = BigDecimal::new("15.23456").truncate(-1) # ==> 10.0
- abs
c = a.abs
returns an absolute value of a.
- to_i
changes a to an integer.
i = a.to_i
i becomes to Fixnum or Bignum.
If a is Infinity or NaN,then i becomes to nil.
- to_s[(n)]
converts to string(default results look like "0.xxxxxEn").
BigDecimal("1.23456").to_s # ==> "0.123456E1"
If n(>0) is given,then a space is inserted to each of two parts divided by the decimal point
after every n digits for readability.
BigDecimal("0.1234567890123456789").to_s(10) # ==> "0.1234567890 123456789E0"
n can be a string representing a positive integer number.
BigDecimal("0.1234567890123456789").to_s("10") # ==> "0.1234567890 123456789E0"
If the first character is '+'(or ' '),then '+'(or ' ') will be set before value string
when the value is positive.
BigDecimal("0.1234567890123456789").to_s(" 10") # ==> " 0.1234567890 123456789E0"
BigDecimal("0.1234567890123456789").to_s("+10") # ==> "+0.1234567890 123456789E0"
BigDecimal("-0.1234567890123456789").to_s("10") # ==> "-0.1234567890 123456789E0"
At the end of the string,'E'(or 'e') or 'F'(or 'f') can be specified to change
number representation.
BigDecimal("1234567890.123456789").to_s("E") # ==> "0.1234567890123456789E10"
BigDecimal("1234567890.123456789").to_s("F") # ==> "1234567890.123456789"
BigDecimal("1234567890.123456789").to_s("5E") # ==> "0.12345 67890 12345 6789E10"
BigDecimal("1234567890.123456789").to_s("5F") # ==> "12345 67890.12345 6789"
- exponent
returns an integer holding exponent value of a.
n = a.exponent
means a = 0.xxxxxxx*10**n.
- precs
n,m = a.precs
precs returns number of significant digits (n) and maximum number of
significant digits (m) of a.
- to_f
Creates a new Float object having (nearly) the same value.
Use split method if you want to convert by yourself.
- sign
n = a.sign
returns positive value if a > 0,negative value if a < 0,
otherwise zero if a == 0.
where the value of n means that a is:
n = BigDecimal::SIGN_NaN(0) : a is NaN
n = BigDecimal::SIGN_POSITIVE_ZERO(1) : a is +0
n = BigDecimal::SIGN_NEGATIVE_ZERO(-1) : a is -0
n = BigDecimal::SIGN_POSITIVE_FINITE(2) : a is positive
n = BigDecimal::SIGN_NEGATIVE_FINITE(-2) : a is negative
n = BigDecimal::SIGN_POSITIVE_INFINITE(3) : a is +Infinity
n = BigDecimal::SIGN_NEGATIVE_INFINITE(-3) : a is -Infinity
The value in () is the actual value,see (Internal structure.
- nan?
a.nan? returns True when a is NaN.
- infinite?
a.infinite? returns 1 when a is Infinity, -1 when a is -Infinity, nil otherwise.
- finite?
a.finite? returns true when a is neither Infinity nor NaN.
- zero?
c = a.zero?
returns true if a is equal to 0,otherwise returns false
- nonzero?
c = a.nonzero?
returns nil if a is 0,otherwise returns a itself.
- split
decomposes a BigDecimal value to 4 parts.
All 4 parts are returned as an array.
Parts consist of a sign(0 when the value is NaN,+1 for positive and
-1 for negative value), a string representing fraction part,base value(always 10 currently),and an integer(Fixnum) for exponent respectively.
a=BigDecimal::new("3.14159265")
f,x,y,z = a.split
where f=+1,x="314159265",y=10 and z=1
therefore,you can translate BigDecimal value to Float as:
s = "0."+x
b = f*(s.to_f)*(y**z)
- inspect
is used for debugging output.
p a=BigDecimal::new("3.14",10)
should produce output like "#<0x112344:'0.314E1',4(12)%gt;".
where "0x112344" is the address,
'0.314E1' is the value,4 is the number of the significant digits,
and 12 is the maximum number of the significant digits
the object can hold.
- sqrt
c = a.sqrt(n)
computes square root value of a with significant digit number n at least.
- **
c = a ** n
returns the value of a powered by n.
n must be an integer.
- power
The same as ** method.
c = a.power(n)
returns the value of a powered by n(c=a**n).
n must be an integer.
- divmod,quo,modulo,%,remainder
See,corresponding methods in Float class.
- <=>
c = a <=> b
returns 0 if a==b,1 if a > b,and returns -1 if a < b.
Following methods need no explanation.
- ==
- ===
same as ==,used in case statement.
- !=
- <
- <=
- >
- >=
About 'coerce'
For the binary operation like A op B:
- 1.Both A and B are BigDecimal objects
- A op B is normally performed.
- 2.A is the BigDecimal object but B is other than BigDecimal object
- Operation is performed,after B is translated to corresponding BigDecimal object(because BigDecimal supports coerce method).
- 3.A is not the BigDecimal object but B is BigDecimal object
- If A has coerce method,then B will translate A to corresponding
BigDecimal object and the operation is performed,otherwise an error occures.
String is not translated to BigDecimal in default.
Uncomment /* #define ENABLE_NUMERIC_STRING */ in bigdecimal.c, compile and install
again if you want to enable string to BigDecimal conversion.
Translation stops without error at the character representing non digit.
For instance,"10XX" is translated to 10,"XXXX" is translated to 0.
String representing zero or infinity such as "Infinity","+Infinity","-Infinity",and "NaN" can also be translated to BigDecimal unless false is specified by mode method.
BigDecimal class supports coerce method(for the details about coerce method,see Ruby documentations). This means the most binary operation can be performed if the BigDecimal object is at the left hand side of the operation.
For example:
a = BigDecimal.E(20)
c = a * "0.123456789123456789123456789" # A String is changed to BigDecimal object.
is performed normally.
But,because String does not have coerce method,the following example can not be performed.
a = BigDecimal.E(20)
c = "0.123456789123456789123456789" * a # ERROR
If you actually have any inconvenience about the error above.
You can define a new class derived from String class,
and define coerce method within the new class.