2003-03-28 08:00:21 +03:00
< HTML >
< HEAD >
< META HTTP-EQUIV = "Content-Type" CONTENT = "text/html" >
< style type = "text/css" > < ! - -
2003-06-27 08:38:57 +04:00
body { color: #3f0f0f; background: #fefeff; margin-left: 2em; margin-right: 2em;}
h1 { color: #ffffff; background-color: #3939AD; border-color: #FF00FF; width: 100%; border-style: solid;
border-top-width: 0.1em; border-bottom-width: 0.1em; border-right: none; border-left: none;
padding: 0.1em; font-weight: bold; font-size: 160%; text-align: center;}
h2 { color: #00007f; background-color: #e7e7ff; border-color: #000094; width: 100%; border-style: solid; border-le ft: none; border-right: none; border-top-width: 0.1em; border-bottom-width: 0.1em; padding: 0.1em;
font-weight: bold; font-size: 110%;
2003-03-28 08:00:21 +03:00
}
2003-06-27 08:38:57 +04:00
h3 { color: #00007f; padding: 0.2em; font-size: 110%;}
h4, h5 { color: #000000; padding: 0.2em; font-size: 100%;}
table { margin-top: 0.2em; margin-bottom: 0.2em; margin-left: 2em; margin-right: 2em;}
caption { color: #7f0000; font-weight: bold;}
th { background: #e7e7ff; padding-left: 0.2em; padding-right: 0.2em;}
td { background: #f3f7ff; padding-left: 0.2em; padding-right: 0.2em;}
code { color: #0000df;}
dt { margin-top: 0.2em;}
li { margin-top: 0.2em;}
2003-03-28 08:00:21 +03:00
pre
2003-06-27 08:38:57 +04:00
{ BACKGROUND-COLOR: #d0d0d0; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none;
BORDER-RIGHT: medium none; BORDER-TOP: medium none; LINE-HEIGHT: 100%; MARGIN: 12px 12px 12px 12px;
PADDING-BOTTOM: 12px; PADDING-LEFT: 12px; PADDING-RIGHT: 12px; PADDING-TOP: 12px;
WHITE-SPACE: pre; WIDTH: 100%
2003-03-28 08:00:21 +03:00
}
-->< / style >
< TITLE > BigDecimal:An extension library for Ruby< / TITLE >
< / HEAD >
< BODY BGCOLOR = #FFFFE0 >
< H1 > BigDecimal(Variable Precision Floating Library for Ruby)< / H1 >
< DIV align = "right" > < A HREF = "./bigdecimal_ja.html" > Japanese< / A > < / DIV > < BR >
BigDecimal is an extension library for the Ruby interpreter.
Using BigDecimal class, you can obtain any number of significant digits in computation.
For the details about Ruby see:< BR >
< UL >
< LI > < A HREF = "http://www.ruby-lang.org/en/" > http://www.ruby-lang.org/en/< / A > :Official Ruby page(English).< / LI >
< LI > < A HREF = "http://kahori.com/ruby/ring/" > http://kahori.com/ruby/ring/< / A > :Mutually linked pages relating to Ruby(Japanese).
< / LI >
< / UL >
NOTE:< BR >
This software is provided "AS IS" and without any express or
implied warranties,including,without limitation,the implied
warranties of merchantibility and fitness for a particular
purpose. For the details,see COPYING and README included in this
distribution.
< BR >
< hr >
< H2 > Contents< / H2 >
< UL >
< LI > < A HREF = "#INTRO" > Introduction< / LI >
< LI > < A HREF = "#SPEC" > Usage and methods< / A > < / LI >
< LI > < A HREF = "#UNDEF" > Infinity,NaN,Zero< / A > < / LI >
< LI > < A HREF = "#STRUCT" > Internal structure< / A > < / LI >
< LI > < A HREF = "#BASE" > Binary or decimal number representation< / A > < / LI >
< LI > < A HREF = "#PREC" > Resulting number of significant digits< / A > < / LI >
< / UL >
< HR >
< A NAME = "#INTRO" >
< H2 > Introduction< / H2 >
Ruby already has builtin (variable length integer number) class Bignum. Using Bignum class,you can obtain
2003-06-27 08:38:57 +04:00
any integer value in magnitude. But, variable length decimal number class is not yet built in.
2003-03-28 08:00:21 +03:00
This is why I made variable length floating class BigDecimal.
Feel free to send any comments or bug reports to me.
< A HREF = "mailto:shigeo@tinyforest.gr.jp" > shigeo@tinyforest.gr.jp< / A >
I will try(but can't promise) to fix bugs reported.
< hr >
< H2 > Installation< / H2 >
The Ruby latest version can be downloaded from < A HREF = "http://www.ruby-lang.org/en/" > Official Ruby page< / A > .
Once decompress the downloaded Ruby archive,follow the normal installation procedures according to the
documents included.
< A NAME = "#SPEC" >
< H2 > Usage and methods< / H2 >
Suppose you already know Ruby programming,
to create BigDecimal objects,the program would like:< BR >
< CODE > < PRE >
require 'bigdecimal'
a=BigDecimal::new("0.123456789123456789")
2003-07-18 19:23:23 +04:00
b=BigDecimal("123456.78912345678",40)
2003-03-28 08:00:21 +03:00
c=a+b
< / PRE > < / CODE >
< H3 > List of methods< / H3 >
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.
2003-07-18 19:23:23 +04:00
< P >
Some more methods are available in Ruby code (not C code).
To use them,just require util.rb as:
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-07-18 19:23:23 +04:00
require "bigdecimal/util.rb"
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-07-18 19:23:23 +04:00
String to BigDecimal conversion, BigDecimal to String conversion
(in "nnnnnn.mmmm" form not in "0.xxxxxEn" form) etc are defined.
For details,see the util.rb code.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< H4 > < U > Class methods< / U > < / H4 >
2003-03-28 08:00:21 +03:00
< UL >
2003-06-27 08:38:57 +04:00
< LI > < B > new< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
"new" method creates a new BigDecimal object.< BR >
2003-07-18 19:23:23 +04:00
a=BigDecimal::new(s[,n]) or< BR >
a=BigDecimal(s[,n]) or< BR >
2003-03-28 08:00:21 +03:00
where:< BR >
s: Initial value string.< BR >
n: Maximum number of significant digits of a. n must be a Fixnum object.
If n is omitted or is equal to 0,then the maximum number of significant digits of a is determined from the length of s.
2003-07-25 06:26:56 +04:00
Actual number of digits handled in computations are usually gretaer than n.< BR >
2003-07-22 18:04:23 +04:00
n is useful when performing divisions like
< CODE > < PRE >
BigDecimal("1") / BigDecimal("3") # => 0.3333333333 33E0
BigDecimal("1",10) / BigDecimal("3",10) # => 0.3333333333 3333333333 33333333E0
< / PRE > < / CODE >
2003-07-25 06:26:56 +04:00
but the resulting digits obtained may differ in future version.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > mode< / B > < / LI > < BLOCKQUOTE >
mode method controls BigDecimal computation.Following usage are defined.< BR >
< P > < B > [EXCEPTION control]< / B > < P >
Actions when computation results NaN or Infinity can be defined as follows.
< P >
< BLOCKQUOTE >
f = BigDecimal::mode(BigDecimal::EXCEPTION_NaN,flag)< BR >
f = BigDecimal::mode(BigDecimal::EXCEPTION_INFINITY,flag)< BR >
f = BigDecimal::mode(BigDecimal::EXCEPTION_UNDERFLOW,flag)< BR >
f = BigDecimal::mode(BigDecimal::EXCEPTION_OVERFLOW,flag)< BR >
f = BigDecimal::mode(BigDecimal::EXCEPTION_ZERODIVIDE,flag)< BR >
f = BigDecimal::mode(BigDecimal::EXCEPTION_ALL,flag)< BR >
< / BLOCKQUOTE >
EXCEPTION_NaN controls the execution when computation results to NaN.< BR >
EXCEPTION_INFINITY controls the execution when computation results to Infinity(<28> }Infinity).< BR >
EXCEPTION_UNDERFLOW controls the execution when computation underflows.< BR >
EXCEPTION_OVERFLOW controls the execution when computation overflows.< BR >
EXCEPTION_ZERODIVIDE controls the execution when zero-division occures.< BR >
EXCEPTION_ALL controls the execution for any exception defined occures.< BR >
If the flag is true,then the relating exception is thrown.< BR >
No exception is thrown when the flag is false(default) and computation
continues with the result:< BR >
< BLOCKQUOTE >
EXCEPTION_NaN results to NaN< BR >
EXCEPTION_INFINITY results to +Infinity or -Infinity< BR >
EXCEPTION_UNDERFLOW results to 0.< BR >
EXCEPTION_OVERFLOW results to +Infinity or -Infinity< BR >
EXCEPTION_ZERODIVIDE results to +Infinity or -Infinity< BR >
< / BLOCKQUOTE >
EXCEPTION_INFINITY,EXCEPTION_OVERFLOW, and EXCEPTION_ZERODIVIDE are
currently the same.< BR >
The return value of mode method is the previous value set.< BR >
nil is returned if any argument is wrong.< BR >
Suppose the return value of the mode method is f,then
f & BigDecimal::EXCEPTION_NaN !=0 means EXCEPTION_NaN is set to on.
< P >
< B > [ROUND error control]< / B > < P >
Rounding operation can be controlled as:
< BLOCKQUOTE >
f = BigDecimal::mode(BigDecimal::COMP_MODE,flag)
< / BLOCKQUOTE >
where flag must be one of:
< TABLE >
< TR > < TD > COMP_MODE_TRUNCATE< / TD > < TD > truncate< / TD > < / TR >
2003-07-23 08:48:56 +04:00
< TR > < TD > COMP_MODE_ROUND< / TD > < TD > round,default< / TD > < / TR >
2003-06-27 08:38:57 +04:00
< TR > < TD > COMP_MODE_CEIL< / TD > < TD > ceil< / TD > < / TR >
< TR > < TD > COMP_MODE_FLOOR< / TD > < TD > floor< / TD > < / TR >
< TR > < TD > COMP_MODE_EVEN< / TD > < TD > Banker's rounding< / TD > < / TR >
< / TABLE >
nil is returned if any argument is illegal.< BR >
The digit location for rounding operation can not be specified by mode method,
2003-07-25 06:26:56 +04:00
use truncate/round/ceil/floor/add/sub/mult/div mthods for each instance instead.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > limit[(n)]< / B > < / LI > < BLOCKQUOTE >
Limits the maximum digits that the newly created BigDecimal objects can hold
2003-07-25 06:26:56 +04:00
never exceed n+? (Currently,? can not be determined beforehand,but not so big).
This means the rounding operation specified by BigDecimal.mode is
performed if necessary.
limit returns maximum value before set.
2003-06-27 08:38:57 +04:00
Zero,the default value,means no upper limit.< BR >
2003-07-25 06:26:56 +04:00
Except for zero,the limit has more priority than instance methods such as truncate,round,ceil,floor,add,sub,mult,and div. < BR >
2003-06-27 08:38:57 +04:00
mf = BigDecimal::limit(n)< BR >
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > double_fig< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
double_fig is a class method which returns the number of digits
the Float class can have.
< CODE > < PRE >
p BigDecimal::double_fig # ==> 20 (depends on the CPU etc.)
< / PRE > < / CODE >
The equivalent C programs which calculates the value of
double_fig is:
< CODE > < PRE >
double v = 1.0;
int double_fig = 0;
while(v + 1.0 > 1.0) {
++double_fig;
v /= 10;
}
< / PRE > < / CODE >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > BASE< / B > < / LI > < BLOCKQUOTE >
Base value used in the BigDecimal calculation.
On 32 bits integer system,the value of BASE is 10000.< BR >
b = BigDecimal::BASE< BR >
< / BLOCKQUOTE >
< / UL >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< H4 > < U > Instance methods< / U > < / H4 >
< UL >
< LI > < B > +< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
addition(c = a + b)< BR >
For the resulting number of significant digits of c,see < A HREF = "#PREC" > Resulting number of significant digits< / A > .
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > -< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
subtraction (c = a - b) or negation (c = -a)< BR >
For the resulting number of significant digits of c,see < A HREF = "#PREC" > Resulting number of significant digits< / A > .
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > *< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
multiplication(c = a * b)< BR >
For the resulting number of significant digits of c,see < A HREF = "#PREC" > Resulting number of significant digits< / A > .
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > /< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
division(c = a / b)< BR >
For the resulting number of significant digits of c,see < A HREF = "#PREC" > Resulting number of significant digits< / A > .
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > add< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.add(b,n)< BR >
c = a.add(b,n) performs c = a + b.
If n is less than the actual significant digits of a + b,
2003-07-25 06:26:56 +04:00
then c is rounded properly according to the BigDecimal.limit.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > sub< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.sub(b,n)< BR >
c = a.sub(b,n) performs c = a - b.
If n is less than the actual significant digits of a - b,
2003-07-25 06:26:56 +04:00
then c is rounded properly according to the BigDecimal.limit.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > mult< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.mult(b,n)< BR >
c = a.mult(b,n) performs c = a * b.
If n is less than the actual significant digits of a * b,
2003-07-25 06:26:56 +04:00
then c is rounded properly according to the BigDecimal.limit.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > div< / B > < / LI > < BLOCKQUOTE >
2003-07-25 06:26:56 +04:00
c = a.div(b,n)< BR >
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.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > %< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
r = a%b < BR >
is the same as:< BR >
r = a-((a/b).floor)*b< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > fix< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.fix< BR >
returns integer part of a.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > frac< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.frac< BR >
returns fraction part of a.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > floor[(n)]< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.floor< BR >
2003-06-27 08:38:57 +04:00
returns the maximum integer value (in BigDecimal) which is less than or equal to a.
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal("1.23456").floor # ==> 1
c = BigDecimal("-1.23456").floor # ==> -2
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
As shown in the following example,an optional integer argument (n) specifying the position
2003-06-27 08:38:57 +04:00
of the target digit can be given.< BR >
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).< BR >
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 ) .
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal::new("1.23456").floor(4) # ==> 1.2345
c = BigDecimal::new("15.23456").floor(-1) # ==> 10.0
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > ceil[(n)]< / B > < / LI > < BLOCKQUOTE >
c = a.ceil< BR >
returns the minimum integer value (in BigDecimal) which is greater than or equal to a.
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal("1.23456").ceil # ==> 2
c = BigDecimal("-1.23456").ceil # ==> -1
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
As shown in the following example,an optional integer argument (n) specifying the position
2003-06-27 08:38:57 +04:00
of the target digit can be given.< BR >
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).< BR >
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 ) .
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal::new("1.23456").ceil(4) # ==> 1.2346
c = BigDecimal::new("15.23456").ceil(-1) # ==> 20.0
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > round[(n[,b])]< / B > < / LI > < BLOCKQUOTE >
c = a.round< BR >
round a to the nearest 1<> D< BR >
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal("1.23456").round # ==> 1
c = BigDecimal("-1.23456").round # ==> -1
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
As shown in the following example,an optional integer argument (n) specifying the position
of the target digit can be given.< BR >
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).< BR >
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 ) .
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal::new("1.23456").round(4) # ==> 1.2346
c = BigDecimal::new("15.23456").round(-1) # ==> 20.0
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-06-27 08:38:57 +04:00
If the second optional argument b is given with the non-zero value(default is zero) then
so called Banker's rounding is performed.< BR >
Suppose the digit p is to be rounded,then:< BR >
If p< 5 then p is truncated < BR >
If p>5 then p is rounded up< BR >
If p is 5 then round up operation is taken only when the left hand side digit of p is odd.
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal::new("1.23456").round(3,1) # ==> 1.234
c = BigDecimal::new("1.23356").round(3,1) # ==> 1.234
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > truncate[(n)]< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.truncate< BR >
truncate a to the nearest 1<> D< BR >
As shown in the following example,an optional integer argument (n) specifying the position
2003-06-27 08:38:57 +04:00
of the target digit can be given.< BR >
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).< BR >
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 ) .
2003-03-28 08:00:21 +03:00
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-06-27 08:38:57 +04:00
c = BigDecimal::new("1.23456").truncate(4) # ==> 1.2345
c = BigDecimal::new("15.23456").truncate(-1) # ==> 10.0
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > divmod< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c,r = a.divmod(b) # a = c*b + r< BR >
returns the quotient and remainder of a/b.< BR >
a = c * b + r is always satisfied.< BR >
2003-06-27 08:38:57 +04:00
where c is the integer satisfying
2003-03-28 08:00:21 +03:00
c = (a/b).floor < BR >
and,therefore
r = a - c*b< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > remainder< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
r=a.remainder(b)< BR >
returns the remainder of a/b.< BR >
2003-06-27 08:38:57 +04:00
where c is the integer satisfying
2003-03-28 08:00:21 +03:00
c = (a/b).fix < BR >
and,therefore:
r = a - c*b< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > abs< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.abs< BR >
returns an absolute value of a.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > to_i< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
changes a to an integer.< BR >
i = a.to_i< BR >
i becomes to Fixnum or Bignum.
2003-06-27 08:38:57 +04:00
If a is Infinity or NaN,then i becomes to nil.
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > to_s[(n)]< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
converts to string(results look like "0.xxxxxEn").< BR >
s = a.to_s< BR >
If n is given,then a space is inserted after every n digits for readability.< BR >
s = a.to_s(n)
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > exponent< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
returns an integer holding exponent value of a.< BR >
n = a.exponent < BR >
means a = 0.xxxxxxx*10**n.
2003-07-22 18:04:23 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-07-22 18:04:23 +04:00
< LI > < B > prec< / B > < / LI > < BLOCKQUOTE >
n,m = a.prec < BR >
prec returns number of significant digits (n) and maximum number of
significant digits (m) of a.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-07-22 18:04:23 +04:00
2003-06-27 08:38:57 +04:00
< LI > < B > to_f< / B > < / LI > < BLOCKQUOTE >
2003-07-18 19:23:23 +04:00
Creates a new Float object having (nearly) the same value.
Use split method if you want to convert by yourself.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > sign< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
n = a.sign < BR >
2003-07-18 19:23:23 +04:00
returns positive value if a > 0,negative value if a < 0,
otherwise zero if a == 0.< BR >
2003-03-28 08:00:21 +03:00
where the value of n means that a is:< BR >
n = BigDecimal::SIGN_NaN(0) : a is NaN< BR >
n = BigDecimal::SIGN_POSITIVE_ZERO(1) : a is +0< BR >
n = BigDecimal::SIGN_NEGATIVE_ZERO(-1) : a is -0< BR >
n = BigDecimal::SIGN_POSITIVE_FINITE(2) : a is positive< BR >
n = BigDecimal::SIGN_NEGATIVE_FINITE(-2) : a is negative< BR >
n = BigDecimal::SIGN_POSITIVE_INFINITE(3) : a is +Infinity< BR >
n = BigDecimal::SIGN_NEGATIVE_INFINITE(-3) : a is -Infinity< BR >
The value in () is the actual value,see (< A HREF = "#STRUCT" > Internal structure< / A > .< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > nan?< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
a.nan? returns True when a is NaN.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > infinite?< / B > < / LI > < BLOCKQUOTE >
a.infinite? returns 1 when a is +<2B> <> ,-1 when a is -<2D> <> , nil otherwise.
< / BLOCKQUOTE >
< LI > < B > finite?< / B > < / LI > < BLOCKQUOTE >
a.finite? returns true when a is neither <20> <> nor NaN.
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > zero?< / B > < / LI > < BLOCKQUOTE >
c = a.zero?< BR >
returns true if a is equal to 0,otherwise returns false< BR >
< / BLOCKQUOTE >
< LI > < B > nonzero?< / B > < / LI > < BLOCKQUOTE >
c = a.nonzero?< BR >
returns nil if a is 0,otherwise returns a itself.< BR >
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > split< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
decomposes a BigDecimal value to 4 parts.
All 4 parts are returned as an array.< BR >
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.
2003-06-27 08:38:57 +04:00
a=BigDecimal::new("3.14159265")< BR >
f,x,y,z = a.split< BR >
2003-03-28 08:00:21 +03:00
where f=+1,x="314159265",y=10 and z=1< BR >
therefore,you can translate BigDecimal value to Float as:< BR >
s = "0."+x< BR >
b = f*(s.to_f)*(y**z)< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > inspect< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
is used for debugging output.< BR >
p a=BigDecimal::new("3.14",10)< BR >
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.
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > sqrt< / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a.sqrt(n)< BR >
computes square root value of a with significant digit number n at least.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
2003-06-27 08:38:57 +04:00
< LI > < B > **< / B > < / LI > < BLOCKQUOTE >
c = a ** n< BR >
returns the value of a powered by n.
n must be an integer.< BR >
< / BLOCKQUOTE >
< LI > < B > power< / B > < / LI > < BLOCKQUOTE >
The same as ** method.< BR >
2003-03-28 08:00:21 +03:00
c = a.power(n)< BR >
returns the value of a powered by n(c=a**n).
n must be an integer.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
< LI > < B > < => < / B > < / LI > < BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
c = a < => b < BR >
returns 0 if a==b,1 if a & gt b,and returns -1 if a & lt b.< BR >
2003-06-27 08:38:57 +04:00
< / BLOCKQUOTE >
2003-03-28 08:00:21 +03:00
< / UL >
Following methods need no explanation.< BR >
< UL >
< LI > ==< / LI >
< LI > ===< / LI >
same as ==,used in case statement.
< LI > !=< / LI >
< LI > < < / LI >
< LI > < =< / LI >
< LI > > < / LI >
< LI > > =< / LI >
< / UL >
2003-07-18 19:23:23 +04:00
< H4 > < U > Class methods(trial version)< / U > < / H4 >
Following class methods are in trial stage, and not usable in default.
Uncomment /* #define ENABLE_TRIAL_METHOD */ in bigdecimal.c, compile and install
again if you want to use.
< UL >
< LI > < B > E< / B > < / LI > < BLOCKQUOTE >
e = BigDecimal::E(n)< BR >
where e(=2.718281828....) is the base value of natural logarithm.< BR >
n specifies the length of significant digits of e.
< / BLOCKQUOTE >
< LI > < B > PI< / B > < / LI > < BLOCKQUOTE >
e = BigDecimal::PI(n)< BR >
returns at least n digits of the ratio of the circumference of a circle to its diameter
(pi=3.14159265358979....) using J.Machin's formula.< BR >
< / BLOCKQUOTE >
< / UL >
< H4 > < U > Instance methods(trial version)< / U > < / H4 >
Following instance methods are in trial stage, and not usable in default.
Uncomment /* #define ENABLE_TRIAL_METHOD */ in bigdecimal.c, compile and install
again if you want to use.
< UL >
< LI > < B > sincos< / B > < / LI > < BLOCKQUOTE >
computes and returns sine and cosine value of a with significant digit number n at least.< BR >
sin,cos = a.sincos(n)< BR >
Computation may return bad results unless |a|< 2 * 3 . 1415 . . . . .
< / BLOCKQUOTE >
< LI > < B > exp< / B > < / LI > < BLOCKQUOTE >
c = a.exp(n)< BR >
computes the base of natural logarithm value(e=2.718281828....) powered by a
with significant digit number n at least.< BR >
< / BLOCKQUOTE >
< / UL >
2003-03-28 08:00:21 +03:00
< HR >
< H3 > About 'coerce'< / H3 >
< B > For the binary operation like A op B:< / B >
< DL >
< DT > 1.Both A and B are BigDecimal objects< / DT >
< DD > A op B is normally performed.< / DD >
< DT > 2.A is the BigDecimal object but B is other than BigDecimal object< / DT >
< DD > Operation is performed,after B is translated to correcponding BigDecimal object(because BigDecimal supports coerce method).< / DD >
< DT > 3.A is not the BigDecimal object but B is BigDecimal object< / DT >
< DD > If A has coerce mthod,then B will translate A to corresponding
BigDecimal object and the operation is performed,otherwise an error occures.< / DD >
< / DL >
2003-07-18 19:23:23 +04:00
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.
2003-03-28 08:00:21 +03:00
Translation stops without error at the character representing non digit.
For instance,"10XX" is translated to 10,"XXXX" is translated to 0.< BR >
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.< BR >
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.< BR > < BR >
For example:
< CODE > < PRE >
a = BigDecimal.E(20)
c = a * "0.123456789123456789123456789" # A String is changed to BigDecimal object.
< / PRE > < / CODE >
is performed normally.< BR >
But,because String does not have coerce method,the following example can not be performed.< BR >
< CODE > < PRE >
a = BigDecimal.E(20)
c = "0.123456789123456789123456789" * a # ERROR
< / PRE > < / CODE >
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.< BR >
< hr >
< A NAME = "#UNDEF" >
< H2 > Infinity,Not a Number(NaN),Zero< / H2 >
Infinite numbers and NaN can be represented by string writing "+Infinity"(or "Infinity"),"-Infinity",and "NaN" respectively in your program.
Infinite numbers can be obtained by 1.0/0.0(=Infinity) or -1.0/0.0(=-Infinity).
< BR > < BR >
NaN(Not a number) can be obtained by undefined computation like 0.0/0.0
or Infinity-Infinity.
Any computation including NaN results to NaN.
Comparisons with NaN never become true,including comparison with NaN itself.
< BR > < BR >
Zero has two different variations as +0.0 and -0.0.
But,still, +0.0==-0.0 is true.
< BR > < BR >
Computation results including Infinity,NaN,+0.0 or -0.0 become complicated.
Run following program and comfirm the results.
Send me any incorrect result if you find.
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-03-28 08:00:21 +03:00
require "bigdecimal"
aa = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
ba = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
opa = %w(+ - * / < => > >= < == != < =)
for a in aa
for b in ba
for op in opa
x = BigDecimal::new(a)
y = BigDecimal::new(b)
eval("ans= x #{op} y;print a,' ',op,' ',b,' ==> ',ans.to_s,\"\n\"")
end
end
end
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
< hr >
< A NAME = "#STRUCT" >
< H2 > Internal structure< / H2 >
BigDecimal number is defined by the structure Real in BigDecimal.h.
Digits representing a float number are kept in the array frac[] defined in the structure.
In the program,any floating number(BigDecimal number) is represented as:< BR >
< BigDecimal number > = 0.xxxxxxxxx*BASE**n< BR > < BR >
where 'x' is any digit representing mantissa(kept in the array frac[]),
BASE is base value(=10000 in 32 bit integer system),
and n is the exponent value.< BR >
Larger BASE value enables smaller size of the array frac[],and increases computation speed.
The value of BASE is defined ind VpInit(). In 32 bit integer system,this value is
10000. In 64 bit integer system,the value becomes larger.
BigDecimal has not yet been compiled and tested on 64 bit integer system.
It will be very nice if anyone try to run BigDecimal on 64 bit system and
inform me the results.
When BASE is 10000,an element of the array frac[] can have vale of from 0 to 9999.
(up to 4 digits).< BR >
The structure Real is defined in bigdecimal.h as:< BR >
< CODE > < PRE >
typedef struct {
VALUE obj; /* Back pointer(VALUE) for Ruby object. */
unsigned long MaxPrec; /* The size of the array frac[] */
unsigned long Prec; /* Current size of frac[] actually used. */
short sign; /* Attribute of the value. */
/* ==0 : NaN */
/* 1 : +0 */
/* -1 : -0 */
/* 2 : Positive number */
/* -2 : Negative number */
/* 3 : +Infinity */
/* -3 : -Infinity */
unsigned short flag; /* Control flag */
int exponent; /* Exponent value(0.xxxx*BASE**exponent) */
unsigned long frac[1]; /* An araay holding mantissa(Variable) */
} Real;
< / CODE > < / PRE >
The decimal value 1234.56784321 is represented as(BASE=10000):< BR >
< PRE >
0.1234 5678 4321*(10000)**1
< / PRE >
where frac[0]=1234,frac[1]=5678,frac[2]=4321,
Prec=3,sign=2,exponent=1. MaxPrec can be any value greater than or equal to
Prec.
< hr >
< A NAME = "#BASE" >
< H2 > Binary or decimal number representation< / H2 >
I adopted decimal number representation for BigDecimal implementation.
Of cource,binary number representation is common on the most computers.
< H3 > Advantages using decimal representation< / H3 >
The reason why I adopted decimal number representation for BigDecimal is:< BR >
< DL >
< DT > Easy for debugging
< DD > The floating number 1234.56784321 can be easily represented as:< BR >
frac[0]=1234,frac[1]=5678,frac[2]=4321,exponent=1,and sign=2.
< DT > Exact representation
< DD > Following program can add all numbers(in decimal) in a file
without any error(no round operation).< BR >
2003-07-22 18:04:23 +04:00
< CODE > < PRE >
2003-03-28 08:00:21 +03:00
file = File::open(....,"r")
s = BigDecimal::new("0")
while line = file.gets
s = s + line
end
2003-07-22 18:04:23 +04:00
< / PRE > < / CODE >
2003-03-28 08:00:21 +03:00
If the internal representation is binary,translation from decimal to
binary is required and the translation error is inevitable.
For example, 0.1 can not exactly be represented in binary.< BR >
0.1 => b1*2**(-1)+b1*2**(-2)+b3*2**(-3)+b4*2**(-4)....< BR >
where b1=0,b2=0,b3=0,b4=1...< BR >
bn(n=1,2,3,...) is infinite series of digit with value of 0 or 1,
and rounding operation is necessary but where we should round the series ?
Of cource,exact "0.1" is printed if the rouding operation is properly done,
< DT > Significant digit we can have is automatically determined
< DD > In binary representation,0.1 can not be represented in finite series of digit.
But we only need one element(frac[0]=1) in decimal representation.
This means that we can always determine the size of the array frac[] in Real
structure.
< / DL >
< H3 > Disadvantage of decimal representation< / H3 >
Advantages stated so far can also be disadvantages if the input from outside is
represented in binary.
Translation error from decimal to binary or vice versa is inevitable.
So,translation from Float(binary) to BigDecimal(decimal) is not alway done exactly.
< H4 > Which is the first input?< / H4 >
Because most people uses decimal notatin for numeric data representation,
BigDecimal can handle numeric data without loss of translation error.
< hr >
< A NAME = "#PREC" >
< H2 > Resulting number of significant digits< / H2 >
For the fundamental arithmetics such as addition,subtraction,
multiplication,and division,I prepared 2 group of methods< BR >
< H3 > 1. +,-,*,/< / H3 >
For the operation + - * /,you can not specify the resulting
number of significant digits.< BR >
Resulting number of significant digits are defined as:< BR >
2003-07-22 18:04:23 +04:00
1.1 For *,resulting number of significant digits is the sum of the
significant digits of both side of the operator. For / ,resulting number of significant digits is the sum of the
maximum significant digits of both side of the operator.< BR >
2003-03-28 08:00:21 +03:00
1.2 For + and -,resulting number of significant digits is determined so that
no round operation is needed. < br >
For example, c has more than 100 siginificant digits if c is computed as:< BR >
c = 0.1+0.1*10**(-100)< br >
< BR >
2003-07-25 06:26:56 +04:00
As +,-,and * are always exact(no round operation is performed unless BigDecimal.limit is specified),
2003-03-28 08:00:21 +03:00
which means more momories are required to keep computation results.
2003-07-25 06:26:56 +04:00
But,the division such as c=1.0/3.0 will always be rounded.< BR >
2003-03-28 08:00:21 +03:00
< H3 > 2. assign,add,sub,mult,div< / H3 >
The length of the significant digits obtained from +,-,*,/
is always defined by that of right and left side of the operator.
To specify the length of the significant digits by your self,
2003-07-25 06:26:56 +04:00
use methos assign,add,sub,mult,div.
< CODE > < PRE >
BigDecimal("2").div(3,12) # 2.0/3.0 => 0.6666666666 67E0
< / PRE > < / CODE >
< / BLOCKQUOTE >
< H3 > 3. truncate,round,ceil,floor< / H3 >
Using these methods,you can specify rounding location relatively from
decimal point.
< CODE > < PRE >
BigDecimal("6.66666666666666").round(12) # => 0.6666666666 667E1
< / PRE > < / CODE >
< / BLOCKQUOTE >
< H3 > 4. Example< / H3 >
2003-03-28 08:00:21 +03:00
Following example compute the ratio of the circumference of a circle to
its dirmeter(pi=3.14159265358979....) using J.Machin's formula.
< BR > < BR >
< CODE > < PRE >
#!/usr/local/bin/ruby
require "bigdecimal"
#
2003-07-25 06:26:56 +04:00
# Calculates 3.1415.... (the number of times that a circle's diameter
# will fit around the circle) using J. Machin's formula.
2003-03-28 08:00:21 +03:00
#
2003-05-06 17:18:26 +04:00
def big_pi(sig) # sig: Number of significant figures
2003-03-28 08:00:21 +03:00
exp = -sig
pi = BigDecimal::new("0")
two = BigDecimal::new("2")
m25 = BigDecimal::new("-0.04")
m57121 = BigDecimal::new("-57121")
u = BigDecimal::new("1")
k = BigDecimal::new("1")
w = BigDecimal::new("1")
t = BigDecimal::new("-80")
2003-07-25 06:26:56 +04:00
while (u.nonzero? & & u.exponent >= exp)
2003-03-28 08:00:21 +03:00
t = t*m25
2003-07-25 06:26:56 +04:00
u = t.div(k,sig)
2003-03-28 08:00:21 +03:00
pi = pi + u
k = k+two
end
u = BigDecimal::new("1")
k = BigDecimal::new("1")
w = BigDecimal::new("1")
t = BigDecimal::new("956")
2003-07-25 06:26:56 +04:00
while (u.nonzero? & & u.exponent >= exp )
t = t.div(m57121,sig)
u = t.div(k,sig)
2003-03-28 08:00:21 +03:00
pi = pi + u
k = k+two
end
pi
end
if $0 == __FILE__
2003-07-25 06:26:56 +04:00
if ARGV.size == 1
print "PI("+ARGV[0]+"):\n"
p big_pi(ARGV[0].to_i)
else
print "TRY: ruby pi.rb 1000 \n"
end
2003-03-28 08:00:21 +03:00
end
< / PRE > < / CODE >
< HR >
< FONT size = 2 >
< I >
< A HREF = "http://www.tinyforest.gr.jp" >
Shigeo Kobayashi
< / A >
(E-Mail:< A HREF = "mailto:shigeo@tinyforest.gr.jp" > < shigeo@tinyforest.gr.jp> < / U > < / A > )
< / I >
< / FONT >
< / TD >
< / TR >
< / TABLE >
< / BODY >
< / HTML >