long-decimal 1.00.03 → 1.00.04

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4d758878cf5a3d2d5f20a4738e94a261114da04
4
- data.tar.gz: 3db2e6b11a3f8aae44a7cccfc81ea902165ec4bb
3
+ metadata.gz: 1698251d167db9e4042ac9028306ddd349d91ef4
4
+ data.tar.gz: cb895fd675267f4b93074aa79e5abebcca4dbbb0
5
5
  SHA512:
6
- metadata.gz: 8aa62b76ff524e76b9c2d38dbb5cd660c38dfd1fdae8f575312da56ecf7438875860fdd1fae605ba2dc04409fc252a67fcdb596117d04b2784e724657d20e425
7
- data.tar.gz: 4dde6badc8da94abaef63c8151a13452dda84658a41bb83bd5efc219d36e3a03cb32b6999ec20ef28f91392d11b2dbba5dc7ddcb0ef32f06b5531e75cb346519
6
+ metadata.gz: 97dc3cd8924ed56c0e8a284e4d1b8c8b4ee4a17f7b4bbbce15670078fa6383746c236309206f5985d6405ac3862742108fc9121acae3a37ebe7a1cdc902a3bc8
7
+ data.tar.gz: 3cb4fd2b5e2eb0926ef1d82994fa47f5910b7d097440f372268305345f04e451f64c8bb931e7f94a9d8c90f070fd20826dbf0d7a3a17c626e622f2a04ea1f95f
data/README CHANGED
@@ -1,13 +1,18 @@
1
1
  Version
2
2
  -------
3
3
 
4
- This version (1.00.03) is fixing bugs from version 1.00.02.
4
+ This version (1.00.04) is fixing bugs from version 1.00.03.
5
5
  It can be considered as a release candidate from the upcoming
6
6
  version 1.01.00.
7
7
 
8
8
  Change Log
9
9
  ----------
10
10
 
11
+ Improvements in 1.00.04
12
+ - tests improved
13
+ - tested against 1.8.7, 1.9.3, 2.0.0, 2.1.1 and 2.2.0
14
+ - some minor bugfixes concerning Complex
15
+
11
16
  Improvements in 1.00.03
12
17
  - tests improved
13
18
  - bug fixes in mean calculation functionality for non-LongDecimal paramters
@@ -103,7 +108,7 @@ require 'long-decimal'
103
108
  Test
104
109
  ----
105
110
 
106
- Tests have been run with Ruby 1.8.7, 1.9.3, 2.0.0 and 2.1.1
111
+ Tests have been run with Ruby 1.8.7, 1.9.3, 2.0.0, 2.1.1 and 2.2.0
107
112
  Some tests do not work with 1.8.7 because of missing language features.
108
113
  They are skipped. No major effort for adapting tests to this version
109
114
  will be allocated.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
7
7
  #
8
- # TAG: $TAG v1.00.03$
8
+ # TAG: $TAG v1.00.04$
9
9
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/Rakefile,v 1.4 2009/04/15 19:29:37 bk1 Exp $
10
10
  # CVS-Label: $Name: $
11
11
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -7,7 +7,7 @@
7
7
  #
8
8
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
9
9
  #
10
- # TAG: $TAG v1.00.03$
10
+ # TAG: $TAG v1.00.04$
11
11
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/lib/long-decimal-extra.rb,v 1.29 2011/02/04 23:17:21 bk1 Exp $
12
12
  # CVS-Label: $Name: $
13
13
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -470,12 +470,12 @@ def LongMath.continued_fraction(x, steps)
470
470
  arr = []
471
471
  steps.times do
472
472
  xi = x.to_ld(0, LongMath::ROUND_FLOOR)
473
+ arr.push(xi.to_i)
473
474
  xd = x-xi
474
475
  if xd.zero?
475
476
  return arr
476
477
  end
477
478
  x = 1/xd
478
- arr.push(xi.to_i)
479
479
  end
480
480
  arr
481
481
  end
@@ -7,7 +7,7 @@
7
7
  # additional functionality, mostly transcendental functions,
8
8
  # may be found in long-decimal-extra.rb
9
9
  #
10
- # TAG: $TAG v1.00.03$
10
+ # TAG: $TAG v1.00.04$
11
11
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/lib/long-decimal.rb,v 1.87 2011/01/30 20:01:40 bk1 Exp $
12
12
  # CVS-Label: $Name: $
13
13
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -766,7 +766,7 @@ class LongDecimalBase < Numeric
766
766
  @@RCS_ID='-$Id: long-decimal.rb,v 1.87 2011/01/30 20:01:40 bk1 Exp $-'
767
767
 
768
768
  # allow easy check if running with version 1.9
769
- RUNNING_19 = RUBY_VERSION.match /^1\.9/
769
+ RUNNING_AT_LEAST_19 = RUBY_VERSION.match /^(1\.9|[2-9]\.)/
770
770
 
771
771
  include LongDecimalRoundingMode
772
772
 
@@ -2223,7 +2223,7 @@ class LongDecimal < LongDecimalBase
2223
2223
  # has too many digits before the decimal point to be expressed
2224
2224
  # as Float.
2225
2225
  # s, o = other.coerce(self.to_f)
2226
- if (RUNNING_19)
2226
+ if (RUNNING_AT_LEAST_19)
2227
2227
  s, o = other.coerce(self)
2228
2228
  # puts "complex coerce 19: #{self}, #{other} -> #{s}, #{o}"
2229
2229
  return o, s
@@ -2885,7 +2885,7 @@ class Complex
2885
2885
  p = self*other.conjugate
2886
2886
  d = other.abs2
2887
2887
  r = p.real/d
2888
- i = p.image/d
2888
+ i = p.imag/d
2889
2889
  Complex(r, i)
2890
2890
  elsif Complex.generic?(other)
2891
2891
  Complex(@real/other, @image/other)
@@ -2896,9 +2896,9 @@ class Complex
2896
2896
  x/y
2897
2897
  end
2898
2898
  end
2899
- else
2899
+ elsif (RUBY_VERSION.match /^1\.9/) || (RUBY_VERSION.match /^2\.[0-9]/)
2900
2900
  #
2901
- # Division by real or complex number for Ruby 1.9
2901
+ # Division by real or complex number for Ruby >=1.9
2902
2902
  #
2903
2903
  def / (other)
2904
2904
  if other.kind_of?(Complex)
@@ -2911,13 +2911,13 @@ class Complex
2911
2911
  else
2912
2912
  r = p.real/d
2913
2913
  end
2914
- if (d.kind_of? Integer) && (p.image.kind_of? Integer)
2915
- i = Rational(p.image, d)
2914
+ if (d.kind_of? Integer) && (p.imag.kind_of? Integer)
2915
+ i = Rational(p.imag, d)
2916
2916
  else
2917
- i = p.image/d
2917
+ i = p.imag/d
2918
2918
  end
2919
2919
  Complex(r, i)
2920
- elsif Complex.generic?(other)
2920
+ elsif other.kind_of? Integer
2921
2921
  r = nil
2922
2922
  i = nil
2923
2923
  if (other.kind_of? Integer) && (real.kind_of? Integer)
@@ -2925,14 +2925,14 @@ class Complex
2925
2925
  else
2926
2926
  r = real/other
2927
2927
  end
2928
- if (other.kind_of? Integer) && (image.kind_of? Integer)
2929
- i = Rational(image, other)
2928
+ if (other.kind_of? Integer) && (imag.kind_of? Integer)
2929
+ i = Rational(imag, other)
2930
2930
  else
2931
- i = image/other
2931
+ i = imag/other
2932
2932
  end
2933
2933
  Complex(r, i)
2934
- elsif other.kind_of? BigDecimal
2935
- Complex(real/other, image/other)
2934
+ elsif (other.kind_of? BigDecimal) || (other.kind_of? Float) || (other.kind_of? LongDecimal) || (other.kind_of? Rational)
2935
+ Complex(real/other, imag/other)
2936
2936
  else
2937
2937
  x, y = other.coerce(self)
2938
2938
  x/y
@@ -1,3 +1,3 @@
1
1
  module LongDecimalSupport
2
- VERSION = "1.00.03"
2
+ VERSION = "1.00.04"
3
3
  end
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/bk1/ruby-long-decimal"
14
14
  spec.license = "LGPL or Ruby"
15
15
  spec.files = "./Gemfile", "./LICENSE", "./README", "./Rakefile", "./lib/long-decimal-extra.rb", "./lib/long-decimal.rb", "./test/testlongdecimal.rb", "./test/testlongdeclib.rb", "./test/testrandlib.rb", "./test/testrandom.rb", "./test/testrandpower.rb", "./tex/long-decimal.pdf", "./tex/long-decimal.tex", "./long-decimal.gemspec", "./lib/long-decimal/version.rb"
16
- spec.executables =
16
+ spec.executables =
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
@@ -4,9 +4,7 @@
4
4
  #
5
5
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
6
6
  #
7
- # TAG: $TAG v1.00.03$
8
- # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testlongdecimal.rb,v 1.86 2011/01/16 22:16:59 bk1 Exp $
9
- # CVS-Label: $Name: $
7
+ # TAG: $TAG v1.00.04$
10
8
  # Author: $Author: bk1 $ (Karl Brodowsky)
11
9
  #
12
10
 
@@ -45,8 +43,6 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
45
43
  include TestLongDecHelper
46
44
  include LongDecimalRoundingMode
47
45
 
48
- @RCS_ID='-$Id: testlongdecimal.rb,v 1.86 2011/01/16 22:16:59 bk1 Exp $-'
49
-
50
46
  #
51
47
  # test split_to_words and merge_from_words
52
48
  #
@@ -1149,20 +1145,20 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
1149
1145
  z = x/y
1150
1146
  msg = "x=#{x} y=#{y} z=#{z} xr=#{xr.inspect} (#{tw_c}) xi=#{xi.inspect} (#{fi_c}) yr=#{yr.inspect} (#{th_c}) yi=#{yi.inspect} (#{fo_c})"
1151
1147
  # puts msg
1152
- if (xr.kind_of? Integer) && (xi.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) # && ! LongDecimal::RUNNING_19
1148
+ if (xr.kind_of? Integer) && (xi.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) # && ! LongDecimal::RUNNING_AT_LEAST_19
1153
1149
  # ruby 1.9 creates rational result even from integers, ruby 1.8 uses integers as results.
1154
1150
  zc = (x.cdiv y)
1155
1151
  assert_equal_complex(zc, z, "all int zc=#{zc} " + msg)
1156
1152
  next
1157
1153
  end
1158
1154
  unless has_bigdecimal
1159
- assert_equal_complex(zr, z, msg)
1155
+ assert_equal_complex(zr, z, msg, 1e-15)
1160
1156
  end
1161
1157
  unless has_rational
1162
- assert_equal_complex(zb, z, msg)
1158
+ assert_equal_complex(zb, z, msg, 1e-15)
1163
1159
  end
1164
- assert_equal_complex(zf, z, msg)
1165
- assert_equal_complex(zl, z, msg)
1160
+ assert_equal_complex(zf, z, msg, 1e-15)
1161
+ assert_equal_complex(zl, z, msg, 1e-15)
1166
1162
  unless (xr.kind_of? LongDecimalBase) || (xi.kind_of? LongDecimalBase) || (yr.kind_of? LongDecimalBase) || (yi.kind_of? LongDecimalBase)
1167
1163
  zc = x.cdiv(y)
1168
1164
  assert_equal_complex(z, zc, msg, 1.0e-15)
@@ -1229,12 +1225,17 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
1229
1225
  threes = [ 3, 3.0, BigDecimal("3.0"), Rational(3, 1), LongDecimal("3.0"), LongDecimalQuot(3, 1) ]
1230
1226
  fours = [ 4, 4.0, BigDecimal("4.0"), Rational(4, 1), LongDecimal("4.0"), LongDecimalQuot(4, 1) ]
1231
1227
  zr = Complex(Rational(24,10),Rational(-32,10))
1228
+ deep_freeze_complex(zr)
1232
1229
  zb = Complex(BigDecimal("2.4"),BigDecimal("-3.2"))
1230
+ deep_freeze_complex(zb)
1233
1231
  zf = Complex(2.4,-3.2)
1232
+ deep_freeze_complex(zf)
1234
1233
  zl = Complex(LongDecimal("2.4"),LongDecimal("-3.2"))
1234
+ deep_freeze_complex(zl)
1235
1235
  zi = nil
1236
1236
  if ($test_type == :v20)
1237
1237
  zi = Complex(2, -4)
1238
+ deep_freeze_complex(zi)
1238
1239
  else
1239
1240
  zi = zr
1240
1241
  end
@@ -1248,12 +1249,14 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
1248
1249
  next
1249
1250
  end
1250
1251
  y = Complex(yr, yi)
1252
+ # puts "x=#{x} y=#{y}"
1251
1253
  z = x/y
1252
- msg = "x=#{x} y=#{y} z=#{z} yr=#{yr.inspect} yi=#{yi.inspect} x=#{x.inspect}"
1253
- if (x.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) && LongDecimal::RUNNING_19
1254
+ # puts "x=#{x} y=#{y} z=#{z}"
1255
+ msg = "x=#{x} y=#{y} z=#{z} -- zr=#{zr} zb=#{zb} zf=#{zf} zl=#{zl} zi=#{zi} -- yr=#{yr.inspect} yi=#{yi.inspect} x=#{x.inspect}"
1256
+ if (x.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) && LongDecimal::RUNNING_AT_LEAST_19
1254
1257
  has_rational # quotients of ints within Complex are Rational in 1.9
1255
1258
  end
1256
- if (x.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) && ! LongDecimal::RUNNING_19
1259
+ if (x.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) && ! LongDecimal::RUNNING_AT_LEAST_19
1257
1260
  assert_equal_complex(zi, z, msg)
1258
1261
  next
1259
1262
  end
@@ -1270,6 +1273,36 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
1270
1273
  end
1271
1274
  end
1272
1275
 
1276
+ #
1277
+ # test division of Complex, which is overriden in long-decimal.rb
1278
+ # due to bug 1454/1455 (redmine.ruby-lang.org) mixing of BigDecimal with Rational or Complex won't work.
1279
+ #
1280
+ def test_real_by_complex_div_single_case
1281
+ print "\ntest_real_by_complex_div [#{Time.now}]: "
1282
+ zr = Complex(Rational(24,10),Rational(-32,10))
1283
+ zb = Complex(BigDecimal("2.4"),BigDecimal("-3.2"))
1284
+ zf = Complex(2.4,-3.2)
1285
+ zl = Complex(LongDecimal("2.4"),LongDecimal("-3.2"))
1286
+ zi = nil
1287
+ if ($test_type == :v20)
1288
+ zi = Complex(2, -4)
1289
+ else
1290
+ zi = zr
1291
+ end
1292
+ x = 20.0
1293
+ yr = 3
1294
+ yi = 4.0
1295
+ has_rational = false
1296
+ has_bigdecimal = false
1297
+ y = Complex(yr, yi)
1298
+ z = x/y
1299
+ msg = "x=#{x} y=#{y} z=#{z} -- zr=#{zr} zb=#{zb} zf=#{zf} zl=#{zl} zi=#{zi} -- yr=#{yr.inspect} yi=#{yi.inspect} x=#{x.inspect}"
1300
+ assert_equal_complex(zr, z, msg)
1301
+ assert_equal_complex(zb, z, msg)
1302
+ assert_equal_complex(zf, z, msg)
1303
+ assert_equal_complex(zl, z, msg)
1304
+ end
1305
+
1273
1306
  #
1274
1307
  # test multiplicity_of_factor for rationals
1275
1308
  #
@@ -3892,7 +3925,7 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
3892
3925
 
3893
3926
  y = Complex(5, 3)
3894
3927
  z = x + y
3895
- # if (LongDecimalBase::RUNNING_19)
3928
+ # if (LongDecimalBase::RUNNING_AT_LEAST_19)
3896
3929
  zz = Complex(LongDecimal(724, 2), 3)
3897
3930
  # else
3898
3931
  # zz = Complex(7.24, 3)
@@ -3972,7 +4005,7 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
3972
4005
 
3973
4006
  y = Complex(5, 3)
3974
4007
  z = x - y
3975
- #if (LongDecimalBase::RUNNING_19)
4008
+ #if (LongDecimalBase::RUNNING_AT_LEAST_19)
3976
4009
  zz = Complex(LongDecimal(-276, 2), -3)
3977
4010
  #else
3978
4011
  # zz = Complex(-2.76, -3)
@@ -4672,7 +4705,7 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
4672
4705
  x = LongDecimal(224, 2) # 2.24 dx=1 sx=2
4673
4706
 
4674
4707
  y = Complex(5, 3)
4675
- puts("x=#{x.inspect} y=#{y.inspect}")
4708
+ # puts("x=#{x.inspect} y=#{y.inspect}")
4676
4709
  z = x.divide(y, ROUND_DOWN)
4677
4710
  zz = 2.24 / Complex(5, 3)
4678
4711
  assert_kind_of(Complex, z, "z=#{z.inspect}")
@@ -6645,14 +6678,231 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
6645
6678
  assert_equal("227/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
6646
6679
  end
6647
6680
 
6681
+ #
6682
+ # test rounding with ROUND_GEOMETRIC_*
6683
+ #
6684
+ def test_round_to_scale_geometric_common_ldq
6685
+ print "\ntest_round_to_scale_geometric_common [#{Time.now}]: "
6686
+ ALL_ROUNDING_MODES.each do |rounding_mode|
6687
+ unless (rounding_mode.major == MAJOR_GEOMETRIC)
6688
+ next
6689
+ end
6690
+
6691
+ # close to sqrt(2), but > sqrt(2)
6692
+ l = LongDecimalQuot(Rational(175568277047523, 124145519261542), 0)
6693
+ r = l.round_to_scale(0, rounding_mode)
6694
+ assert_equal(2, r, "l=#{l.inspect} r=#{r.inspect}")
6695
+ assert_kind_of(LongDecimal, r, "must be LongDecimal")
6696
+
6697
+ # close to sqrt(2), but < sqrt(2)
6698
+ l = LongDecimalQuot(Rational(72722761475561,51422757785981), 0)
6699
+ r = l.round_to_scale(0, rounding_mode)
6700
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6701
+ assert_kind_of(LongDecimal, r, "must be LongDecimal")
6702
+
6703
+ # close to sqrt(6), but > sqrt(6)
6704
+ l = LongDecimalQuot(Rational(4122760103977442405, 1683109764441408998), 0)
6705
+ r = l.round_to_scale(0, rounding_mode)
6706
+ assert_equal(3, r, "l=#{l.inspect} r=#{r.inspect}")
6707
+ assert_kind_of(LongDecimal, r, "must be LongDecimal")
6708
+
6709
+ # close to sqrt(6), but < sqrt(6)
6710
+ l = LongDecimalQuot(Rational(1853138378693569178, 756540575094624409), 0)
6711
+ r = l.round_to_scale(0, rounding_mode)
6712
+ assert_equal(2, r, "l=#{l.inspect} r=#{r.inspect}")
6713
+ assert_kind_of(LongDecimal, r, "must be LongDecimal")
6714
+
6715
+ end
6716
+ end
6717
+ def test_ldq_round_to_scale_harmonic_common
6718
+ print "\ntest_ldq_round_to_scale_harmonic_common [#{Time.now}]: "
6719
+
6720
+ delta = Rational(3, 5463458053)
6721
+ arr = [ Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ]
6722
+
6723
+ ALL_ROUNDING_MODES.each do |rounding_mode|
6724
+ unless (rounding_mode.major == MAJOR_HARMONIC)
6725
+ next
6726
+ end
6727
+ # only 0 gets rounded to zero
6728
+ l = LongDecimalQuot(Rational(0, 1), 0)
6729
+ r = l.round_to_scale(0, rounding_mode)
6730
+ assert_equal(0, r, "l=#{l.inspect} r=#{r.inspect}")
6731
+
6732
+ l = LongDecimalQuot(Rational(1, 100), 0)
6733
+ r = l.round_to_scale(0, rounding_mode)
6734
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6735
+ l = LongDecimalQuot(Rational(50, 100), 0)
6736
+ r = l.round_to_scale(0, rounding_mode)
6737
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6738
+ l = LongDecimalQuot(Rational(99, 100), 0)
6739
+ r = l.round_to_scale(0, rounding_mode)
6740
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6741
+ l = LongDecimalQuot(Rational(100, 100), 0)
6742
+ r = l.round_to_scale(0, rounding_mode)
6743
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6744
+
6745
+ l = LongDecimalQuot(Rational(1, 101), 0)
6746
+ r = l.round_to_scale(0, rounding_mode)
6747
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6748
+ l = LongDecimalQuot(Rational(50, 101), 0)
6749
+ r = l.round_to_scale(0, rounding_mode)
6750
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6751
+ l = LongDecimalQuot(Rational(99, 101), 0)
6752
+ r = l.round_to_scale(0, rounding_mode)
6753
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6754
+ l = LongDecimalQuot(Rational(101, 101), 0)
6755
+ r = l.round_to_scale(0, rounding_mode)
6756
+ assert_equal(1, r, "l=#{l.inspect} r=#{r.inspect}")
6757
+
6758
+ l = LongDecimalQuot(Rational(-1, 100), 0)
6759
+ r = l.round_to_scale(0, rounding_mode)
6760
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6761
+ l = LongDecimalQuot(Rational(-50, 100), 0)
6762
+ r = l.round_to_scale(0, rounding_mode)
6763
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6764
+ l = LongDecimalQuot(Rational(-99, 100), 0)
6765
+ r = l.round_to_scale(0, rounding_mode)
6766
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6767
+ l = LongDecimalQuot(Rational(-100, 100), 0)
6768
+ r = l.round_to_scale(0, rounding_mode)
6769
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6770
+
6771
+ l = LongDecimalQuot(Rational(-1, 101), 0)
6772
+ r = l.round_to_scale(0, rounding_mode)
6773
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6774
+ l = LongDecimalQuot(Rational(-50, 101), 0)
6775
+ r = l.round_to_scale(0, rounding_mode)
6776
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6777
+ l = LongDecimalQuot(Rational(-99, 101), 0)
6778
+ r = l.round_to_scale(0, rounding_mode)
6779
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6780
+ l = LongDecimalQuot(Rational(-101, 101), 0)
6781
+ r = l.round_to_scale(0, rounding_mode)
6782
+ assert_equal(-1, r, "l=#{l.inspect} r=#{r.inspect}")
6783
+
6784
+ arr.each do |rat|
6785
+ rat_up = rat + delta
6786
+ rat_down = rat - delta
6787
+ [ -1, 1].each do |sign|
6788
+ l_up = LongDecimalQuot(sign * rat_up, 0)
6789
+ r_up = l_up.round_to_scale(0, rounding_mode)
6790
+ r_up_expected = l_up.round_to_scale(0, ROUND_UP)
6791
+ assert_equal(r_up_expected, r_up, "l=#{l_up} r=#{r_up}")
6792
+
6793
+ l_down = LongDecimalQuot(sign * rat_down, 0)
6794
+ r_down = l_down.round_to_scale(0, rounding_mode)
6795
+ r_down_expected = l_down.round_to_scale(0, ROUND_DOWN)
6796
+ assert_equal(r_down_expected, r_down, "l=#{l_down} r=#{r_down}")
6797
+ end
6798
+ end
6799
+ end
6800
+ end
6801
+
6802
+ #
6803
+ # test rounding of LongDecimalQuot with ROUND_HARMONIC_UP
6804
+ #
6805
+ def test_ldq_round_to_scale_harmonic_up
6806
+ print "\ntest_ldq_round_to_scale_harmonic_up [#{Time.now}]: "
6807
+
6808
+ arr = [ Rational(0, 1), Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ]
6809
+ arr.each do |rat|
6810
+ [ -1, 1].each do |sign|
6811
+ l = LongDecimalQuot(sign * rat, 0)
6812
+ r = l.round_to_scale(0, ROUND_HARMONIC_UP)
6813
+ re = l.round_to_scale(0, ROUND_UP)
6814
+ assert_equal(re, r, "l=#{l.inspect} r=#{r.inspect}")
6815
+ end
6816
+ end
6817
+ end
6818
+
6819
+ #
6820
+ # test rounding of LongDecimalQuot with ROUND_HARMONIC_DOWN
6821
+ #
6822
+ def test_ldq_round_to_scale_harmonic_down
6823
+ print "\ntest_ldq_round_to_scale_harmonic_down [#{Time.now}]: "
6824
+
6825
+ arr = [ Rational(0, 1), Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ]
6826
+ arr.each do |rat|
6827
+ [ -1, 1].each do |sign|
6828
+ l = LongDecimalQuot(sign * rat, 0)
6829
+ r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
6830
+ re = l.round_to_scale(0, ROUND_DOWN)
6831
+ assert_equal(re, r, "l=#{l.inspect} r=#{r.inspect}")
6832
+ end
6833
+ end
6834
+ end
6835
+
6836
+ #
6837
+ # test rounding of LongDecimalQuot with ROUND_HARMONIC_CEILING
6838
+ #
6839
+ def test_ldq_round_to_scale_harmonic_ceiling
6840
+ print "\ntest_ldq_round_to_scale_harmonic_ceiling [#{Time.now}]: "
6841
+
6842
+ arr = [ Rational(0, 1), Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ]
6843
+ arr.each do |rat|
6844
+ [ -1, 1].each do |sign|
6845
+ l = LongDecimalQuot(sign * rat, 0)
6846
+ r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
6847
+ re = l.round_to_scale(0, ROUND_CEILING)
6848
+ assert_equal(re, r, "l=#{l.inspect} r=#{r.inspect}")
6849
+ end
6850
+ end
6851
+ end
6852
+
6853
+ #
6854
+ # test rounding of LongDecimalQuot with ROUND_HARMONIC_FLOOR
6855
+ #
6856
+ def test_ldq_round_to_scale_harmonic_floor
6857
+ print "\ntest_ldq_round_to_scale_harmonic_floor [#{Time.now}]: "
6858
+
6859
+ arr = [ Rational(0, 1), Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ]
6860
+ arr.each do |rat|
6861
+ [ -1, 1].each do |sign|
6862
+ l = LongDecimalQuot(sign * rat, 0)
6863
+ r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
6864
+ re = l.round_to_scale(0, ROUND_FLOOR)
6865
+ assert_equal(re, r, "l=#{l.inspect} r=#{r.inspect}")
6866
+ end
6867
+ end
6868
+ end
6869
+
6870
+ #
6871
+ # test rounding of LongDecimalQuot with ROUND_HARMONIC_EVEN
6872
+ #
6873
+ def test_ldq_round_to_scale_harmonic_odd_even
6874
+ print "\ntest_ldq_round_to_scale_harmonic_even [#{Time.now}]: "
6875
+
6876
+ sorted_arr = [ Rational(0, 1), Rational(112, 15), Rational(12, 5), Rational(144, 17), Rational(180, 19), Rational(24, 7), Rational(4, 3), Rational(40, 9), Rational(60, 11), Rational(84, 13) ].sort
6877
+ sorted_arr.each_with_index do |rat, idx|
6878
+ [ -1, 1].each do |sign|
6879
+ l = LongDecimalQuot(sign * rat, 0)
6880
+ r_even = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
6881
+ r_odd = l.round_to_scale(0, ROUND_HARMONIC_ODD)
6882
+ r_up = l.round_to_scale(0, ROUND_UP)
6883
+ r_down = l.round_to_scale(0, ROUND_DOWN)
6884
+ msg = "l=#{l} r_even=#{r_even} r_odd=#{r_odd} r_up=#{r_up} r_down=#{r_down}"
6885
+ if (idx[0] == 1)
6886
+ # idx is odd, lower is odd, upper is even
6887
+ assert_equal(r_down, r_odd, msg)
6888
+ assert_equal(r_up, r_even, msg)
6889
+ else
6890
+ # idx is even, lower is even, upper is odd
6891
+ assert_equal(r_down, r_even, msg)
6892
+ assert_equal(r_up, r_odd, msg)
6893
+ end
6894
+ end
6895
+ end
6896
+ end
6897
+
6648
6898
  # TODO: do tests with rational numbers that approach square and cube roots with continuos fractions
6649
6899
  # TODO: do tests for harmonic that are exact matches
6650
- # def test_ldq_round_to_scale_geometric_common
6651
6900
  # def test_ldq_round_to_scale_harmonic_common
6652
6901
  # def test_ldq_round_to_scale_harmonic_*
6653
6902
  # def test_ldq_round_to_scale_quadratic_common
6654
6903
  # def test_ldq_round_to_scale_cubic_common
6655
6904
 
6905
+
6656
6906
  #
6657
6907
  # test rounding of LongDecimalQuot with ROUND_UNNECESSARY
6658
6908
  #
@@ -7517,13 +7767,15 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
7517
7767
  # test the right ordering of the means excluding agm/hgm
7518
7768
  def test_means_three_param
7519
7769
  print "\ntest_means_three_param [#{Time.now}] (4 min): "
7520
- arr = [ 0, 1, 2, 0.0, 1.0, Math::PI, Rational(40, 9), Rational(0, 1), Rational(1,1), LongDecimal(3333333333333333, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
7770
+ # arr = [ 0, 1, 2, 0.0, 1.0, Math::PI, Rational(40, 9), Rational(0, 1), Rational(1,1), LongDecimal(3333333333333333, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
7771
+ arr = [ 1, 2, 1.0, Math::PI, Rational(40, 9), Rational(1,1), LongDecimal(3333333333333333, 10), LongMath.pi(100) ]
7521
7772
  arr.each do |x|
7522
7773
  arr.each do |y|
7523
7774
  print ":"
7524
7775
  arr.each do |z|
7525
7776
  print "."
7526
- 12.times do |prec|
7777
+ # 12.times do |prec|
7778
+ [0,1,2,5,10,11,12].each do |prec|
7527
7779
  ALL_ROUNDING_MODES.each do |rm|
7528
7780
  if (rm == ROUND_UNNECESSARY)
7529
7781
  next
@@ -7567,7 +7819,8 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
7567
7819
  # test the right ordering of the means (including agm/hgm)
7568
7820
  def test_means_three_param_agm_hgm
7569
7821
  print "\ntest_means_three_param_agm_hgm [#{Time.now}] (4 min): "
7570
- arr = [ 0, 1, 2, 0.0, 1.0, Math::PI, Rational(40, 9), Rational(0, 1), Rational(1,1), LongDecimal(3333333333333333, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
7822
+ # arr = [ 0, 1, 2, 0.0, 1.0, Math::PI, Rational(40, 9), Rational(0, 1), Rational(1,1), LongDecimal(3333333333333333, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
7823
+ arr = [ 1, 2, 1.0, Math::PI, Rational(40, 9), Rational(1,1), LongDecimal(3333333333333333, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
7571
7824
  arr.each do |x|
7572
7825
  x.freeze
7573
7826
  arr.each do |y|
@@ -7576,8 +7829,9 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
7576
7829
  arr.each do |z|
7577
7830
  z.freeze
7578
7831
  print "."
7579
- prec = 0
7580
- 3.times do |i|
7832
+ # prec = 0
7833
+ # 3.times do |i|
7834
+ [0, 31].each_with_index do |prec, i|
7581
7835
  ALL_ROUNDING_MODES.each do |rm|
7582
7836
  if (rm == ROUND_UNNECESSARY)
7583
7837
  next
@@ -7619,7 +7873,7 @@ class TestLongDecimal_class < UnitTest # RUNIT::TestCase
7619
7873
  assert_equal(cm, ma, text)
7620
7874
  end
7621
7875
  end
7622
- prec = (prec << 3) + 11
7876
+ # prec = (prec << 3) + 11
7623
7877
  end
7624
7878
  end
7625
7879
  end
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
6
6
  #
7
- # TAG: $TAG v1.00.03$
7
+ # TAG: $TAG v1.00.04$
8
8
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testlongdeclib.rb,v 1.42 2011/02/03 00:22:39 bk1 Exp $
9
9
  # CVS-Label: $Name: $
10
10
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -41,20 +41,22 @@ module TestLongDecHelper
41
41
  def assert_equal_rbo(lhs, rhs, msg="", lhsname="lhs", rhsname="rhs", delta=0)
42
42
  msg2 = "#{lhsname}=#{lhs} (#{lhs.class}) #{rhsname}=#{rhs} (#{rhs.class}) " + msg
43
43
  if (lhs.kind_of? Rational) && (rhs.kind_of? BigDecimal) || (lhs.kind_of? BigDecimal) && (rhs.kind_of? Rational)
44
- assert_equal(lhs.to_ld, rhs.to_ld, msg2)
44
+ lhs_ld = lhs.to_ld
45
+ rhs_ld = rhs.to_ld
46
+ assert(lhs_ld == rhs_ld || (lhs_ld - rhs_ld).abs <= delta, msg2 + " as ld: #{lhs_ld} #{rhs_ld}")
45
47
  elsif (delta > 0 && ((lhs.kind_of? Float) || (rhs.kind_of? Float)))
46
48
  assert_equal_float(lhs, rhs, delta, msg2 + " d=#{delta}")
47
49
  elsif ((lhs.kind_of? Rational) && (rhs.kind_of? Rational))
48
- assert_equal(lhs.numerator*rhs.denominator, lhs.denominator*rhs.numerator, msg)
50
+ assert_equal(lhs.numerator*rhs.denominator, lhs.denominator*rhs.numerator, msg + " rational")
49
51
  else
50
- assert_equal(lhs, rhs, msg2)
52
+ assert_equal(lhs, rhs, msg2 + " exact")
51
53
  end
52
54
  end
53
55
 
54
56
  def assert_equal_complex(lhs, rhs, msg="", delta=0)
55
57
  msg2 = "lhs=#{lhs} rhs=#{rhs} " + msg
56
- assert_equal_rbo(lhs.real, rhs.real, "real: #{lhs.real==rhs.real} " + msg2, "lhsr", "rhsr", delta)
57
- assert_equal_rbo(lhs.image, rhs.image, "imag: #{lhs.image==rhs.image} " + msg2, "lhsi", "rhsi", delta)
58
+ assert_equal_rbo(lhs.real, rhs.real, "real: #{lhs.real==rhs.real} " + msg2, "lhsr", "rhsr", delta)
59
+ assert_equal_rbo(lhs.imag, rhs.imag, "imag: #{lhs.imag==rhs.imag} " + msg2, "lhsi", "rhsi", delta)
58
60
  end
59
61
 
60
62
  #
@@ -881,6 +883,12 @@ module TestLongDecHelper
881
883
  r
882
884
  end
883
885
 
886
+ def deep_freeze_complex(z)
887
+ z.imag.freeze
888
+ z.real.freeze
889
+ z.freeze
890
+ end
891
+
884
892
  end
885
893
 
886
894
  # end of file testlongdecimal.rb
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
6
6
  #
7
- # TAG: $TAG v1.00.03$
7
+ # TAG: $TAG v1.00.04$
8
8
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testrandlib.rb,v 1.10 2011/02/03 00:22:39 bk1 Exp $
9
9
  # CVS-Label: $Name: $
10
10
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
6
6
  #
7
- # TAG: $TAG v1.00.03$
7
+ # TAG: $TAG v1.00.04$
8
8
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testrandom.rb,v 1.18 2011/02/03 00:22:39 bk1 Exp $
9
9
  # CVS-Label: $Name: $
10
10
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2015
6
6
  #
7
- # TAG: $TAG v1.00.03$
7
+ # TAG: $TAG v1.00.04$
8
8
  # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testrandpower.rb,v 1.14 2009/05/09 15:37:00 bk1 Exp $
9
9
  # CVS-Label: $Name: $
10
10
  # Author: $Author: bk1 $ (Karl Brodowsky)
@@ -126,7 +126,7 @@ irrational values, this is done in a uniform way. Since rounding
126
126
  operations can also apply to internal intermediate results, it is a
127
127
  good idea not to constrain their definition to rational numbers.
128
128
 
129
- Major rounding modes are
129
+ Major rounding modes are
130
130
 
131
131
  \begin{tabular}{|l|p{100mm}|}
132
132
  \hline
@@ -262,19 +262,19 @@ zero, should it occur, shoud be rounded.
262
262
  \section{Division}
263
263
 
264
264
  Regular division using $/$ yields an instance of {\slshape LongDecimalQuot\/}. The approximate number of significant digits is estimated by using the partial derivatives of $f(x, y) = \frac{x}{y}$.
265
- Assume $x=\ld(m, s)$ and $y=\ld(n, t)$. So we get for $\ld(m, s) / \ld(n, t)$ an result
265
+ Assume $x=\ld(m, s)$ and $y=\ld(n, t)$. So we get for $\ld(m, s) / \ld(n, t)$ an result
266
266
 
267
267
  $$\frac{x}{y} = \ldq(10^{t-s}\frac{m}{n}, r)$$
268
268
 
269
- with
269
+ with
270
270
 
271
- $$r \approx -\log_{10}\left(10^{-s} \frac{1}{|y|} + 10^{-t} \frac{|x|}{y^2}\right)
271
+ $$r \approx -\log_{10}\left(10^{-s} \frac{1}{|y|} + 10^{-t} \frac{|x|}{y^2}\right)
272
272
  = 2 \log_{10}(y) + s + t -\log_{10}\left(|m| + |n|) \right)$$
273
273
 
274
274
  In order to avoid expensive logarithmic operation for such basic operations as division this is approximated by
275
275
 
276
276
  $$r = \max( 0, 2 v + s + t - \max( u + s, v + t) - 3)$$
277
- with $u = \lfloor\log |m| \rfloor - s + 1$ and $v = \lfloor\log |n| \rfloor - t + 1$ for $m$ and $n$ not zero
277
+ with $u = \lfloor\log |m| \rfloor - s + 1$ and $v = \lfloor\log |n| \rfloor - t + 1$ for $m$ and $n$ not zero
278
278
  and $u=-s$ for $m = 0$ and $v=-t$ for $n=0$. Using $\max(a, b)$ instead of $log_{10}(10^a + 10^b)$ is a good approximation, when a and b are far apart.
279
279
 
280
280
  It is recommended to use explicit rounding after having performed
@@ -336,7 +336,7 @@ For the logarithm we first use $x = e^n x_0$ with $n \in \mathbb{N}_0$ and $x_0
336
336
 
337
337
  $$\log(x) = n + \log(x_0).$$
338
338
 
339
- From here we make use of the efficient square root calculation facility and use
339
+ From here we make use of the efficient square root calculation facility and use
340
340
 
341
341
  $$\bigwedge_{m=0}^\infty x_{m+1} = \sqrt{x_m}$$
342
342
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: long-decimal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.00.03
4
+ version: 1.00.04
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Brodowsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-01 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 2.2.2
83
+ rubygems_version: 2.4.5
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Fixed Point Decimal Numbers