summaryrefslogtreecommitdiff
path: root/rational.c
AgeCommit message (Collapse)Author
2017-10-19freeze Complex and Rationalnobu
* complex.c (nucomp_s_new_internal, nucomp_loader): Complex instances are always frozen now. [Feature #13983] * rational.c (nurat_s_new_internal, nurat_loader): Rational instances are always frozen now. [Feature #13983] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60214 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-31Improve performance of implicit type conversionwatson1978
To convert the object implicitly, it has had two parts in convert_type() which are 1. lookink up the method's id 2. calling the method Seems that strncmp() and strcmp() in convert_type() are slightly heavy to look up the method's id for type conversion. This patch will add and use internal APIs (rb_convert_type_with_id, rb_check_convert_type_with_id) to call the method without looking up the method's id when convert the object. Array#flatten -> 19 % up Array#+ -> 3 % up [ruby-dev:50024] [Bug #13341] [Fix GH-1537] ### Before Array#flatten 104.119k (± 1.1%) i/s - 525.690k in 5.049517s Array#+ 1.993M (± 1.8%) i/s - 10.010M in 5.024258s ### After Array#flatten 124.005k (± 1.0%) i/s - 624.240k in 5.034477s Array#+ 2.058M (± 4.8%) i/s - 10.302M in 5.019328s ### Test Code require 'benchmark/ips' class Foo def to_ary [1,2,3] end end Benchmark.ips do |x| ary = [] 100.times { |i| ary << i } array = [ary] x.report "Array#flatten" do |i| i.times { array.flatten } end x.report "Array#+" do |i| obj = Foo.new i.times { array + obj } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58978 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-27Improve performance of some Time & Rational methodswatson1978
rational.c (i_gcd): replace GCD algorithm from Euclidean algorithm to Stein algorithm (https://en.wikipedia.org/wiki/Binary_GCD_algorithm). Some Time methods will call internal quov() function and it calls Rational#quo -> f_muldiv() -> i_gcd() in rational.c And some Rational methods also call i_gcd(). The implementation of Euclidean algorithm spent a long time at modulo operation (ie "x = y % x;"). The Stein algorithm will replace with shift operation which is faster than modulo. Time#subsec -> 36 % up Time#to_r -> 26 % up Rational#+ -> 14 % up Rational#- -> 15 % up Rational#* -> 13 % up [ruby-core:80843] [Bug #13503] [Fix GH-1596] ### Before Time#subsec 2.142M (± 9.8%) i/s - 10.659M in 5.022659s Time#to_r 2.003M (± 9.1%) i/s - 9.959M in 5.012445s Rational#+ 3.843M (± 0.9%) i/s - 19.274M in 5.016254s Rational#- 3.820M (± 1.3%) i/s - 19.149M in 5.014137s Rational#* 5.198M (± 1.4%) i/s - 26.016M in 5.005664s * After Time#subsec 2.902M (± 2.9%) i/s - 14.505M in 5.001815s Time#to_r 2.503M (± 4.8%) i/s - 12.512M in 5.011454s Rational#+ 4.390M (± 1.2%) i/s - 22.001M in 5.012413s Rational#- 4.391M (± 1.2%) i/s - 22.013M in 5.014584s Rational#* 5.872M (± 2.2%) i/s - 29.369M in 5.003666s * Test code require 'benchmark/ips' Benchmark.ips do |x| x.report "Time#subsec" do |t| time = Time.now t.times { time.subsec } end x.report "Time#to_r" do |t| time = Time.now t.times { time.to_r } end x.report "Rational#+" do |t| rat1 = 1/2r rat2 = 1/3r t.times { rat1 + rat2 } end x.report "Rational#-" do |t| rat1 = 1/3r rat2 = 1/2r t.times { rat1 - rat2 } end x.report "Rational#*" do |t| rat1 = 1/3r rat2 = 1/2r t.times { rat1 * rat2 } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-27Improve performance of some Time methodswatson1978
internal.h : add rb_numeric_quo() as internal API. rational.c : rename numeric_quo() to rb_numeric_quo() as internal API. time.c (quov): optimize by invoking rb_numeric_quo() to retrieve a value of Numeric#quo instead of method dispatching via rb_funcall(). Time#subsec -> 7 % up Time#- -> 26 % up Time#to_f -> 30 % up Time#to_r -> 7 % up [ruby-core:80915] [Bug #13519] [Fix GH-1601] ### Before Time#subsec 2.024M (± 8.7%) i/s - 10.062M in 5.009762s Time#- 5.049M (± 4.7%) i/s - 25.186M in 5.002379s Time#to_f 5.625M (± 4.2%) i/s - 28.066M in 5.000749s Time#to_r 1.880M (± 9.7%) i/s - 9.361M in 5.027527s ### After Time#subsec 2.155M (± 9.7%) i/s - 10.724M in 5.022579s Time#- 6.362M (± 2.0%) i/s - 31.824M in 5.004625s Time#to_f 7.287M (± 4.8%) i/s - 36.402M in 5.010983s Time#to_r 2.020M (± 9.4%) i/s - 10.059M in 5.021852s ### Test code require 'benchmark/ips' Benchmark.ips do |x| x.report "Time#subsec" do |t| time = Time.now t.times { time.subsec } end x.report "Time#-" do |t| time1 = Time.now time2 = Time.now t.times { time1 - time2 } end x.report "Time#to_f" do |t| time = Time.now t.times { time.to_f } end x.report "Time#to_r" do |t| time = Time.now t.times { time.to_r } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58921 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-01rational.c: canonicalization casenobu
* rational.c (float_numerator, float_denominator): fix for canonicalization case where `Float#to_r` could return an Integer not a Rational. although mathn.rb has been removed in the trunk, fix for the backport purpose. [ruby-core:80942] [Bug #13528] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58523 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-03improve docs for #truncate, #floor, and #ceil methodsstomar
* numeric.c: [DOC] improve and harmonize documentation for {Float,Integer,Numeric}#{truncate,floor,ceil}. * rational.c: [DOC] ditto for Rational#{truncate,floor,ceil}. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-03improve docs for #round methodsstomar
* numeric.c: [DOC] improve and harmonize documentation for {Float,Integer,Numeric}#round. * rational.c: [DOC] ditto for Rational#round. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58242 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-01rational.c: improves Rational#round rdoc [ci skip]nobu
* rational.c (nurat_round_n): [DOC] improves Integer#round documentation as well as Float#round. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-01rational.c: improve docsstomar
* rational.c: [DOC] improve docs for Rational and related methods * improve class documentation for Rational * fix call-seq's * simplify examples for Rational#{floor,ceil,truncate,round} * fix wrong examples for #floor, subtraction, and exponentiation * improve docs for #<=>, Kernel#Rational, {String,Float}#to_r, Integer.{gcd,lcm,gcdlcm} * fix typos, grammar, and rdoc formatting * other improvements git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-16rational.c: initialize nnobu
* rational.c (read_num): `n` was used uninitialized when the string started with a period. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-16rational.c: float denomnobu
* rational.c (parse_rat): allow float as a denominator as well as a numerator. [ruby-core:79104] [Bug #13134] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-16rational.c: read_numnobu
* rational.c (read_num): use rb_int_parse_cstr to parse integer parts, and make String#to_r consistent with #to_i and #to_f. [ruby-core:80098] [Bug #13105] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57989 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-16rational.c: zero divisionnobu
* rational.c (read_rat_nos): denominator cannot be 0, raise zero division in that case. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-22rational.c: infinity in powernobu
* rational.c (nurat_expt): return 0 due to overflow. [ruby-core:79686] [Bug #13242]: git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57689 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-22rational.c: infinity in powernobu
* rational.c (nurat_expt): return Infinity due to overflow. [ruby-core:79686] [Bug #13242]: git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-22rational.c: fix rdockazu
* rational.c: [DOC] fix wrong indentations and comment out some lines in code examples to make them valid Ruby code and syntax highlighted on the rendered page. [ci skip] [ruby-core:79607] [Bug #13233] Author: Marcus Stollsteimer <sto.mar@web.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57686 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-05rational.c: fix rdoc [ci skip]nobu
* rational.c (rb_rational_plus): [DOC] fix an example. A patch by Trygve Flathen <at.ruby-lang AT flathen.net> in [ruby-core:71755]. [Bug #11752] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57539 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-10rational.c: short circuit optimizationnobu
* rational.c (nurat_reduce): short circuit when arguments are ONE, nothing is needed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57299 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-10rational.c: f_idivnobu
* rational.c (f_idiv): call rb_int_idiv directly if possible. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-30rational.c: memory leak in gcdnobu
* rational.c (rb_gcd_gmp): fix memory leak. patched by KISHIMOTO, Makoto <ksmakoto AT dd.iij4u.or.jp> in [ruby-dev:49934]. [Bug #13089] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-29rational.c: refactor to_rnobu
* rational.c (read_num, read_rat_nos): refactor to curtail creating Rational objects. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57233 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-29rational.c: fix for mathnnobu
* rational.c (read_num, read_rat_nos): dispatch by the type of numerator, for mathn. [ruby-core:78893] [Bug #13084] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-29rational.c: canonicalizationnobu
* rational.c (canonicalization): define always regardless CANON, and remove unnecessary ifdefs. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-25round-downnobu
* numeric.c (round_half_down, int_round_half_down): support round-down mode. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-22complex.c: optimize f_gt_p some casesmrkn
* complex.c (f_gt_p): optimize f_gt_p for specific types of arguments. * internal.h (rb_int_gt, rb_float_gt, rb_rational_cmp): exported. * numeric.c (rb_float_gt): rename from flo_gt and be exported. * numeric.c (rb_int_gt): rename from int_gt and be exported. * rational.c (rb_rational_cmp): rename from nurat_cmp and be exported. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56872 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-18complex.c: optimize f_negatemrkn
* complex.c (f_negate): optimize for special numeric types. * complex.c (nucomp_expt): use rb_int_uminus instead of f_negate for fixnum value. * internal.h (rb_float_uminus, rb_rational_uminus): exported. * numeric.c (rb_float_uminus): rename from flo_uminus. * rational.c (rb_rational_uminus): rename from nurat_negate, and add assertion for the parameter. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56830 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-18rational.c: optimization and refactoringmrkn
* rational.c (nurat_s_new_bang, nurat_canonicalize): small optimization by using rb_int_uminus instead of f_negate. * rational.c (nurat_canonicalize): add assertions for parameters. * rational.c (f_negate, id_negate): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56828 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-18internal.h: round macrosnobu
* internal.h (ROUND_FUNC, ROUND_CALL): macros wrapping round functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56824 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-16complex.c: optimize Numeric#polar and Numeric#argmrkn
* complex.c (numeric_polar): optimize for Integer, Float, and Rational. * complex.c (numeric_arg): directly create the value of pi. * complex.c (f_negative_p): optimize for Integer, Float, and Rational. * rational.c (INT_NEGATIVE_P): move the definition into internal.h. * internal.h (INT_NEGATIVE_P): ditto. * numeric.c (rb_float_abs): rename from flo_abs and export to be used from other source files.. * internal.h (rb_float_abs): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-16rational.c: optimize Rational#absmrkn
* rational.c (rb_rational_abs): optimize Rational#abs with the specialized implementation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-14rational.c: cast to intnobu
* rational.c (f_kind_of_p): rb_obj_is_kind_of returns Qtrue or Qfalse always, and is safe to cast down to int. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56775 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-13purge id_eqeq_pnobu
* rational.c (f_{eqeq,zero,one,minus_one}_p, nurat_eqeq_p): use rb_equal. this function returns Qtrue or Qfalse always, so it is safe to cast down to int. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56763 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: refactoringmrkn
* rational.c: use RB_INTEGER_TYPE_P, RB_FLOAT_TYPE_P, and FIXNUM_ZERO_P. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56761 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: remove f_negative_pmrkn
* rational.c (f_negative_p): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56760 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Integer#lcmmrkn
* rational.c (f_div, f_mul, f_abs): optimize Integer#lcm Author: Tadashi Saito <tad.a.digger@gmail.com> * numeric.c (rb_int_abs): rename from int_abs to be exported. * internal.h (rb_int_div, rb_int_abs): exported. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56759 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize (-rational).rationalize(some)mrkn
* rational.c (nurat_rationalize): optimize (-rational).rationalize(some). Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56758 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: define Rational#{negative?,positive?}mrkn
* rational.c (nurat_{negative,positive}_p): define Rational#negative? and Rational#positive?, respectively. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: refactoringmrkn
* rational.c (id_expt, id_fdiv, f_expt, f_fdiv, f_positive_p): removed. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56755 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12raitonal.c: remove needless macromrkn
* rational.c (id_lshift, f_lshift): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56753 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Float#rationalizemrkn
* rational.c (rb_flt_rationalize{,_with_prec},float_rationalize): optimize Float#rationalize. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56752 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: use rb_num_zerodiv instead of rb_raise_zerodivmrkn
* rational.c (rb_raize_zerodiv): replace by rb_num_zerodiv. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56751 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Float#to_rmrkn
* rational.c (float_to_r): optimize Float#to_r. * numeric.c (rb_int_lshift): exported. * internal.h (rb_int_lshift): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56750 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Float#{numerator,denominator}mrkn
* rational.c (float_{numerator,denominator}): optimize Float#{numerator,denominator}. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56749 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Numeric#quomrkn
* rational.c (numeric_quo): optimize Numeric#quo. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56748 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: refactor by removing needless ID varsmrkn
* rational.c (id_cmp, id_trunate): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56745 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Rational#fdivmrkn
* rational.c (nurat_fdiv): optimize Rational#fdiv. Author: Tadashi Saito <tad.a.digger@gmail.com> * rational.c (f_to_f, id_to_f): removed. * rational.c (f_expt): only used when FLT_RADIX is not 2. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Rational#to_imrkn
* rational.c (nurat_truncate): optimize Rational#to_i. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize Rational#{floor,ceil,round,truncate}mrkn
* rational.c (f_{expt10,round_common},nurat_{floor,ceil,round_half_{up,even}}): optimize Rational#{floor,ceil,round,truncate}. Author: Tadashi Saito <tad.a.digger@gmail.com> * numeric.c (rb_int_divmod): rename from int_divmod to be exported. * numeric.c (rb_int_and): rename from int_and to be exported. * intern.h (rb_int_{divmod,and}): exported. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56742 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize rational.coerce(float) and float [+-*/] rationalmrkn
* rational.c (nurat_coerce): optimize rational.coerce(float). note that this makes `float [+-*/] rational` faster Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-12rational.c: optimize construction of Rationalmrkn
* rational.c (nurat_{s_new_bang,canonicalize,f_rational,s_convert}): optimize construction of Rational from numbers. Author: Tadashi Saito <tad.a.digger@gmail.com> * rational.c (read_{num,rat_nos}): optimize construction of Rational from string. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e