BigDecimal(Variable Precision Floating Library for Ruby)

Japanese

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:
NOTE:
This software is provided "AS IS" and without any express or implied warranties,including,without limitation,the implied warranties of merchantability and fitness for a particular purpose. For the details,see COPYING and README included in this distribution.

Contents


Introduction

Ruby already has builtin (variable length integer number) class Bignum. Using Bignum class,you can obtain any integer value in magnitude. But, variable length decimal number class is not yet built in. This is why I made variable length floating class BigDecimal. Feel free to send any comments or bug reports to me.
shigeo@tinyforest.gr.jp I will try(but can't promise) to fix bugs reported.

Installation

The Ruby latest version can be downloaded from Official Ruby page. Once decompress the downloaded Ruby archive,follow the normal installation procedures according to the documents included.

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

Following methods need no explanation.

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.

Infinity,Not a Number(NaN),Zero

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

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.

Zero has two different variations as +0.0 and -0.0. But,still, +0.0==-0.0 is true.

Computation results including Infinity,NaN,+0.0 or -0.0 become complicated. Run following program and confirm the results. Send me any incorrect result if you find.
 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

Internal structure

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:
= 0.xxxxxxxxx*BASE**n

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.
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 is 1000000000. When BASE is 10000,an element of the array frac[] can have value of from 0 to 9999. (up to 4 digits).
The structure Real is defined in bigdecimal.h as:
  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 array holding mantissa(Variable)   */
  } Real;
The decimal value 1234.56784321 is represented as(BASE=10000):
    0.1234 5678 4321*(10000)**1
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.

Binary or decimal number representation

I adopted decimal number representation for BigDecimal implementation. Of cource,binary number representation is common on the most computers.

Advantages using decimal representation

The reason why I adopted decimal number representation for BigDecimal is:
Easy for debugging
The floating number 1234.56784321 can be easily represented as:
frac[0]=1234,frac[1]=5678,frac[2]=4321,exponent=1,and sign=2.
Exact representation
Following program can add all numbers(in decimal) in a file without any error(no round operation).
   file = File::open(....,"r")
   s = BigDecimal::new("0")
   while line = file.gets
      s = s + line
   end
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.
0.1 => b1*2**(-1)+b1*2**(-2)+b3*2**(-3)+b4*2**(-4)....
where b1=0,b2=0,b3=0,b4=1...
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 course, exact "0.1" is printed if the rounding operation is properly done,
Significant digit we can have is automatically determined
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.

Disadvantage of decimal representation

Because most computers have no internal decimal representation. Once you use BigDecimal,you need to keep using it without considering computation cost if exact computation is required.

Which is the first input?

Because most people uses decimal notation for numeric data representation, BigDecimal can handle numeric data without loss of translation error.

Resulting number of significant digits

For the fundamental arithmetics such as addition,subtraction, multiplication,and division,I prepared 2 group of methods

1. +,-,*,/

For the operation + - * /,you can not specify the resulting number of significant digits.
Resulting number of significant digits are defined as:
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.
1.2 For + and -,resulting number of significant digits is determined so that no round operation is needed.
For example, c has more than 100 significant digits if c is computed as:
c = 0.1+0.1*10**(-100)

As +,-,and * are always exact(no round operation is performed unless BigDecimal.limit is specified), which means more memory is required to keep computation results. But,the division such as c=1.0/3.0 will always be rounded.

2. add,sub,mult,div

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, use methos add,sub,mult,div.
 BigDecimal("2").div(3,12) # 2.0/3.0 => 0.6666666666 67E0

3. truncate,round,ceil,floor

Using these methods,you can specify rounding location relatively from decimal point.
 BigDecimal("6.66666666666666").round(12) # => 0.6666666666 667E1

4. Example

Following example compute the ratio of the circumference of a circle to its diameter(pi=3.14159265358979....) using J.Machin's formula.

#!/usr/local/bin/ruby

require "bigdecimal"
#
# Calculates 3.1415.... (the number of times that a circle's diameter
# will fit around the circle) using J. Machin's formula.
#
def big_pi(sig) # sig: Number of significant figures
  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")
  while (u.nonzero? && u.exponent >= exp) 
    t   = t*m25
    u   = t.div(k,sig)
    pi  = pi + u
    k   = k+two
  end

  u = BigDecimal::new("1")
  k = BigDecimal::new("1")
  w = BigDecimal::new("1")
  t = BigDecimal::new("956")
  while (u.nonzero? && u.exponent >= exp )
    t   = t.div(m57121,sig)
    u   = t.div(k,sig)
    pi  = pi + u
    k   = k+two
  end
  pi
end

if $0 == __FILE__
  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
end


Shigeo Kobayashi (E-Mail:<shigeo@tinyforest.gr.jp>)