From 2439bc9e69e57244ec7af77cc3bc899d78c3a0b4 Mon Sep 17 00:00:00 2001 From: zzak Date: Sun, 19 May 2013 21:00:43 +0000 Subject: * ext/bigdecimal/bigdecimal.c: Formatting for BigMath [Fixes GH-306] Based on a patch by @eLobato. * ext/bigdecimal/lib/bigdecimal/math.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/bigdecimal/bigdecimal.c | 21 +++++------ ext/bigdecimal/lib/bigdecimal/math.rb | 71 +++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 26 deletions(-) (limited to 'ext/bigdecimal') diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index d6046f3744..06b3886eb5 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2630,14 +2630,14 @@ BigDecimal_save_limit(VALUE self) } /* call-seq: - * BigMath.exp(x, prec) + * BigMath.exp(decimal, numeric) -> BigDecimal * * Computes the value of e (the base of natural logarithms) raised to the - * power of x, to the specified number of digits of precision. + * power of +decimal+, to the specified number of digits of precision. * - * If x is infinity, returns Infinity. + * If +decimal+ is infinity, returns Infinity. * - * If x is NaN, returns NaN. + * If +decimal+ is NaN, returns NaN. */ static VALUE BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec) @@ -2760,16 +2760,16 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec) } /* call-seq: - * BigMath.log(x, prec) + * BigMath.log(decimal, numeric) -> BigDecimal * - * Computes the natural logarithm of x to the specified number of digits of - * precision. + * Computes the natural logarithm of +decimal+ to the specified number of + * digits of precision, +numeric+. * - * If x is zero or negative, raises Math::DomainError. + * If +decimal+ is zero or negative, raises Math::DomainError. * - * If x is positive infinity, returns Infinity. + * If +decimal+ is positive infinity, returns Infinity. * - * If x is NaN, returns NaN. + * If +decimal+ is NaN, returns NaN. */ static VALUE BigMath_s_log(VALUE klass, VALUE x, VALUE vprec) @@ -3228,7 +3228,6 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "truncate", BigDecimal_truncate, -1); rb_define_method(rb_cBigDecimal, "_dump", BigDecimal_dump, -1); - /* mathematical functions */ rb_mBigMath = rb_define_module("BigMath"); rb_define_singleton_method(rb_mBigMath, "exp", BigMath_s_exp, 2); rb_define_singleton_method(rb_mBigMath, "log", BigMath_s_log, 2); diff --git a/ext/bigdecimal/lib/bigdecimal/math.rb b/ext/bigdecimal/lib/bigdecimal/math.rb index 7289661fc2..4504ccb2b0 100644 --- a/ext/bigdecimal/lib/bigdecimal/math.rb +++ b/ext/bigdecimal/lib/bigdecimal/math.rb @@ -20,30 +20,40 @@ require 'bigdecimal' # # Example: # -# require "bigdecimal" # require "bigdecimal/math" # # include BigMath # # a = BigDecimal((PI(100)/2).to_s) -# puts sin(a,100) # -> 0.10000000000000000000......E1 +# puts sin(a,100) # => 0.10000000000000000000......E1 # module BigMath module_function - # Computes the square root of x to the specified number of digits of - # precision. + # call-seq: + # sqrt(decimal, numeric) -> BigDecimal # - # BigDecimal.new('2').sqrt(16).to_s - # -> "0.14142135623730950488016887242096975E1" + # Computes the square root of +decimal+ to the specified number of digits of + # precision, +numeric+. # - def sqrt(x,prec) + # BigMath::sqrt(BigDecimal.new('2'), 16).to_s + # #=> "0.14142135623730950488016887242096975E1" + # + def sqrt(x, prec) x.sqrt(prec) end - # Computes the sine of x to the specified number of digits of precision. + # call-seq: + # sin(decimal, numeric) -> BigDecimal + # + # Computes the sine of +decimal+ to the specified number of digits of + # precision, +numeric+. + # + # If +decimal+ is Infinity or NaN, returns NaN. + # + # BigMath::sin(BigMath::PI(5)/4, 5).to_s + # #=> "0.70710678118654752440082036563292800375E0" # - # If x is infinite or NaN, returns NaN. def sin(x, prec) raise ArgumentError, "Zero or negative precision for sin" if prec <= 0 return BigDecimal("NaN") if x.infinite? || x.nan? @@ -77,9 +87,17 @@ module BigMath neg ? -y : y end - # Computes the cosine of x to the specified number of digits of precision. + # call-seq: + # cos(decimal, numeric) -> BigDecimal + # + # Computes the cosine of +decimal+ to the specified number of digits of + # precision, +numeric+. + # + # If +decimal+ is Infinity or NaN, returns NaN. + # + # BigMath::cos(BigMath::PI(4), 16).to_s + # #=> "-0.999999999999999999999999999999856613163740061349E0" # - # If x is infinite or NaN, returns NaN. def cos(x, prec) raise ArgumentError, "Zero or negative precision for cos" if prec <= 0 return BigDecimal("NaN") if x.infinite? || x.nan? @@ -113,9 +131,17 @@ module BigMath y end - # Computes the arctangent of x to the specified number of digits of precision. + # call-seq: + # atan(decimal, numeric) -> BigDecimal + # + # Computes the arctangent of +decimal+ to the specified number of digits of + # precision, +numeric+. + # + # If +decimal+ is NaN, returns NaN. + # + # BigMath::atan(BigDecimal.new('-1'), 16).to_s + # #=> "-0.785398163397448309615660845819878471907514682065E0" # - # If x is NaN, returns NaN. def atan(x, prec) raise ArgumentError, "Zero or negative precision for atan" if prec <= 0 return BigDecimal("NaN") if x.nan? @@ -144,7 +170,15 @@ module BigMath y end - # Computes the value of pi to the specified number of digits of precision. + # call-seq: + # PI(numeric) -> BigDecimal + # + # Computes the value of pi to the specified number of digits of precision, + # +numeric+. + # + # BigMath::PI(10).to_s + # #=> "0.3141592653589793238462643388813853786957412E1" + # def PI(prec) raise ArgumentError, "Zero or negative argument for PI" if prec <= 0 n = prec + BigDecimal.double_fig @@ -181,8 +215,15 @@ module BigMath pi end + # call-seq: + # E(numeric) -> BigDecimal + # # Computes e (the base of natural logarithms) to the specified number of - # digits of precision. + # digits of precision, +numeric+. + # + # BigMath::E(10).to_s + # #=> "0.271828182845904523536028752390026306410273E1" + # def E(prec) raise ArgumentError, "Zero or negative precision for E" if prec <= 0 n = prec + BigDecimal.double_fig -- cgit v1.2.3