ruby-changes:28788
From: zzak <ko1@a...>
Date: Mon, 20 May 2013 06:03:24 +0900 (JST)
Subject: [ruby-changes:28788] zzak:r40840 (trunk): * ext/bigdecimal/bigdecimal.c: Formatting for BigMath [Fixes GH-306]
zzak 2013-05-20 06:00:43 +0900 (Mon, 20 May 2013) New Revision: 40840 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40840 Log: * ext/bigdecimal/bigdecimal.c: Formatting for BigMath [Fixes GH-306] Based on a patch by @eLobato. * ext/bigdecimal/lib/bigdecimal/math.rb: ditto Modified files: trunk/ChangeLog trunk/ext/bigdecimal/bigdecimal.c trunk/ext/bigdecimal/lib/bigdecimal/math.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40839) +++ ChangeLog (revision 40840) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon May 20 05:58:12 2013 Zachary Scott <zachary@z...> + + * ext/bigdecimal/bigdecimal.c: Formatting for BigMath [Fixes GH-306] + Based on a patch by @eLobato. + * ext/bigdecimal/lib/bigdecimal/math.rb: ditto + Mon May 20 04:56:59 2013 Zachary Scott <zachary@z...> * lib/forwardable.rb: Forwardable examples in overview were broken Index: ext/bigdecimal/bigdecimal.c =================================================================== --- ext/bigdecimal/bigdecimal.c (revision 40839) +++ ext/bigdecimal/bigdecimal.c (revision 40840) @@ -2630,14 +2630,14 @@ BigDecimal_save_limit(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2630 } /* 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, VALU https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2760 } /* 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) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L3228 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); Index: ext/bigdecimal/lib/bigdecimal/math.rb =================================================================== --- ext/bigdecimal/lib/bigdecimal/math.rb (revision 40839) +++ ext/bigdecimal/lib/bigdecimal/math.rb (revision 40840) @@ -20,30 +20,40 @@ require 'bigdecimal' https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/math.rb#L20 # # 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 https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/math.rb#L87 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 https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/math.rb#L131 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 https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/math.rb#L170 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 https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/math.rb#L215 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/