ruby-changes:50504
From: nagachika <ko1@a...>
Date: Sat, 3 Mar 2018 11:07:24 +0900 (JST)
Subject: [ruby-changes:50504] nagachika:r62638 (ruby_2_4): merge revision(s) 57597, 57951: [Backport #13232]
nagachika 2018-03-03 11:07:18 +0900 (Sat, 03 Mar 2018) New Revision: 62638 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62638 Log: merge revision(s) 57597,57951: [Backport #13232] bigdecimal: version 1.3.1 Import bigdecimal version 1.3.1. The full commit log is here: https://github.com/ruby/bigdecimal/compare/v1.3.0...v1.3.1 bigdecimal: version 1.3.2 Import bigdecimal version 1.3.2. The full commit log is here: https://github.com/ruby/bigdecimal/compare/v1.3.1...v1.3.2 This fixes [ruby-core:79603] [Bug #13232] Modified directories: branches/ruby_2_4/ Modified files: branches/ruby_2_4/ext/bigdecimal/bigdecimal.c branches/ruby_2_4/ext/bigdecimal/bigdecimal.gemspec branches/ruby_2_4/ext/bigdecimal/bigdecimal.h branches/ruby_2_4/ext/bigdecimal/extconf.rb branches/ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb branches/ruby_2_4/test/bigdecimal/test_bigdecimal_util.rb branches/ruby_2_4/version.h Index: ruby_2_4/test/bigdecimal/test_bigdecimal_util.rb =================================================================== --- ruby_2_4/test/bigdecimal/test_bigdecimal_util.rb (revision 62637) +++ ruby_2_4/test/bigdecimal/test_bigdecimal_util.rb (revision 62638) @@ -42,10 +42,19 @@ class TestBigDecimalUtil < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/ruby_2_4/test/bigdecimal/test_bigdecimal_util.rb#L42 end def test_Rational_to_d_with_zero_precision - assert_raise(ArgumentError) { 355.quo(113).to_d(0) } + assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d(0)) end def test_Rational_to_d_with_negative_precision assert_raise(ArgumentError) { 355.quo(113).to_d(-42) } end + + def test_String_to_d + assert_equal("2.5".to_d, BigDecimal.new('2.5')) + end + + def test_invalid_String_to_d + assert_equal("invalid".to_d, BigDecimal.new('0.0')) + end + end Index: ruby_2_4/ext/bigdecimal/bigdecimal.c =================================================================== --- ruby_2_4/ext/bigdecimal/bigdecimal.c (revision 62637) +++ ruby_2_4/ext/bigdecimal/bigdecimal.c (revision 62638) @@ -231,6 +231,7 @@ static inline VALUE BigDecimal_div2(VALU https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L231 static Real* GetVpValueWithPrec(VALUE v, long prec, int must) { + ENTER(1); Real *pv; VALUE num, bg; char szD[128]; @@ -296,6 +297,7 @@ again: https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L297 case T_BIGNUM: bg = rb_big2str(v, 10); + PUSH(bg); return VpCreateRbObject(strlen(RSTRING_PTR(bg)) + VpBaseFig() + 1, RSTRING_PTR(bg)); default: @@ -1319,25 +1321,14 @@ BigDecimal_divide(Real **c, Real **res, https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L1321 return Qnil; } - /* call-seq: - * div(value, digits) - * quo(value) - * - * Divide by the specified value. - * - * e.g. - * c = a.div(b,n) - * - * digits:: If specified and less than the number of significant digits of the - * result, the result is rounded to that number of digits, according - * to BigDecimal.mode. - * - * If digits is 0, the result is the same as the / operator. If not, the - * result is an integer BigDecimal, by analogy with Float#div. - * - * The alias quo is provided since <code>div(value, 0)</code> is the same as - * computing the quotient; see BigDecimal#divmod. - */ +/* call-seq: + * a / b -> bigdecimal + * quo(value) -> bigdecimal + * + * Divide by the specified value. + * + * See BigDecimal#div. + */ static VALUE BigDecimal_div(VALUE self, VALUE r) /* For c = self/r: with round operation */ @@ -1603,6 +1594,37 @@ BigDecimal_div2(VALUE self, VALUE b, VAL https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L1594 } } + /* + * Document-method: BigDecimal#div + * + * call-seq: + * div(value, digits) -> bigdecimal or integer + * + * Divide by the specified value. + * + * digits:: If specified and less than the number of significant digits of the + * result, the result is rounded to that number of digits, according + * to BigDecimal.mode. + * + * If digits is 0, the result is the same as for the / operator + * or #quo. + * + * If digits is not specified, the result is an integer, + * by analogy with Float#div; see also BigDecimal#divmod. + * + * Examples: + * + * a = BigDecimal("4") + * b = BigDecimal("3") + * + * a.div(b, 3) # => 0.133e1 + * + * a.div(b, 0) # => 0.1333333333333333333e1 + * a / b # => 0.1333333333333333333e1 + * a.quo(b) # => 0.1333333333333333333e1 + * + * a.div(b) # => 1 + */ static VALUE BigDecimal_div3(int argc, VALUE *argv, VALUE self) { @@ -3194,6 +3216,19 @@ get_vp_value: https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L3216 * Note also that in mathematics, there is no particular concept of negative * or positive zero; true mathematical zero has no sign. * + * == bigdecimal/util + * + * When you require +bigdecimal/util+, the #to_d method will be + * available on BigDecimal and the native Integer, Float, Rational, + * and String classes: + * + * require 'bigdecimal/util' + * + * 42.to_d # => 0.42e2 + * 0.5.to_d # => 0.5e0 + * (2/3r).to_d(3) # => 0.667e0 + * "0.5".to_d # => 0.5e0 + * * == License * * Copyright (C) 2002 by Shigeo Kobayashi <shigeo@t...>. @@ -5921,17 +5956,12 @@ Exit: https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.c#L5956 } /* - * - * nf: digit position for operation. - * - */ -VP_EXPORT int -VpMidRound(Real *y, unsigned short f, ssize_t nf) -/* * Round relatively from the decimal point. * f: rounding mode * nf: digit location to round from the decimal point. */ +VP_EXPORT int +VpMidRound(Real *y, unsigned short f, ssize_t nf) { /* fracf: any positive digit under rounding position? */ /* fracf_1further: any positive digits under one further than the rounding position? */ Index: ruby_2_4/ext/bigdecimal/bigdecimal.gemspec =================================================================== --- ruby_2_4/ext/bigdecimal/bigdecimal.gemspec (revision 62637) +++ ruby_2_4/ext/bigdecimal/bigdecimal.gemspec (revision 62638) @@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.gemspec#L1 # coding: utf-8 -_VERSION = '1.3.0' +_VERSION = '1.3.2' Gem::Specification.new do |s| s.name = "bigdecimal" @@ -33,4 +33,5 @@ Gem::Specification.new do |s| https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.gemspec#L33 s.add_development_dependency "rake", "~> 10.0" s.add_development_dependency "rake-compiler", "~> 0.9" s.add_development_dependency "minitest", "~> 4.7.5" + s.add_development_dependency "pry" end Index: ruby_2_4/ext/bigdecimal/bigdecimal.h =================================================================== --- ruby_2_4/ext/bigdecimal/bigdecimal.h (revision 62637) +++ ruby_2_4/ext/bigdecimal/bigdecimal.h (revision 62638) @@ -9,6 +9,8 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/bigdecimal.h#L9 #ifndef RUBY_BIG_DECIMAL_H #define RUBY_BIG_DECIMAL_H 1 +#define RUBY_NO_OLD_COMPATIBILITY + #include "ruby/ruby.h" #include <float.h> Index: ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb =================================================================== --- ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb (revision 62637) +++ ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb (revision 62638) @@ -1,72 +1,79 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb#L1 # frozen_string_literal: false -# BigDecimal extends the native Integer class to provide the #to_d method. # -# When you require the BigDecimal library in your application, this method will -# be available on Integer objects. +#-- +# bigdecimal/util extends various native classes to provide the #to_d method, +# and provides BigDecimal#to_d and BigDecimal#to_digits. +#++ + + class Integer < Numeric # call-seq: # int.to_d -> bigdecimal # - # Convert +int+ to a BigDecimal and return it. + # Returns the value of +int+ as a BigDecimal. # # require 'bigdecimal' # require 'bigdecimal/util' # - # 42.to_d - # # => 0.42e2 + # 42.to_d # => 0.42e2 + # + # See also BigDecimal::new. # def to_d BigDecimal(self) end end -# BigDecimal extends the native Float class to provide the #to_d method. -# -# When you require BigDecimal in your application, this method will be -# available on Float objects. + class Float < Numeric # call-seq: - # flt.to_d -> bigdecimal + # float.to_d -> bigdecimal + # float.to_d(precision) -> bigdecimal # - # Convert +flt+ to a BigDecimal and return it. + # Returns the value of +float+ as a BigDecimal. + # The +precision+ parameter is used to determine the number of + # significant digits for the result (the default is Float::DIG). # # require 'bigdecimal' # require 'bigdecimal/util' # - # 0.5.to_d - # # => 0.5e0 + # 0.5.to_d # => 0.5e0 + # 1.234.to_d(2) # => 0.12e1 + # + # See also BigDecimal::new. # def to_d(precision=nil) BigDecimal(self, precision || Float::DIG) end end -# BigDecimal extends the native String class to provide the #to_d method. -# -# When you require BigDecimal in your application, this method will be -# available on String objects. + class String # call-seq: - # string.to_d -> bigdecimal + # str.to_d -> bigdecimal # - # Convert +string+ to a BigDecimal and return it. + # Returns the result of interpreting leading characters in +str+ + # as a BigDecimal. # # require 'bigdecimal' # require 'bigdecimal/util' # - # "0.5".to_d - # # => 0.5e0 + # "0.5".to_d # => 0.5e0 + # "123.45e1".to_d # => 0.12345e4 + # "45.67 degrees".to_d # => 0.4567e2 + # + # See also BigDecimal::new. # def to_d - BigDecimal(self) + begin + BigDecimal(self) + rescue ArgumentError + BigDecimal(0) + end end end -# BigDecimal extends the native Numeric class to provide the #to_digits and -# #to_d methods. -# -# When you require BigDecimal in your application, this method will be -# available on BigDecimal objects. + class BigDecimal < Numeric # call-seq: # a.to_digits -> string @@ -74,12 +81,11 @@ class BigDecimal < Numeric https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb#L81 # Converts a BigDecimal to a String of the form "nnnnnn.mmm". # This method is deprecated; use BigDecimal#to_s("F") instead. # - # require 'bigdecimal' # require 'bigdecimal/util' # # d = BigDecimal.new("3.14") - # d.to_digits - # # => "3.14" + # d.to_digits # => "3.14" + # def to_digits if self.nan? || self.infinite? || self.zero? self.to_s @@ -94,35 +100,35 @@ class BigDecimal < Numeric https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/lib/bigdecimal/util.rb#L100 # a.to_d -> bigdecimal # # Returns self. + # + # require 'bigdecimal/util' + # + # d = BigDecimal.new("3.14") + # d.to_d # => 0.314e1 + # def to_d self end end -# BigDecimal extends the native Rational class to provide the #to_d method. -# -# When you require BigDecimal in your application, this method will be -# available on Rational objects. + class Rational < Numeric # call-seq: - # r.to_d(precision) -> bigdecimal + # rat.to_d(precision) -> bigdecimal # - # Converts a Rational to a BigDecimal. + # Returns the value as a BigDecimal. + # + # The required +precision+ parameter is used to determine the number of + # significant digits for the result. + # + # require 'bigdecimal' + # require 'bigdecimal/util' + # + # Rational(22, 7).to_d(3) # => 0.314e1 + # + # See also BigDecimal::new. # - # The required +precision+ parameter is used to determine the amount of - # significant digits for the result. See BigDecimal#div for more information, - # as it is used along with the #denominator and the +precision+ for - # parameters. - # - # r = (22/7.0).to_r - # # => (7077085128725065/2251799813685248) - # r.to_d(3) - # # => 0.314e1 def to_d(precision) - if precision <= 0 - raise ArgumentError, "negative precision" - end - num = self.numerator - BigDecimal(num).div(self.denominator, precision) + BigDecimal(self, precision) end end Index: ruby_2_4/ext/bigdecimal/extconf.rb =================================================================== --- ruby_2_4/ext/bigdecimal/extconf.rb (revision 62637) +++ ruby_2_4/ext/bigdecimal/extconf.rb (revision 62638) @@ -14,17 +14,4 @@ have_func("rb_rational_den", "ruby.h") https://github.com/ruby/ruby/blob/trunk/ruby_2_4/ext/bigdecimal/extconf.rb#L14 have_func("rb_array_const_ptr", "ruby.h") have_func("rb_sym2str", "ruby.h") -have_macro("FIX_CONST_VALUE_PTR", "ruby.h") -have_macro("RARRAY_CONST_PTR", "ruby.h") -have_macro("RARRAY_AREF", "ruby.h") - create_makefile('bigdecimal') - -# Add additional dependencies -open('Makefile', 'a') do |io| - if RUBY_VERSION >= '2.4' - io.puts <<-MAKEFILE -bigdecimal.o: $(hdrdir)/ruby/backward.h - MAKEFILE - end -end Index: ruby_2_4/version.h =================================================================== --- ruby_2_4/version.h (revision 62637) +++ ruby_2_4/version.h (revision 62638) @@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/version.h#L1 #define RUBY_VERSION "2.4.4" -#define RUBY_RELEASE_DATE "2018-03-02" -#define RUBY_PATCHLEVEL 242 +#define RUBY_RELEASE_DATE "2018-03-03" +#define RUBY_PATCHLEVEL 243 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 3 -#define RUBY_RELEASE_DAY 2 +#define RUBY_RELEASE_DAY 3 #include "ruby/version.h" Index: ruby_2_4 =================================================================== --- ruby_2_4 (revision 62637) +++ ruby_2_4 (revision 62638) Property changes on: ruby_2_4 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r57597,57951 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/