From 66910f17b7a7c458eac88fbb1086191886caf459 Mon Sep 17 00:00:00 2001 From: shigek Date: Tue, 22 Jul 2003 14:04:23 +0000 Subject: Explanation for prec, and some more for max. number of sig. digits. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/bigdecimal/bigdecimal_en.html | 66 +++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 27 deletions(-) (limited to 'ext/bigdecimal/bigdecimal_en.html') diff --git a/ext/bigdecimal/bigdecimal_en.html b/ext/bigdecimal/bigdecimal_en.html index 5b157101a1..ee3420ed5f 100644 --- a/ext/bigdecimal/bigdecimal_en.html +++ b/ext/bigdecimal/bigdecimal_en.html @@ -91,9 +91,9 @@ This means the number of significant digits in BigDecimal is always a multiple o

Some more methods are available in Ruby code (not C code). To use them,just require util.rb as: -


+
 require "bigdecimal/util.rb"
-
+
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. @@ -108,7 +108,13 @@ where:
s: Initial value string.
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. -Currently, n has no actual meaning(reserved for future use). +n is useful when performing divisions like +
+BigDecimal("1")    / BigDecimal("3")    # => 0.3333333333 33E0
+BigDecimal("1",10) / BigDecimal("3",10) # => 0.3333333333 3333333333 33333333E0
+
+but the result may differ in future version. +
  • mode
  • @@ -270,55 +276,55 @@ 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::new("1.23456").floor(4)   #  ==> 1.2345
      c = BigDecimal::new("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::new("1.23456").ceil(4)   # ==> 1.2346
      c = BigDecimal::new("15.23456").ceil(-1) # ==> 20.0
    -
    +
  • round[(n[,b])]
  • c = a.round
    round a to the nearest 1D
    -
    
    +
      c = BigDecimal("1.23456").round  #  ==> 1
      c = BigDecimal("-1.23456").round #  ==> -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::new("1.23456").round(4)   #  ==> 1.2346
     c = BigDecimal::new("15.23456").round(-1) #  ==> 20.0
    -
    +
    If the second optional argument b is given with the non-zero value(default is zero) then so called Banker's rounding is performed.
    @@ -326,10 +332,10 @@ Suppose the digit p is to be rounded,then:
    If p<5 then p is truncated
    If p>5 then p is rounded up
    If p is 5 then round up operation is taken only when the left hand side digit of p is odd. -
    
    +
     c = BigDecimal::new("1.23456").round(3,1)   #  ==> 1.234
     c = BigDecimal::new("1.23356").round(3,1)   #  ==> 1.234
    -
    +
  • truncate[(n)]
  • @@ -340,10 +346,10 @@ 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
    -
    +
  • divmod
  • @@ -388,8 +394,14 @@ s = a.to_s(n) returns an integer holding exponent value of a.
    n = a.exponent
    means a = 0.xxxxxxx*10**n. +
    +
  • prec
  • +n,m = a.prec
    +prec 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. @@ -583,7 +595,7 @@ 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. -
    
    +
      require "bigdecimal"
      aa  = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
      ba  = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
    @@ -597,7 +609,7 @@ Send me any incorrect result if you find.
         end
       end
      end
    -
    +

    @@ -660,13 +672,13 @@ The reason why I adopted decimal number representation for BigDecimal is:
    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. @@ -704,8 +716,9 @@ multiplication,and division,I prepared 2 group of methods
    For the operation + - * /,you can not specify the resulting number of significant digits.
    Resulting number of significant digits are defined as:
    -1.1 For * and /,resulting number of significant digits is the sum of the -significant digits of both side of the operator.
    +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 siginificant digits if c is computed as:
    @@ -714,8 +727,7 @@ c = 0.1+0.1*10**(-100)
    As +,-,and * are always exact(no round operation is performed), which means more momories are required to keep computation results. -As for the division as c = a/b,the significant digits of c is the same -as a*b. Division such as c=1.0/3.0 will be rounded.
    +Division such as c=1.0/3.0 will be rounded.

    2. assign,add,sub,mult,div

    The length of the significant digits obtained from +,-,*,/ -- cgit v1.2.3