long-decimal 1.00.01 → 1.00.02
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +15 -0
- data/README +41 -41
- data/Rakefile +19 -73
- data/lib/long-decimal-extra.rb +5 -672
- data/lib/long-decimal.rb +1609 -141
- data/lib/long-decimal/version.rb +3 -0
- data/long-decimal.gemspec +22 -0
- data/test/testlongdecimal.rb +2498 -651
- data/test/testlongdeclib.rb +20 -10
- data/test/testrandlib.rb +6 -5
- data/test/testrandom.rb +30 -14
- data/test/testrandpower.rb +22 -7
- data/tex/long-decimal.pdf +0 -0
- data/tex/long-decimal.tex +420 -0
- metadata +72 -64
- data/VERSION +0 -1
- data/install.rb +0 -18
- data/make_doc.rb +0 -12
- data/test/testlongdecimal-extra.rb +0 -1750
- data/test/testlongdecimal-performance.rb +0 -357
- data/test/testrandom-extra.rb +0 -80
- data/version.rb +0 -39
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'long-decimal/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "long-decimal"
|
8
|
+
spec.version = LongDecimalSupport::VERSION
|
9
|
+
spec.authors = ["Karl Brodowsky"]
|
10
|
+
spec.email = ["karl_brodowsky@yahoo.com"]
|
11
|
+
spec.summary = %q{Fixed Point Decimal Numbers}
|
12
|
+
spec.description = %q{Decimal arbitrary precision fixed point numbers in pure Ruby.}
|
13
|
+
spec.homepage = "https://github.com/bk1/ruby-long-decimal"
|
14
|
+
spec.license = "LGPL or Ruby"
|
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 =
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/test/testlongdecimal.rb
CHANGED
@@ -5,11 +5,20 @@
|
|
5
5
|
# (C) Karl Brodowsky (IT Sky Consulting GmbH) 2006-2009
|
6
6
|
#
|
7
7
|
# CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testlongdecimal.rb,v 1.86 2011/01/16 22:16:59 bk1 Exp $
|
8
|
-
# CVS-Label: $Name:
|
8
|
+
# CVS-Label: $Name: $
|
9
9
|
# Author: $Author: bk1 $ (Karl Brodowsky)
|
10
10
|
#
|
11
11
|
|
12
|
-
|
12
|
+
$test_type = nil
|
13
|
+
if ((RUBY_VERSION.match /^1\./) || (RUBY_VERSION.match /^2\.0/)) then
|
14
|
+
require 'test/unit'
|
15
|
+
$test_type = :v20
|
16
|
+
else
|
17
|
+
require 'minitest/autorun'
|
18
|
+
require 'test/unit/assertions'
|
19
|
+
include Test::Unit::Assertions
|
20
|
+
$test_type = :v21
|
21
|
+
end
|
13
22
|
|
14
23
|
# require "runit/testcase"
|
15
24
|
# require "runit/cui/testrunner"
|
@@ -20,11 +29,20 @@ load "test/testlongdeclib.rb"
|
|
20
29
|
|
21
30
|
LongMath.prec_overflow_handling = :warn_use_max
|
22
31
|
|
32
|
+
if ($test_type == :v20)
|
33
|
+
class UnitTest < Test::Unit::TestCase
|
34
|
+
end
|
35
|
+
else
|
36
|
+
class UnitTest < MiniTest::Test
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
23
40
|
#
|
24
41
|
# test class for LongDecimal and LongDecimalQuot
|
25
42
|
#
|
26
|
-
class TestLongDecimal_class <
|
43
|
+
class TestLongDecimal_class < UnitTest # RUNIT::TestCase
|
27
44
|
include TestLongDecHelper
|
45
|
+
include LongDecimalRoundingMode
|
28
46
|
|
29
47
|
@RCS_ID='-$Id: testlongdecimal.rb,v 1.86 2011/01/16 22:16:59 bk1 Exp $-'
|
30
48
|
|
@@ -186,6 +204,82 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
186
204
|
assert((zr-xr).abs <= zr.unit, "zr-xr")
|
187
205
|
end
|
188
206
|
|
207
|
+
#
|
208
|
+
# test exp2 of LongMath
|
209
|
+
#
|
210
|
+
def test_exp2
|
211
|
+
print "\ntest_exp2 [#{Time.now}]: "
|
212
|
+
10.times do |i|
|
213
|
+
n = (i*i+i)/2
|
214
|
+
x = LongDecimal(n, 3*i)+LongMath.pi(20)
|
215
|
+
check_exp2_floated(x, n)
|
216
|
+
|
217
|
+
y = LongMath.exp2(x, n)
|
218
|
+
yy = LongMath.exp2(x, n + 5)
|
219
|
+
assert_equal(yy.round_to_scale(y.scale, LongDecimal::ROUND_HALF_DOWN), y, "x=#{x} y=#{y} yy=#{yy}")
|
220
|
+
z = LongMath.power(2, x, n)
|
221
|
+
assert_equal(z, y, "exp2 x=#{x} y=#{y} z=#{z} i=#{i} n=#{n}")
|
222
|
+
end
|
223
|
+
|
224
|
+
# random tests that have failed previously
|
225
|
+
check_exp2_floated(LongDecimal("-0.00147492625237084606064197462823289474038138852346725504592707365251736299180323394082648207114993022483949313246714392730651107673327728615912046468517225938833913598854936005"), 20)
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# test the calculation of the exponential function where result is
|
231
|
+
# near zero
|
232
|
+
#
|
233
|
+
def test_exp2_near_zero
|
234
|
+
print "\ntest_exp2_near_zero [#{Time.now}]: "
|
235
|
+
|
236
|
+
x = LongDecimal(1, 100)
|
237
|
+
y = LongMath.log2(x, 100)
|
238
|
+
z = check_exp2_floated(y, 100)
|
239
|
+
assert_equal(x, z, "must be equal")
|
240
|
+
z = check_exp2_floated(y, 99)
|
241
|
+
assert(z.zero?, "must be zero")
|
242
|
+
z = check_exp2_floated(y * 100, 99)
|
243
|
+
assert(z.zero?, "must be zero")
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
#
|
248
|
+
# test exp10 of LongMath
|
249
|
+
#
|
250
|
+
def test_exp10
|
251
|
+
print "\ntest_exp10 [#{Time.now}]: "
|
252
|
+
10.times do |i|
|
253
|
+
n = (i*i+i)/2
|
254
|
+
x = LongDecimal(n, 3*i)+LongMath.pi(20)
|
255
|
+
check_exp10_floated(x, n)
|
256
|
+
|
257
|
+
y = LongMath.exp10(x, n)
|
258
|
+
yy = LongMath.exp10(x, n + 5)
|
259
|
+
assert_equal(yy.round_to_scale(y.scale, LongDecimal::ROUND_HALF_DOWN), y, "x=#{x} y=#{y} yy=#{yy}")
|
260
|
+
z = LongMath.power(10, x, n)
|
261
|
+
assert_equal(z, y, "exp10 x=#{x} y=#{y} z=#{z} i=#{i} n=#{n}")
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
#
|
266
|
+
# test the calculation of the exponential function where result is
|
267
|
+
# near zero
|
268
|
+
#
|
269
|
+
def test_exp10_near_zero
|
270
|
+
print "\ntest_exp10_near_zero [#{Time.now}]: "
|
271
|
+
|
272
|
+
x = LongDecimal(1, 100)
|
273
|
+
y = LongMath.log10(x, 100)
|
274
|
+
z = check_exp10_floated(y, 100)
|
275
|
+
assert_equal(x, z, "must be equal")
|
276
|
+
z = check_exp10_floated(y, 99)
|
277
|
+
assert(z.zero?, "must be zero")
|
278
|
+
z = check_exp10_floated(y * 100, 99)
|
279
|
+
assert(z.zero?, "must be zero")
|
280
|
+
|
281
|
+
end
|
282
|
+
|
189
283
|
#
|
190
284
|
# test LongMath.log with non-LongDecimal arguments
|
191
285
|
#
|
@@ -282,6 +376,410 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
282
376
|
end
|
283
377
|
|
284
378
|
|
379
|
+
#
|
380
|
+
# test LongMath.power for bases that can be expressed as integer
|
381
|
+
#
|
382
|
+
def test_lm_power_xint
|
383
|
+
print "\ntest_lm_power_xint [#{Time.now}]: "
|
384
|
+
|
385
|
+
xx = LongMath.log(3, 40)
|
386
|
+
pi = LongMath.pi(40)
|
387
|
+
sq = LongMath.sqrt(5, 40)
|
388
|
+
|
389
|
+
check_power_xint(2, 700.01, 10)
|
390
|
+
check_power_xint(2, 100.001, 10)
|
391
|
+
check_power_xint(2, 1.000000001, 10)
|
392
|
+
check_power_xint(2, 0.01, 10)
|
393
|
+
check_power_xint(2, 1e-10, 10)
|
394
|
+
check_power_xint(2, 1e-90, 10)
|
395
|
+
check_power_xint(2, 0, 10)
|
396
|
+
check_power_xint(2, -1.000000001, 10)
|
397
|
+
check_power_xint(2, -100.001, 10)
|
398
|
+
check_power_xint(2, -700.01, 10)
|
399
|
+
check_power_xint(2, xx, 10)
|
400
|
+
check_power_xint(2, pi, 10)
|
401
|
+
check_power_xint(2, sq, 10)
|
402
|
+
|
403
|
+
check_power_xint(10, 308.01, 10)
|
404
|
+
check_power_xint(10, 100.001, 10)
|
405
|
+
check_power_xint(10, 1.000000001, 10)
|
406
|
+
check_power_xint(10, 0.01, 10)
|
407
|
+
check_power_xint(10, 1e-10, 10)
|
408
|
+
check_power_xint(10, 1e-90, 10)
|
409
|
+
check_power_xint(10, 0, 10)
|
410
|
+
check_power_xint(10, -1.000000001, 10)
|
411
|
+
check_power_xint(10, -100.001, 10)
|
412
|
+
check_power_xint(10, -308.01, 10)
|
413
|
+
check_power_xint(10, xx, 10)
|
414
|
+
check_power_xint(10, pi, 10)
|
415
|
+
check_power_xint(10, sq, 10)
|
416
|
+
|
417
|
+
check_power_xint(2, 700.01, 100)
|
418
|
+
check_power_xint(2, 100.001, 100)
|
419
|
+
check_power_xint(2, 1.000000001, 100)
|
420
|
+
check_power_xint(2, 0.01, 100)
|
421
|
+
check_power_xint(2, 1e-10, 100)
|
422
|
+
check_power_xint(2, 1e-90, 100)
|
423
|
+
check_power_xint(2, 0, 100)
|
424
|
+
check_power_xint(2, -1.000000001, 100)
|
425
|
+
check_power_xint(2, -100.001, 100)
|
426
|
+
check_power_xint(2, -700.01, 100)
|
427
|
+
check_power_xint(2, xx, 100)
|
428
|
+
check_power_xint(2, pi, 100)
|
429
|
+
check_power_xint(2, sq, 100)
|
430
|
+
|
431
|
+
check_power_xint(10, 308.01, 100)
|
432
|
+
check_power_xint(10, 100.001, 100)
|
433
|
+
check_power_xint(10, 1.000000001, 100)
|
434
|
+
check_power_xint(10, 0.01, 100)
|
435
|
+
check_power_xint(10, 1e-10, 100)
|
436
|
+
check_power_xint(10, 1e-90, 100)
|
437
|
+
check_power_xint(10, 0, 100)
|
438
|
+
check_power_xint(10, -1.000000001, 100)
|
439
|
+
check_power_xint(10, -100.001, 100)
|
440
|
+
check_power_xint(10, -308.01, 100)
|
441
|
+
check_power_xint(10, xx, 100)
|
442
|
+
check_power_xint(10, pi, 100)
|
443
|
+
check_power_xint(10, sq, 100)
|
444
|
+
|
445
|
+
check_power_xint(2, 700.01, 40)
|
446
|
+
check_power_xint(2, 100.001, 40)
|
447
|
+
check_power_xint(2, 1.000000001, 40)
|
448
|
+
check_power_xint(2, 0.01, 40)
|
449
|
+
check_power_xint(2, 1e-10, 40)
|
450
|
+
check_power_xint(2, 1e-90, 40)
|
451
|
+
check_power_xint(2, 0, 40)
|
452
|
+
check_power_xint(2, -1.000000001, 40)
|
453
|
+
check_power_xint(2, -100.001, 40)
|
454
|
+
check_power_xint(2, -700.01, 40)
|
455
|
+
check_power_xint(2, xx, 40)
|
456
|
+
check_power_xint(2, pi, 40)
|
457
|
+
check_power_xint(2, sq, 40)
|
458
|
+
|
459
|
+
check_power_xint(10, 308.01, 40)
|
460
|
+
check_power_xint(10, 100.001, 40)
|
461
|
+
check_power_xint(10, 1.000000001, 40)
|
462
|
+
check_power_xint(10, 0.01, 40)
|
463
|
+
check_power_xint(10, 1e-10, 40)
|
464
|
+
check_power_xint(10, 1e-90, 40)
|
465
|
+
check_power_xint(10, 0, 40)
|
466
|
+
check_power_xint(10, -1.000000001, 40)
|
467
|
+
check_power_xint(10, -100.001, 40)
|
468
|
+
check_power_xint(10, -308.01, 40)
|
469
|
+
check_power_xint(10, xx, 40)
|
470
|
+
check_power_xint(10, pi, 40)
|
471
|
+
check_power_xint(10, sq, 40)
|
472
|
+
|
473
|
+
end
|
474
|
+
|
475
|
+
#
|
476
|
+
# test LongMath.power for bases that can be expressed as integer
|
477
|
+
#
|
478
|
+
def test_lm_power_yint
|
479
|
+
print "\ntest_lm_power_yint [#{Time.now}] (2 min): "
|
480
|
+
|
481
|
+
xx = LongMath.log(3, 40)
|
482
|
+
pi = LongMath.pi(40)
|
483
|
+
sq = LongMath.sqrt(5, 40)
|
484
|
+
|
485
|
+
check_power_yint(xx, 400, 10)
|
486
|
+
check_power_yint(xx, 100, 10)
|
487
|
+
check_power_yint(xx, 1, 10)
|
488
|
+
check_power_yint(xx, 0, 10)
|
489
|
+
check_power_yint(xx, -1, 10)
|
490
|
+
check_power_yint(xx, -100, 10)
|
491
|
+
check_power_yint(xx, -400, 10)
|
492
|
+
|
493
|
+
check_power_yint(pi, 400, 10)
|
494
|
+
check_power_yint(pi, 100, 10)
|
495
|
+
check_power_yint(pi, 1, 10)
|
496
|
+
check_power_yint(pi, 0, 10)
|
497
|
+
check_power_yint(pi, -1, 10)
|
498
|
+
check_power_yint(pi, -100, 10)
|
499
|
+
check_power_yint(pi, -400, 10)
|
500
|
+
|
501
|
+
check_power_yint(sq, 400, 10)
|
502
|
+
check_power_yint(sq, 100, 10)
|
503
|
+
check_power_yint(sq, 1, 10)
|
504
|
+
check_power_yint(sq, 0, 10)
|
505
|
+
check_power_yint(sq, -1, 10)
|
506
|
+
check_power_yint(sq, -100, 10)
|
507
|
+
check_power_yint(sq, -400, 10)
|
508
|
+
|
509
|
+
check_power_yint(xx, 400, 100)
|
510
|
+
check_power_yint(xx, 100, 100)
|
511
|
+
check_power_yint(xx, 1, 100)
|
512
|
+
check_power_yint(xx, 0, 100)
|
513
|
+
check_power_yint(xx, -1, 100)
|
514
|
+
check_power_yint(xx, -100, 100)
|
515
|
+
check_power_yint(xx, -400, 100)
|
516
|
+
|
517
|
+
check_power_yint(pi, 400, 100)
|
518
|
+
check_power_yint(pi, 100, 100)
|
519
|
+
check_power_yint(pi, 1, 100)
|
520
|
+
check_power_yint(pi, 0, 100)
|
521
|
+
check_power_yint(pi, -1, 100)
|
522
|
+
check_power_yint(pi, -100, 100)
|
523
|
+
check_power_yint(pi, -400, 100)
|
524
|
+
|
525
|
+
check_power_yint(sq, 400, 100)
|
526
|
+
check_power_yint(sq, 100, 100)
|
527
|
+
check_power_yint(sq, 1, 100)
|
528
|
+
check_power_yint(sq, 0, 100)
|
529
|
+
check_power_yint(sq, -1, 100)
|
530
|
+
check_power_yint(sq, -100, 100)
|
531
|
+
check_power_yint(sq, -400, 100)
|
532
|
+
|
533
|
+
check_power_yint(xx, 400, 40)
|
534
|
+
check_power_yint(xx, 100, 40)
|
535
|
+
check_power_yint(xx, 1, 40)
|
536
|
+
check_power_yint(xx, 0, 40)
|
537
|
+
check_power_yint(xx, -1, 40)
|
538
|
+
check_power_yint(xx, -100, 40)
|
539
|
+
check_power_yint(xx, -400, 40)
|
540
|
+
|
541
|
+
check_power_yint(pi, 400, 40)
|
542
|
+
check_power_yint(pi, 100, 40)
|
543
|
+
check_power_yint(pi, 1, 40)
|
544
|
+
check_power_yint(pi, 0, 40)
|
545
|
+
check_power_yint(pi, -1, 40)
|
546
|
+
check_power_yint(pi, -100, 40)
|
547
|
+
check_power_yint(pi, -400, 40)
|
548
|
+
|
549
|
+
check_power_yint(sq, 400, 40)
|
550
|
+
check_power_yint(sq, 100, 40)
|
551
|
+
check_power_yint(sq, 1, 40)
|
552
|
+
check_power_yint(sq, 0, 40)
|
553
|
+
check_power_yint(sq, -1, 40)
|
554
|
+
check_power_yint(sq, -100, 40)
|
555
|
+
check_power_yint(sq, -400, 40)
|
556
|
+
|
557
|
+
end
|
558
|
+
|
559
|
+
#
|
560
|
+
# test LongMath.power for bases that can be expressed as integer
|
561
|
+
#
|
562
|
+
def test_lm_power_yhalfint
|
563
|
+
print "\ntest_lm_power_yhalfint [#{Time.now}] (10 min): "
|
564
|
+
|
565
|
+
xx = LongMath.log(3, 40)
|
566
|
+
pi = LongMath.pi(40)
|
567
|
+
sq = LongMath.sqrt(5, 40)
|
568
|
+
|
569
|
+
check_power_yhalfint(xx, 801, 10)
|
570
|
+
check_power_yhalfint(xx, 799, 10)
|
571
|
+
check_power_yhalfint(xx, 201, 10)
|
572
|
+
check_power_yhalfint(xx, 3, 10)
|
573
|
+
check_power_yhalfint(xx, 1, 10)
|
574
|
+
check_power_yhalfint(xx, -1, 10)
|
575
|
+
check_power_yhalfint(xx, -201, 10)
|
576
|
+
check_power_yhalfint(xx, -799, 10)
|
577
|
+
check_power_yhalfint(xx, -801, 10)
|
578
|
+
|
579
|
+
check_power_yhalfint(pi, 801, 10)
|
580
|
+
check_power_yhalfint(pi, 799, 10)
|
581
|
+
check_power_yhalfint(pi, 201, 10)
|
582
|
+
check_power_yhalfint(pi, 3, 10)
|
583
|
+
check_power_yhalfint(pi, 1, 10)
|
584
|
+
check_power_yhalfint(pi, -1, 10)
|
585
|
+
check_power_yhalfint(pi, -201, 10)
|
586
|
+
check_power_yhalfint(pi, -799, 10)
|
587
|
+
check_power_yhalfint(pi, -801, 10)
|
588
|
+
|
589
|
+
check_power_yhalfint(sq, 801, 10)
|
590
|
+
check_power_yhalfint(sq, 799, 10)
|
591
|
+
check_power_yhalfint(sq, 201, 10)
|
592
|
+
check_power_yhalfint(sq, 3, 10)
|
593
|
+
check_power_yhalfint(sq, 1, 10)
|
594
|
+
check_power_yhalfint(sq, -1, 10)
|
595
|
+
check_power_yhalfint(sq, -201, 10)
|
596
|
+
check_power_yhalfint(sq, -799, 10)
|
597
|
+
check_power_yhalfint(sq, -801, 10)
|
598
|
+
|
599
|
+
check_power_yhalfint(xx, 801, 40)
|
600
|
+
check_power_yhalfint(xx, 799, 40)
|
601
|
+
check_power_yhalfint(xx, 201, 40)
|
602
|
+
check_power_yhalfint(xx, 3, 40)
|
603
|
+
check_power_yhalfint(xx, 1, 40)
|
604
|
+
check_power_yhalfint(xx, -1, 40)
|
605
|
+
check_power_yhalfint(xx, -201, 40)
|
606
|
+
check_power_yhalfint(xx, -799, 40)
|
607
|
+
check_power_yhalfint(xx, -801, 40)
|
608
|
+
|
609
|
+
check_power_yhalfint(pi, 801, 40)
|
610
|
+
check_power_yhalfint(pi, 799, 40)
|
611
|
+
check_power_yhalfint(pi, 201, 40)
|
612
|
+
check_power_yhalfint(pi, 3, 40)
|
613
|
+
check_power_yhalfint(pi, 1, 40)
|
614
|
+
check_power_yhalfint(pi, -1, 40)
|
615
|
+
check_power_yhalfint(pi, -201, 40)
|
616
|
+
check_power_yhalfint(pi, -799, 40)
|
617
|
+
check_power_yhalfint(pi, -801, 40)
|
618
|
+
|
619
|
+
check_power_yhalfint(sq, 801, 40)
|
620
|
+
check_power_yhalfint(sq, 799, 40)
|
621
|
+
check_power_yhalfint(sq, 201, 40)
|
622
|
+
check_power_yhalfint(sq, 3, 40)
|
623
|
+
check_power_yhalfint(sq, 1, 40)
|
624
|
+
check_power_yhalfint(sq, -1, 40)
|
625
|
+
check_power_yhalfint(sq, -201, 40)
|
626
|
+
check_power_yhalfint(sq, -799, 40)
|
627
|
+
check_power_yhalfint(sq, -801, 40)
|
628
|
+
|
629
|
+
end
|
630
|
+
|
631
|
+
#
|
632
|
+
# test the calculation of the base-10-logarithm function
|
633
|
+
#
|
634
|
+
def test_log10
|
635
|
+
print "\ntest_log10 [#{Time.now}]: "
|
636
|
+
check_log10_floated(10**2000, 30)
|
637
|
+
check_log10_floated(100, 30)
|
638
|
+
check_log10_floated(1, 30)
|
639
|
+
check_log10_floated(0.01, 30)
|
640
|
+
check_log10_floated(1e-10, 30)
|
641
|
+
check_log10_floated(1e-90, 30)
|
642
|
+
check_log10_floated(1e-300, 30)
|
643
|
+
check_log10_floated(LongDecimal(1, 2000), 30)
|
644
|
+
|
645
|
+
check_log10_exact(10**2000, 2000, 30)
|
646
|
+
check_log10_exact(10**0, 0, 30)
|
647
|
+
check_log10_exact(10**1, 1, 30)
|
648
|
+
check_log10_exact(10**10, 10, 30)
|
649
|
+
|
650
|
+
# random tests that have failed
|
651
|
+
check_log10_floated(LongDecimal("587.00000000000000000095700000000000000000000000000000000000000000000000000000000000000000000001206"), 21)
|
652
|
+
check_log10_floated(LongDecimal("543.0000002480000900000000000000000000000000000000000000000000000000000000000000000000847"), 2)
|
653
|
+
check_log10_floated(LongDecimal("180.0000000000000000003570000000000000000000000000000000000000000000000577000000000000000000000000000637"), 2)
|
654
|
+
check_log10_floated(LongDecimal("0.0000000000000000000000000000000000000000180000000063000000000000000000000000000000000025"), 74)
|
655
|
+
check_log10_floated(LongDecimal("0.0000000000000000000000000006200000000000000000000000000000000000000000000000007940000015"), 74)
|
656
|
+
check_log10_floated(LongDecimal("0.00000000000000000000000000000000000000000032900000000000000000000233000000000000000000000000000000254"), 10)
|
657
|
+
check_log10_floated(LongDecimal("0.00000000000000000000000233000000094800000000000000000000000000000000000000000000000000000000000000682"), 100)
|
658
|
+
check_log10_floated(LongDecimal("0.000000000000000000000000000000000000097000000041500000000000000000784"), 97)
|
659
|
+
check_log10_floated(LongDecimal("185.000025300000000000000006320000000000000000000000000000737"), 27)
|
660
|
+
check_log10_floated(LongDecimal("0.00000000000000000000000000000302000000000000000000000000000000000000000000000000000000060673"), 100)
|
661
|
+
check_log10_floated(LongDecimal("442.000000000000045300000000000000000000000000000000000000000000000000000000000000000000000721000000000752"), 97)
|
662
|
+
check_log10_floated(LongDecimal("0.001030927835051546391752577284408545010096715910298651428936221023301883588097777204212461151695109779555577162172"), 62)
|
663
|
+
|
664
|
+
end
|
665
|
+
|
666
|
+
#
|
667
|
+
# test the calculation of the base-10-logarithm function
|
668
|
+
#
|
669
|
+
def test_log2
|
670
|
+
print "\ntest_log2 [#{Time.now}]: "
|
671
|
+
check_log2_floated(10**2000, 30)
|
672
|
+
check_log2_floated(2**2000, 30)
|
673
|
+
check_log2_floated(100, 30)
|
674
|
+
check_log2_floated(1, 30)
|
675
|
+
check_log2_floated(0.01, 30)
|
676
|
+
check_log2_floated(1e-10, 30)
|
677
|
+
check_log2_floated(1e-90, 30)
|
678
|
+
check_log2_floated(1e-300, 30)
|
679
|
+
check_log2_floated(LongDecimal(1, 2000), 30)
|
680
|
+
|
681
|
+
check_log2_exact(2**2000, 2000, 30)
|
682
|
+
check_log2_exact(2**0, 0, 30)
|
683
|
+
check_log2_exact(2**1, 1, 30)
|
684
|
+
check_log2_exact(2**10, 10, 30)
|
685
|
+
|
686
|
+
# random tests that have failed
|
687
|
+
check_log2_floated(LongDecimal("341.00000739000000000000000000000000000000000000000000000000000000000000171"), 3)
|
688
|
+
check_log2_floated(LongDecimal("504.00000000000000000000000000000000000000000000000000000000000000000000000000000000000327400000000000828"), 1)
|
689
|
+
check_log2_floated(LongDecimal("0.0033222591362126245847176079734219269102990032888157967351353737817463383406363175903135730345286354858609181999523961456225622835123748782987926188031081"), 48)
|
690
|
+
check_log2_floated(LongDecimal("0.000802000000000000000000197000000000000000000000302"), 84)
|
691
|
+
check_log2_floated(LongDecimal("0.00000000000000000000000000000000000000000000000000000000000000000000000452000000000069480"), 3)
|
692
|
+
check_log2_floated(LongDecimal("0.0000000000000000000000000000000000000000000000000000930000000000000000000000000983000000000000300"), 61)
|
693
|
+
check_log2_floated(LongDecimal("0.000000000000000000000000000000000000000000000000000000000086000133000000000000000000000000947"), 106)
|
694
|
+
check_log2_floated(LongDecimal("0.00000000000000000000000000000276000000000000000000000000000000000008560000000000000000000000000000161"), 81)
|
695
|
+
|
696
|
+
end
|
697
|
+
|
698
|
+
#
|
699
|
+
# test the calculation of the base-x-logarithm of sqrt(x)
|
700
|
+
#
|
701
|
+
def test_log_2_10_of_sqrt
|
702
|
+
print "\ntest_log_2_10_of_sqrt [#{Time.now}]: "
|
703
|
+
# n = 125
|
704
|
+
n = 30
|
705
|
+
m = 5
|
706
|
+
x2 = LongMath.sqrt(2, n)
|
707
|
+
x10 = LongMath.sqrt(10, n)
|
708
|
+
|
709
|
+
(2*m).times do |i|
|
710
|
+
check_log2_floated(x2, n+m-i)
|
711
|
+
check_log10_floated(x10, n+m-i)
|
712
|
+
end
|
713
|
+
|
714
|
+
y2 = check_log2_floated(x2, n)
|
715
|
+
assert((y2*2).one?, "xe=#{x2} ye=#{y2}")
|
716
|
+
y10 = check_log10_floated(x10, n)
|
717
|
+
assert((y10*2).one?, "xe=#{x10} ye=#{y10}")
|
718
|
+
|
719
|
+
end
|
720
|
+
|
721
|
+
#
|
722
|
+
# test LongMath.power with non-LongDecimal arguments
|
723
|
+
#
|
724
|
+
def test_non_ld_power
|
725
|
+
print "\ntest_non_ld_power [#{Time.now}]: "
|
726
|
+
xi = 77
|
727
|
+
yi = 88
|
728
|
+
zi = LongMath.power(xi, yi, 35)
|
729
|
+
wi = (LongMath.log(zi, 40) / LongMath.log(xi, 40)).round_to_scale(30, LongMath::ROUND_HALF_EVEN)
|
730
|
+
assert(wi.is_int?, "wi=#{wi} not int (zi=#{zi})")
|
731
|
+
assert_equal(yi, wi.to_i, "zi=#{zi} wi=#{wi}")
|
732
|
+
zj = LongMath.power_internal(xi, yi, 35)
|
733
|
+
assert_equal(zi, zj, "internal power should yield the same result zi=#{zi} zj=#{zj}")
|
734
|
+
|
735
|
+
xf = 77.0
|
736
|
+
yf = 88.0
|
737
|
+
zf = LongMath.power(xf, yf, 35)
|
738
|
+
wf = (LongMath.log(zf, 40) / LongMath.log(xf, 40)).round_to_scale(30, LongMath::ROUND_HALF_EVEN)
|
739
|
+
assert(wf.is_int?, "wf=#{wf} not int (zf=#{zf} wi=#{wi} zi=#{zi}")
|
740
|
+
assert_equal(yf, wf.to_i, "yf=#{yf} wf=#{yf}")
|
741
|
+
|
742
|
+
xr = Rational(224, 225)
|
743
|
+
yr = Rational(168, 169)
|
744
|
+
zr = LongMath.power(xr, yr, 35)
|
745
|
+
wr = (LongMath.log(zr, 40) / LongMath.log(xr, 40)).round_to_scale(30, LongMath::ROUND_HALF_EVEN)
|
746
|
+
assert((yr-wr).abs <= wr.unit, "wr-yr")
|
747
|
+
end
|
748
|
+
|
749
|
+
#
|
750
|
+
# test the calculation of the power-function of LongMath
|
751
|
+
#
|
752
|
+
def test_lm_power
|
753
|
+
print "\ntest_lm_power [#{Time.now}]: "
|
754
|
+
check_power_floated(1.001, 1.001, 10)
|
755
|
+
check_power_floated(1.001, 2.001, 10)
|
756
|
+
check_power_floated(2.001, 1.001, 10)
|
757
|
+
check_power_floated(2.001, 2.001, 10)
|
758
|
+
check_power_floated(100.001, 10.001, 10)
|
759
|
+
check_power_floated(10.001, 100.001, 10)
|
760
|
+
check_power_floated(10.001, 100.001, 100)
|
761
|
+
check_power_floated(1e-20, 1.01, 19)
|
762
|
+
check_power_floated(1.01, 1e-20, 19)
|
763
|
+
check_power_floated(1e-20, 1.01, 20)
|
764
|
+
check_power_floated(1.01, 1e-20, 20)
|
765
|
+
check_power_floated(1e-20, 1.01, 21)
|
766
|
+
check_power_floated(1.01, 1e-20, 21)
|
767
|
+
|
768
|
+
check_power_floated(1.001, -1.001, 10)
|
769
|
+
check_power_floated(1.001, -2.001, 10)
|
770
|
+
check_power_floated(2.001, -1.001, 10)
|
771
|
+
check_power_floated(2.001, -2.001, 10)
|
772
|
+
check_power_floated(100.001, -10.001, 10)
|
773
|
+
check_power_floated(10.001, -100.001, 10)
|
774
|
+
check_power_floated(1e-20, -1.01, 19)
|
775
|
+
check_power_floated(1.01, -1e-20, 19)
|
776
|
+
check_power_floated(1e-20, -1.01, 20)
|
777
|
+
check_power_floated(1.01, -1e-20, 20)
|
778
|
+
check_power_floated(1e-20, -1.01, 21)
|
779
|
+
check_power_floated(1.01, -1e-20, 21)
|
780
|
+
|
781
|
+
end
|
782
|
+
|
285
783
|
|
286
784
|
#
|
287
785
|
# test calculation of pi
|
@@ -331,15 +829,15 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
331
829
|
|
332
830
|
l = LongDecimal(s)
|
333
831
|
pi = LongMath.pi 200
|
334
|
-
assert_equal(l.round_to_scale(200,
|
832
|
+
assert_equal(l.round_to_scale(200, ROUND_HALF_EVEN), pi, "200 digits")
|
335
833
|
pi = LongMath.pi 201
|
336
|
-
assert_equal(l.round_to_scale(201,
|
834
|
+
assert_equal(l.round_to_scale(201, ROUND_HALF_EVEN), pi, "201 digits")
|
337
835
|
pi = LongMath.pi 199
|
338
|
-
assert_equal(l.round_to_scale(199,
|
836
|
+
assert_equal(l.round_to_scale(199, ROUND_HALF_EVEN), pi, "199 digits")
|
339
837
|
pi = LongMath.pi 201
|
340
|
-
assert_equal(l.round_to_scale(201,
|
838
|
+
assert_equal(l.round_to_scale(201, ROUND_HALF_EVEN), pi, "201 digits")
|
341
839
|
pi = LongMath.pi 1000
|
342
|
-
assert_equal(l.round_to_scale(1000,
|
840
|
+
assert_equal(l.round_to_scale(1000, ROUND_HALF_EVEN), pi, "1000 digits")
|
343
841
|
end
|
344
842
|
|
345
843
|
#
|
@@ -649,7 +1147,6 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
649
1147
|
y = Complex(yr, yi)
|
650
1148
|
z = x/y
|
651
1149
|
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})"
|
652
|
-
# puts msg
|
653
1150
|
if (xr.kind_of? Integer) && (xi.kind_of? Integer) && (yr.kind_of? Integer) && (yi.kind_of? Integer) # && ! LongDecimal::RUNNING_19
|
654
1151
|
# ruby 1.9 creates rational result even from integers, ruby 1.8 uses integers as results.
|
655
1152
|
zc = (x.cdiv y)
|
@@ -733,7 +1230,12 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
733
1230
|
zb = Complex(BigDecimal("2.4"),BigDecimal("-3.2"))
|
734
1231
|
zf = Complex(2.4,-3.2)
|
735
1232
|
zl = Complex(LongDecimal("2.4"),LongDecimal("-3.2"))
|
736
|
-
zi =
|
1233
|
+
zi = nil
|
1234
|
+
if ($test_type == :v20)
|
1235
|
+
zi = Complex(2, -4)
|
1236
|
+
else
|
1237
|
+
zi = zr
|
1238
|
+
end
|
737
1239
|
twenties.each do |x|
|
738
1240
|
threes.each do |yr|
|
739
1241
|
fours.each do |yi|
|
@@ -930,7 +1432,7 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
930
1432
|
f = 224.225
|
931
1433
|
l = LongDecimal(f, 4)
|
932
1434
|
assert_equal("0.0224225", l.to_s, "l=#{l.inspect} f=#{f.inspect}")
|
933
|
-
assert_equal(l, (f/10000).to_ld(7), "to_ld")
|
1435
|
+
assert_equal(l, (f/10000).to_ld(7), "to_ld l=#{l} f=#{f}")
|
934
1436
|
end
|
935
1437
|
|
936
1438
|
#
|
@@ -1118,23 +1620,23 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1118
1620
|
def test_round_to_scale_up
|
1119
1621
|
print "\ntest_round_to_scale_up [#{Time.now}]: "
|
1120
1622
|
l = LongDecimal("2.21")
|
1121
|
-
r = l.round_to_scale(1,
|
1623
|
+
r = l.round_to_scale(1, ROUND_UP)
|
1122
1624
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1123
1625
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1124
1626
|
l = LongDecimal("-2.21")
|
1125
|
-
r = l.round_to_scale(1,
|
1627
|
+
r = l.round_to_scale(1, ROUND_UP)
|
1126
1628
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1127
1629
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1128
1630
|
l = LongDecimal("2.20")
|
1129
|
-
r = l.round_to_scale(1,
|
1631
|
+
r = l.round_to_scale(1, ROUND_UP)
|
1130
1632
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1131
1633
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1132
1634
|
l = LongDecimal("-2.20")
|
1133
|
-
r = l.round_to_scale(1,
|
1635
|
+
r = l.round_to_scale(1, ROUND_UP)
|
1134
1636
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1135
1637
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1136
1638
|
l = LongDecimal("2.24")
|
1137
|
-
r = l.round_to_scale(4,
|
1639
|
+
r = l.round_to_scale(4, ROUND_UP)
|
1138
1640
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1139
1641
|
end
|
1140
1642
|
|
@@ -1144,23 +1646,23 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1144
1646
|
def test_round_to_scale_down
|
1145
1647
|
print "\ntest_round_to_scale_down [#{Time.now}]: "
|
1146
1648
|
l = LongDecimal("2.29")
|
1147
|
-
r = l.round_to_scale(1,
|
1649
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
1148
1650
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1149
1651
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1150
1652
|
l = LongDecimal("-2.29")
|
1151
|
-
r = l.round_to_scale(1,
|
1653
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
1152
1654
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1153
1655
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1154
1656
|
l = LongDecimal("2.20")
|
1155
|
-
r = l.round_to_scale(1,
|
1657
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
1156
1658
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1157
1659
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1158
1660
|
l = LongDecimal("-2.20")
|
1159
|
-
r = l.round_to_scale(1,
|
1661
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
1160
1662
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1161
1663
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1162
1664
|
l = LongDecimal("2.24")
|
1163
|
-
r = l.round_to_scale(4,
|
1665
|
+
r = l.round_to_scale(4, ROUND_DOWN)
|
1164
1666
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1165
1667
|
end
|
1166
1668
|
|
@@ -1170,23 +1672,23 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1170
1672
|
def test_round_to_scale_ceiling
|
1171
1673
|
print "\ntest_round_to_scale_ceiling [#{Time.now}]: "
|
1172
1674
|
l = LongDecimal("2.21")
|
1173
|
-
r = l.round_to_scale(1,
|
1675
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
1174
1676
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1175
1677
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1176
1678
|
l = LongDecimal("-2.29")
|
1177
|
-
r = l.round_to_scale(1,
|
1679
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
1178
1680
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1179
1681
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1180
1682
|
l = LongDecimal("2.20")
|
1181
|
-
r = l.round_to_scale(1,
|
1683
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
1182
1684
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1183
1685
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1184
1686
|
l = LongDecimal("-2.20")
|
1185
|
-
r = l.round_to_scale(1,
|
1687
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
1186
1688
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1187
1689
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1188
1690
|
l = LongDecimal("2.24")
|
1189
|
-
r = l.round_to_scale(4,
|
1691
|
+
r = l.round_to_scale(4, ROUND_CEILING)
|
1190
1692
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1191
1693
|
end
|
1192
1694
|
|
@@ -1196,23 +1698,23 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1196
1698
|
def test_round_to_scale_floor
|
1197
1699
|
print "\ntest_round_to_scale_floor [#{Time.now}]: "
|
1198
1700
|
l = LongDecimal("2.29")
|
1199
|
-
r = l.round_to_scale(1,
|
1701
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
1200
1702
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1201
1703
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1202
1704
|
l = LongDecimal("-2.21")
|
1203
|
-
r = l.round_to_scale(1,
|
1705
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
1204
1706
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1205
1707
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1206
1708
|
l = LongDecimal("2.20")
|
1207
|
-
r = l.round_to_scale(1,
|
1709
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
1208
1710
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1209
1711
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1210
1712
|
l = LongDecimal("-2.20")
|
1211
|
-
r = l.round_to_scale(1,
|
1713
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
1212
1714
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1213
1715
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1214
1716
|
l = LongDecimal("2.24")
|
1215
|
-
r = l.round_to_scale(4,
|
1717
|
+
r = l.round_to_scale(4, ROUND_FLOOR)
|
1216
1718
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1217
1719
|
end
|
1218
1720
|
|
@@ -1222,39 +1724,39 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1222
1724
|
def test_round_to_scale_half_up
|
1223
1725
|
print "\ntest_round_to_scale_half_up [#{Time.now}]: "
|
1224
1726
|
l = LongDecimal("2.20")
|
1225
|
-
r = l.round_to_scale(1,
|
1727
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1226
1728
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1227
1729
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1228
1730
|
l = LongDecimal("2.21")
|
1229
|
-
r = l.round_to_scale(1,
|
1731
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1230
1732
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1231
1733
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1232
1734
|
l = LongDecimal("2.25")
|
1233
|
-
r = l.round_to_scale(1,
|
1735
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1234
1736
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1235
1737
|
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1236
1738
|
l = LongDecimal("2.29")
|
1237
|
-
r = l.round_to_scale(1,
|
1739
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1238
1740
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1239
1741
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1240
1742
|
l = LongDecimal("-2.20")
|
1241
|
-
r = l.round_to_scale(1,
|
1743
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1242
1744
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1243
1745
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1244
1746
|
l = LongDecimal("-2.21")
|
1245
|
-
r = l.round_to_scale(1,
|
1747
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1246
1748
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1247
1749
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1248
1750
|
l = LongDecimal("-2.25")
|
1249
|
-
r = l.round_to_scale(1,
|
1751
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1250
1752
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1251
1753
|
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1252
1754
|
l = LongDecimal("-2.29")
|
1253
|
-
r = l.round_to_scale(1,
|
1755
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
1254
1756
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1255
1757
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1256
1758
|
l = LongDecimal("2.24")
|
1257
|
-
r = l.round_to_scale(4,
|
1759
|
+
r = l.round_to_scale(4, ROUND_HALF_UP)
|
1258
1760
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1259
1761
|
end
|
1260
1762
|
|
@@ -1264,123 +1766,123 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1264
1766
|
def test_round_to_scale_half_down
|
1265
1767
|
print "\ntest_round_to_scale_half_down [#{Time.now}]: "
|
1266
1768
|
l = LongDecimal("2.20")
|
1267
|
-
r = l.round_to_scale(1,
|
1769
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1268
1770
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1269
1771
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1270
1772
|
l = LongDecimal("2.21")
|
1271
|
-
r = l.round_to_scale(1,
|
1773
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1272
1774
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1273
1775
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1274
1776
|
l = LongDecimal("2.25")
|
1275
|
-
r = l.round_to_scale(1,
|
1777
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1276
1778
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1277
1779
|
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1278
1780
|
l = LongDecimal("2.29")
|
1279
|
-
r = l.round_to_scale(1,
|
1781
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1280
1782
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1281
1783
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1282
1784
|
l = LongDecimal("-2.20")
|
1283
|
-
r = l.round_to_scale(1,
|
1785
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1284
1786
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1285
1787
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1286
1788
|
l = LongDecimal("-2.21")
|
1287
|
-
r = l.round_to_scale(1,
|
1789
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1288
1790
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1289
1791
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1290
1792
|
l = LongDecimal("-2.25")
|
1291
|
-
r = l.round_to_scale(1,
|
1793
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1292
1794
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1293
1795
|
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1294
1796
|
l = LongDecimal("-2.29")
|
1295
|
-
r = l.round_to_scale(1,
|
1797
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
1296
1798
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1297
1799
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1298
1800
|
l = LongDecimal("2.24")
|
1299
|
-
r = l.round_to_scale(4,
|
1801
|
+
r = l.round_to_scale(4, ROUND_HALF_DOWN)
|
1300
1802
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1301
1803
|
end
|
1302
1804
|
|
1303
1805
|
#
|
1304
1806
|
# test rounding with ROUND_HALF_CEILING
|
1305
1807
|
#
|
1306
|
-
def
|
1307
|
-
print "\
|
1808
|
+
def test_round_to_scale_half_ceiling
|
1809
|
+
print "\ntest_round_to_scale_half_ceiling [#{Time.now}]: "
|
1308
1810
|
l = LongDecimal("2.20")
|
1309
|
-
r = l.round_to_scale(1,
|
1811
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1310
1812
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1311
1813
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1312
1814
|
l = LongDecimal("2.21")
|
1313
|
-
r = l.round_to_scale(1,
|
1815
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1314
1816
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1315
1817
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1316
1818
|
l = LongDecimal("2.25")
|
1317
|
-
r = l.round_to_scale(1,
|
1819
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1318
1820
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1319
1821
|
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1320
1822
|
l = LongDecimal("2.29")
|
1321
|
-
r = l.round_to_scale(1,
|
1823
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1322
1824
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1323
1825
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1324
1826
|
l = LongDecimal("-2.20")
|
1325
|
-
r = l.round_to_scale(1,
|
1827
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1326
1828
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1327
1829
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1328
1830
|
l = LongDecimal("-2.21")
|
1329
|
-
r = l.round_to_scale(1,
|
1831
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1330
1832
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1331
1833
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1332
1834
|
l = LongDecimal("-2.25")
|
1333
|
-
r = l.round_to_scale(1,
|
1835
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1334
1836
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1335
1837
|
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1336
1838
|
l = LongDecimal("-2.29")
|
1337
|
-
r = l.round_to_scale(1,
|
1839
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
1338
1840
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1339
1841
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1340
1842
|
l = LongDecimal("2.24")
|
1341
|
-
r = l.round_to_scale(4,
|
1843
|
+
r = l.round_to_scale(4, ROUND_HALF_CEILING)
|
1342
1844
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1343
1845
|
end
|
1344
1846
|
|
1345
1847
|
#
|
1346
1848
|
# test rounding with ROUND_HALF_FLOOR
|
1347
1849
|
#
|
1348
|
-
def
|
1349
|
-
print "\
|
1850
|
+
def test_round_to_scale_half_floor
|
1851
|
+
print "\ntest_round_to_scale_half_floor [#{Time.now}]: "
|
1350
1852
|
l = LongDecimal("2.20")
|
1351
|
-
r = l.round_to_scale(1,
|
1853
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1352
1854
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1353
1855
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1354
1856
|
l = LongDecimal("2.21")
|
1355
|
-
r = l.round_to_scale(1,
|
1857
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1356
1858
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1357
1859
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1358
1860
|
l = LongDecimal("2.25")
|
1359
|
-
r = l.round_to_scale(1,
|
1861
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1360
1862
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1361
1863
|
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1362
1864
|
l = LongDecimal("2.29")
|
1363
|
-
r = l.round_to_scale(1,
|
1865
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1364
1866
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1365
1867
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1366
1868
|
l = LongDecimal("-2.20")
|
1367
|
-
r = l.round_to_scale(1,
|
1869
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1368
1870
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1369
1871
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1370
1872
|
l = LongDecimal("-2.21")
|
1371
|
-
r = l.round_to_scale(1,
|
1873
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1372
1874
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1373
1875
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1374
1876
|
l = LongDecimal("-2.25")
|
1375
|
-
r = l.round_to_scale(1,
|
1877
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1376
1878
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1377
1879
|
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1378
1880
|
l = LongDecimal("-2.29")
|
1379
|
-
r = l.round_to_scale(1,
|
1881
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
1380
1882
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1381
1883
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1382
1884
|
l = LongDecimal("2.24")
|
1383
|
-
r = l.round_to_scale(4,
|
1885
|
+
r = l.round_to_scale(4, ROUND_HALF_FLOOR)
|
1384
1886
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1385
1887
|
end
|
1386
1888
|
|
@@ -1390,64 +1892,755 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1390
1892
|
def test_round_to_scale_half_even
|
1391
1893
|
print "\ntest_round_to_scale_half_even [#{Time.now}]: "
|
1392
1894
|
l = LongDecimal("2.20")
|
1393
|
-
r = l.round_to_scale(1,
|
1895
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1394
1896
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1395
1897
|
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1396
1898
|
l = LongDecimal("2.21")
|
1397
|
-
r = l.round_to_scale(1,
|
1899
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1398
1900
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1399
1901
|
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1400
1902
|
l = LongDecimal("2.25")
|
1401
|
-
r = l.round_to_scale(1,
|
1903
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1402
1904
|
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1403
1905
|
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1404
1906
|
l = LongDecimal("2.35")
|
1405
|
-
r = l.round_to_scale(1,
|
1907
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1406
1908
|
assert_equal("2.4", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1407
1909
|
assert_equal("2.35", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1408
1910
|
l = LongDecimal("2.29")
|
1409
|
-
r = l.round_to_scale(1,
|
1911
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1410
1912
|
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1411
1913
|
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1412
1914
|
l = LongDecimal("-2.20")
|
1413
|
-
r = l.round_to_scale(1,
|
1915
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1414
1916
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1415
1917
|
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1416
1918
|
l = LongDecimal("-2.21")
|
1417
|
-
r = l.round_to_scale(1,
|
1919
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1418
1920
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1419
1921
|
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1420
1922
|
l = LongDecimal("-2.25")
|
1421
|
-
r = l.round_to_scale(1,
|
1923
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1422
1924
|
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1423
1925
|
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1424
1926
|
l = LongDecimal("-2.35")
|
1425
|
-
r = l.round_to_scale(1,
|
1927
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1426
1928
|
assert_equal("-2.4", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1427
1929
|
assert_equal("-2.35", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1428
1930
|
l = LongDecimal("-2.29")
|
1429
|
-
r = l.round_to_scale(1,
|
1931
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
1430
1932
|
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1431
1933
|
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1432
1934
|
l = LongDecimal("2.24")
|
1433
|
-
r = l.round_to_scale(4,
|
1935
|
+
r = l.round_to_scale(4, ROUND_HALF_EVEN)
|
1434
1936
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1435
1937
|
end
|
1436
1938
|
|
1939
|
+
#
|
1940
|
+
# test rounding with ROUND_HALF_ODD
|
1941
|
+
#
|
1942
|
+
def test_round_to_scale_half_odd
|
1943
|
+
print "\ntest_round_to_scale_half_odd [#{Time.now}]: "
|
1944
|
+
l = LongDecimal("2.20")
|
1945
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1946
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1947
|
+
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1948
|
+
l = LongDecimal("2.21")
|
1949
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1950
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1951
|
+
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1952
|
+
l = LongDecimal("2.25")
|
1953
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1954
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1955
|
+
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1956
|
+
l = LongDecimal("2.35")
|
1957
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1958
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1959
|
+
assert_equal("2.35", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1960
|
+
l = LongDecimal("2.29")
|
1961
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1962
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1963
|
+
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1964
|
+
l = LongDecimal("-2.20")
|
1965
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1966
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1967
|
+
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1968
|
+
l = LongDecimal("-2.21")
|
1969
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1970
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1971
|
+
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1972
|
+
l = LongDecimal("-2.25")
|
1973
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1974
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1975
|
+
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1976
|
+
l = LongDecimal("-2.35")
|
1977
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1978
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1979
|
+
assert_equal("-2.35", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1980
|
+
l = LongDecimal("-2.29")
|
1981
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
1982
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1983
|
+
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1984
|
+
l = LongDecimal("2.24")
|
1985
|
+
r = l.round_to_scale(4, ROUND_HALF_ODD)
|
1986
|
+
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1987
|
+
end
|
1988
|
+
|
1989
|
+
#
|
1990
|
+
# test rounding with ROUND_GEOMETRIC_*
|
1991
|
+
#
|
1992
|
+
def test_round_to_scale_geometric_common
|
1993
|
+
print "\ntest_round_to_scale_geometric_common [#{Time.now}]: "
|
1994
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
1995
|
+
if (rounding_mode.major == MAJOR_GEOMETRIC)
|
1996
|
+
l = LongDecimal("0.00")
|
1997
|
+
r = l.round_to_scale(1, rounding_mode)
|
1998
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1999
|
+
assert_equal("0.00", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2000
|
+
l = LongDecimal("0.01")
|
2001
|
+
r = l.round_to_scale(1, rounding_mode)
|
2002
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2003
|
+
assert_equal("0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2004
|
+
l = LongDecimal("0.000001")
|
2005
|
+
r = l.round_to_scale(1, rounding_mode)
|
2006
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2007
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2008
|
+
r = l.round_to_scale(0, rounding_mode)
|
2009
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2010
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2011
|
+
l = LongDecimal("0.099999")
|
2012
|
+
r = l.round_to_scale(1, rounding_mode)
|
2013
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2014
|
+
assert_equal("0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2015
|
+
l = LongDecimal("-0.01")
|
2016
|
+
r = l.round_to_scale(1, rounding_mode)
|
2017
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2018
|
+
assert_equal("-0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2019
|
+
l = LongDecimal("-0.000001")
|
2020
|
+
r = l.round_to_scale(1, rounding_mode)
|
2021
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2022
|
+
assert_equal("-0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2023
|
+
r = l.round_to_scale(0, rounding_mode)
|
2024
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2025
|
+
l = LongDecimal("-0.099999")
|
2026
|
+
r = l.round_to_scale(1, rounding_mode)
|
2027
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2028
|
+
assert_equal("-0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2029
|
+
l = LongDecimal("1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641")
|
2030
|
+
r = l.round_to_scale(0, rounding_mode)
|
2031
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2032
|
+
assert_equal("1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2033
|
+
l = LongDecimal("1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327642")
|
2034
|
+
r = l.round_to_scale(0, rounding_mode)
|
2035
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2036
|
+
assert_equal("1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327642", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2037
|
+
l = LongDecimal("-1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641")
|
2038
|
+
r = l.round_to_scale(0, rounding_mode)
|
2039
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2040
|
+
assert_equal("-1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2041
|
+
l = LongDecimal("-1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327642")
|
2042
|
+
r = l.round_to_scale(0, rounding_mode)
|
2043
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2044
|
+
assert_equal("-1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327642", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2045
|
+
|
2046
|
+
l = LongDecimal("2.20")
|
2047
|
+
r = l.round_to_scale(1, rounding_mode)
|
2048
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2049
|
+
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2050
|
+
l = LongDecimal("2.21")
|
2051
|
+
r = l.round_to_scale(1, rounding_mode)
|
2052
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2053
|
+
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2054
|
+
l = LongDecimal("2.25")
|
2055
|
+
r = l.round_to_scale(1, rounding_mode)
|
2056
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2057
|
+
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2058
|
+
l = LongDecimal("2.29")
|
2059
|
+
r = l.round_to_scale(1, rounding_mode)
|
2060
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2061
|
+
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2062
|
+
l = LongDecimal("-2.20")
|
2063
|
+
r = l.round_to_scale(1, rounding_mode)
|
2064
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2065
|
+
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2066
|
+
l = LongDecimal("-2.21")
|
2067
|
+
r = l.round_to_scale(1, rounding_mode)
|
2068
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2069
|
+
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2070
|
+
l = LongDecimal("-2.25")
|
2071
|
+
r = l.round_to_scale(1, rounding_mode)
|
2072
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2073
|
+
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2074
|
+
l = LongDecimal("-2.29")
|
2075
|
+
r = l.round_to_scale(1, rounding_mode)
|
2076
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2077
|
+
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2078
|
+
l = LongDecimal("2.24")
|
2079
|
+
r = l.round_to_scale(4, rounding_mode)
|
2080
|
+
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2081
|
+
end
|
2082
|
+
end
|
2083
|
+
end
|
2084
|
+
|
2085
|
+
#
|
2086
|
+
# test rounding with ROUND_HARMONIC_*
|
2087
|
+
#
|
2088
|
+
def test_round_to_scale_harmonic_common
|
2089
|
+
print "\ntest_round_to_scale_harmonic_common [#{Time.now}]: "
|
2090
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2091
|
+
if (rounding_mode.major == MAJOR_HARMONIC)
|
2092
|
+
l = LongDecimal("0.00")
|
2093
|
+
r = l.round_to_scale(1, rounding_mode)
|
2094
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2095
|
+
assert_equal("0.00", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2096
|
+
l = LongDecimal("0.01")
|
2097
|
+
r = l.round_to_scale(1, rounding_mode)
|
2098
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2099
|
+
assert_equal("0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2100
|
+
l = LongDecimal("0.000001")
|
2101
|
+
r = l.round_to_scale(1, rounding_mode)
|
2102
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2103
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2104
|
+
r = l.round_to_scale(0, rounding_mode)
|
2105
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2106
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2107
|
+
l = LongDecimal("0.099999")
|
2108
|
+
r = l.round_to_scale(1, rounding_mode)
|
2109
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2110
|
+
assert_equal("0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2111
|
+
l = LongDecimal("-0.01")
|
2112
|
+
r = l.round_to_scale(1, rounding_mode)
|
2113
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2114
|
+
assert_equal("-0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2115
|
+
l = LongDecimal("-0.000001")
|
2116
|
+
r = l.round_to_scale(1, rounding_mode)
|
2117
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2118
|
+
assert_equal("-0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2119
|
+
r = l.round_to_scale(0, rounding_mode)
|
2120
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2121
|
+
l = LongDecimal("-0.099999")
|
2122
|
+
r = l.round_to_scale(1, rounding_mode)
|
2123
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2124
|
+
assert_equal("-0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2125
|
+
|
2126
|
+
# harmonic mean of 1 and 2 is 4/3
|
2127
|
+
l = LongDecimal("1.33333333333333333333333333333333333333333333")
|
2128
|
+
r = l.round_to_scale(0, rounding_mode)
|
2129
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect} rounding_mode=#{rounding_mode}")
|
2130
|
+
assert_equal("1.33333333333333333333333333333333333333333333", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2131
|
+
l = LongDecimal("1.33333333333333333333333333333333333333333334")
|
2132
|
+
r = l.round_to_scale(0, rounding_mode)
|
2133
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2134
|
+
assert_equal("1.33333333333333333333333333333333333333333334", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2135
|
+
l = LongDecimal("-1.33333333333333333333333333333333333333333333")
|
2136
|
+
r = l.round_to_scale(0, rounding_mode)
|
2137
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2138
|
+
assert_equal("-1.33333333333333333333333333333333333333333333", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2139
|
+
l = LongDecimal("-1.33333333333333333333333333333333333333333334")
|
2140
|
+
r = l.round_to_scale(0, rounding_mode)
|
2141
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2142
|
+
assert_equal("-1.33333333333333333333333333333333333333333334", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2143
|
+
|
2144
|
+
l = LongDecimal("2.20")
|
2145
|
+
r = l.round_to_scale(1, rounding_mode)
|
2146
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2147
|
+
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2148
|
+
l = LongDecimal("2.21")
|
2149
|
+
r = l.round_to_scale(1, rounding_mode)
|
2150
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2151
|
+
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2152
|
+
l = LongDecimal("2.25")
|
2153
|
+
r = l.round_to_scale(1, rounding_mode)
|
2154
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2155
|
+
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2156
|
+
l = LongDecimal("2.29")
|
2157
|
+
r = l.round_to_scale(1, rounding_mode)
|
2158
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2159
|
+
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2160
|
+
l = LongDecimal("-2.20")
|
2161
|
+
r = l.round_to_scale(1, rounding_mode)
|
2162
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2163
|
+
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2164
|
+
l = LongDecimal("-2.21")
|
2165
|
+
r = l.round_to_scale(1, rounding_mode)
|
2166
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2167
|
+
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2168
|
+
l = LongDecimal("-2.25")
|
2169
|
+
r = l.round_to_scale(1, rounding_mode)
|
2170
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2171
|
+
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2172
|
+
l = LongDecimal("-2.29")
|
2173
|
+
r = l.round_to_scale(1, rounding_mode)
|
2174
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2175
|
+
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2176
|
+
l = LongDecimal("2.24")
|
2177
|
+
r = l.round_to_scale(4, rounding_mode)
|
2178
|
+
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2179
|
+
end
|
2180
|
+
end
|
2181
|
+
end
|
2182
|
+
|
2183
|
+
#
|
2184
|
+
# test rounding with ROUND_HARMONIC_UP
|
2185
|
+
#
|
2186
|
+
def test_round_to_scale_harmonic_up
|
2187
|
+
print "\ntest_round_to_scale_harmonic_up [#{Time.now}]: "
|
2188
|
+
l = LongDecimal("2.39")
|
2189
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2190
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2191
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2192
|
+
l = LongDecimal("2.40")
|
2193
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2194
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2195
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2196
|
+
l = LongDecimal("2.41")
|
2197
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2198
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2199
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2200
|
+
l = LongDecimal("-2.39")
|
2201
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2202
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2203
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2204
|
+
l = LongDecimal("-2.40")
|
2205
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2206
|
+
assert_equal("-3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2207
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2208
|
+
l = LongDecimal("12.47999")
|
2209
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2210
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2211
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2212
|
+
l = LongDecimal("12.48")
|
2213
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2214
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2215
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2216
|
+
l = LongDecimal("12.48001")
|
2217
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_UP)
|
2218
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2219
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2220
|
+
end
|
2221
|
+
|
2222
|
+
#
|
2223
|
+
# test rounding with ROUND_HARMONIC_DOWN
|
2224
|
+
#
|
2225
|
+
def test_round_to_scale_harmonic_down
|
2226
|
+
print "\ntest_round_to_scale_harmonic_down [#{Time.now}]: "
|
2227
|
+
l = LongDecimal("2.39")
|
2228
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2229
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2230
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2231
|
+
l = LongDecimal("2.40")
|
2232
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2233
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2234
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2235
|
+
l = LongDecimal("2.41")
|
2236
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2237
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2238
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2239
|
+
l = LongDecimal("-2.39")
|
2240
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2241
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2242
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2243
|
+
l = LongDecimal("-2.40")
|
2244
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2245
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2246
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2247
|
+
l = LongDecimal("12.47999")
|
2248
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2249
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2250
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2251
|
+
l = LongDecimal("12.48")
|
2252
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2253
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2254
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2255
|
+
l = LongDecimal("12.48001")
|
2256
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_DOWN)
|
2257
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2258
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2259
|
+
end
|
2260
|
+
|
2261
|
+
#
|
2262
|
+
# test rounding with ROUND_HARMONIC_CEILING
|
2263
|
+
#
|
2264
|
+
def test_round_to_scale_harmonic_ceiling
|
2265
|
+
print "\ntest_round_to_scale_harmonic_ceiling [#{Time.now}]: "
|
2266
|
+
l = LongDecimal("2.39")
|
2267
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2268
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2269
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2270
|
+
l = LongDecimal("2.40")
|
2271
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2272
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2273
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2274
|
+
l = LongDecimal("2.41")
|
2275
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2276
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2277
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2278
|
+
l = LongDecimal("-2.39")
|
2279
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2280
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2281
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2282
|
+
l = LongDecimal("-2.40")
|
2283
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2284
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2285
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2286
|
+
l = LongDecimal("12.47999")
|
2287
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2288
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2289
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2290
|
+
l = LongDecimal("12.48")
|
2291
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2292
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2293
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2294
|
+
l = LongDecimal("12.48001")
|
2295
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_CEILING)
|
2296
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2297
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2298
|
+
end
|
2299
|
+
|
2300
|
+
#
|
2301
|
+
# test rounding with ROUND_HARMONIC_FLOOR
|
2302
|
+
#
|
2303
|
+
def test_round_to_scale_harmonic_floor
|
2304
|
+
print "\ntest_round_to_scale_harmonic_floor [#{Time.now}]: "
|
2305
|
+
l = LongDecimal("2.39")
|
2306
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2307
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2308
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2309
|
+
l = LongDecimal("2.40")
|
2310
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2311
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2312
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2313
|
+
l = LongDecimal("2.41")
|
2314
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2315
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2316
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2317
|
+
l = LongDecimal("-2.39")
|
2318
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2319
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2320
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2321
|
+
l = LongDecimal("-2.40")
|
2322
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2323
|
+
assert_equal("-3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2324
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2325
|
+
l = LongDecimal("12.47999")
|
2326
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2327
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2328
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2329
|
+
l = LongDecimal("12.48")
|
2330
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2331
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2332
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2333
|
+
l = LongDecimal("12.48001")
|
2334
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_FLOOR)
|
2335
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2336
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2337
|
+
end
|
2338
|
+
|
2339
|
+
#
|
2340
|
+
# test rounding with ROUND_HARMONIC_EVEN
|
2341
|
+
#
|
2342
|
+
def test_round_to_scale_harmonic_even
|
2343
|
+
print "\ntest_round_to_scale_harmonic_even [#{Time.now}]: "
|
2344
|
+
l = LongDecimal("2.39")
|
2345
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2346
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2347
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2348
|
+
l = LongDecimal("2.40")
|
2349
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2350
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2351
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2352
|
+
l = LongDecimal("2.41")
|
2353
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2354
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2355
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2356
|
+
l = LongDecimal("-2.39")
|
2357
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2358
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2359
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2360
|
+
l = LongDecimal("-2.40")
|
2361
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2362
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2363
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2364
|
+
l = LongDecimal("12.47999")
|
2365
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2366
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2367
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2368
|
+
l = LongDecimal("12.48")
|
2369
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2370
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2371
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2372
|
+
l = LongDecimal("12.48001")
|
2373
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_EVEN)
|
2374
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2375
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2376
|
+
end
|
2377
|
+
|
2378
|
+
#
|
2379
|
+
# test rounding with ROUND_HARMONIC_ODD
|
2380
|
+
#
|
2381
|
+
def test_round_to_scale_harmonic_even
|
2382
|
+
print "\ntest_round_to_scale_harmonic_even [#{Time.now}]: "
|
2383
|
+
l = LongDecimal("2.39")
|
2384
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2385
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2386
|
+
assert_equal("2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2387
|
+
l = LongDecimal("2.40")
|
2388
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2389
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2390
|
+
assert_equal("2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2391
|
+
l = LongDecimal("2.41")
|
2392
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2393
|
+
assert_equal("3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2394
|
+
assert_equal("2.41", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2395
|
+
l = LongDecimal("-2.39")
|
2396
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2397
|
+
assert_equal("-2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2398
|
+
assert_equal("-2.39", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2399
|
+
l = LongDecimal("-2.40")
|
2400
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2401
|
+
assert_equal("-3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2402
|
+
assert_equal("-2.40", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2403
|
+
l = LongDecimal("12.47999")
|
2404
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2405
|
+
assert_equal("12", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2406
|
+
assert_equal("12.47999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2407
|
+
l = LongDecimal("12.48")
|
2408
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2409
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2410
|
+
assert_equal("12.48", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2411
|
+
l = LongDecimal("12.48001")
|
2412
|
+
r = l.round_to_scale(0, ROUND_HARMONIC_ODD)
|
2413
|
+
assert_equal("13", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2414
|
+
assert_equal("12.48001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2415
|
+
end
|
2416
|
+
|
2417
|
+
#
|
2418
|
+
# test rounding with ROUND_QUADRATIC_*
|
2419
|
+
#
|
2420
|
+
def test_round_to_scale_quadratic_common
|
2421
|
+
print "\ntest_round_to_scale_quadratic_common [#{Time.now}]: "
|
2422
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2423
|
+
if (rounding_mode.major == MAJOR_QUADRATIC)
|
2424
|
+
l = LongDecimal("0.00")
|
2425
|
+
r = l.round_to_scale(1, rounding_mode)
|
2426
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2427
|
+
assert_equal("0.00", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2428
|
+
l = LongDecimal("0.01")
|
2429
|
+
r = l.round_to_scale(1, rounding_mode)
|
2430
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2431
|
+
assert_equal("0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2432
|
+
l = LongDecimal("0.000001")
|
2433
|
+
r = l.round_to_scale(1, rounding_mode)
|
2434
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2435
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2436
|
+
r = l.round_to_scale(0, rounding_mode)
|
2437
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2438
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2439
|
+
l = LongDecimal("0.099999")
|
2440
|
+
r = l.round_to_scale(1, rounding_mode)
|
2441
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2442
|
+
assert_equal("0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2443
|
+
l = LongDecimal("-0.01")
|
2444
|
+
r = l.round_to_scale(1, rounding_mode)
|
2445
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2446
|
+
assert_equal("-0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2447
|
+
l = LongDecimal("-0.000001")
|
2448
|
+
r = l.round_to_scale(1, rounding_mode)
|
2449
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2450
|
+
assert_equal("-0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2451
|
+
r = l.round_to_scale(0, rounding_mode)
|
2452
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2453
|
+
l = LongDecimal("-0.099999")
|
2454
|
+
r = l.round_to_scale(1, rounding_mode)
|
2455
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2456
|
+
assert_equal("-0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2457
|
+
|
2458
|
+
# sqrt(2) is the quadratic mean of 0 and 2: 0.7071067811865475244008443621048490392848359376884740365883398689953662392310535194251937671638207864
|
2459
|
+
l = LongDecimal("0.7071067811")
|
2460
|
+
r = l.round_to_scale(0, rounding_mode)
|
2461
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect} rounding_mode=#{rounding_mode}")
|
2462
|
+
assert_equal("0.7071067811", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2463
|
+
l = LongDecimal("0.7071067812")
|
2464
|
+
r = l.round_to_scale(0, rounding_mode)
|
2465
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2466
|
+
assert_equal("0.7071067812", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2467
|
+
l = LongDecimal("-0.7071067811")
|
2468
|
+
r = l.round_to_scale(0, rounding_mode)
|
2469
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2470
|
+
assert_equal("-0.7071067811", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2471
|
+
l = LongDecimal("-0.7071067812")
|
2472
|
+
r = l.round_to_scale(0, rounding_mode)
|
2473
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2474
|
+
assert_equal("-0.7071067812", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2475
|
+
|
2476
|
+
l = LongDecimal("1.5811388300")
|
2477
|
+
r = l.round_to_scale(0, rounding_mode)
|
2478
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2479
|
+
assert_equal("1.5811388300", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2480
|
+
l = LongDecimal("1.5811388301")
|
2481
|
+
r = l.round_to_scale(0, rounding_mode)
|
2482
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2483
|
+
assert_equal("1.5811388301", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2484
|
+
|
2485
|
+
l = LongDecimal("2.20")
|
2486
|
+
r = l.round_to_scale(1, rounding_mode)
|
2487
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2488
|
+
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2489
|
+
l = LongDecimal("2.21")
|
2490
|
+
r = l.round_to_scale(1, rounding_mode)
|
2491
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2492
|
+
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2493
|
+
l = LongDecimal("2.25")
|
2494
|
+
r = l.round_to_scale(1, rounding_mode)
|
2495
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2496
|
+
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2497
|
+
l = LongDecimal("2.29")
|
2498
|
+
r = l.round_to_scale(1, rounding_mode)
|
2499
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2500
|
+
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2501
|
+
l = LongDecimal("-2.20")
|
2502
|
+
r = l.round_to_scale(1, rounding_mode)
|
2503
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2504
|
+
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2505
|
+
l = LongDecimal("-2.21")
|
2506
|
+
r = l.round_to_scale(1, rounding_mode)
|
2507
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2508
|
+
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2509
|
+
l = LongDecimal("-2.25")
|
2510
|
+
r = l.round_to_scale(1, rounding_mode)
|
2511
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2512
|
+
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2513
|
+
l = LongDecimal("-2.29")
|
2514
|
+
r = l.round_to_scale(1, rounding_mode)
|
2515
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2516
|
+
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2517
|
+
l = LongDecimal("2.24")
|
2518
|
+
r = l.round_to_scale(4, rounding_mode)
|
2519
|
+
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2520
|
+
end
|
2521
|
+
end
|
2522
|
+
end
|
2523
|
+
|
2524
|
+
#
|
2525
|
+
# test rounding with ROUND_CUBIC_*
|
2526
|
+
#
|
2527
|
+
def test_round_to_scale_cubic_common
|
2528
|
+
print "\ntest_round_to_scale_cubic_common [#{Time.now}]: "
|
2529
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2530
|
+
if (rounding_mode.major == MAJOR_CUBIC)
|
2531
|
+
l = LongDecimal("0.00")
|
2532
|
+
r = l.round_to_scale(1, rounding_mode)
|
2533
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2534
|
+
assert_equal("0.00", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2535
|
+
l = LongDecimal("0.01")
|
2536
|
+
r = l.round_to_scale(1, rounding_mode)
|
2537
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2538
|
+
assert_equal("0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2539
|
+
l = LongDecimal("0.000001")
|
2540
|
+
r = l.round_to_scale(1, rounding_mode)
|
2541
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2542
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2543
|
+
r = l.round_to_scale(0, rounding_mode)
|
2544
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2545
|
+
assert_equal("0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2546
|
+
l = LongDecimal("0.099999")
|
2547
|
+
r = l.round_to_scale(1, rounding_mode)
|
2548
|
+
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2549
|
+
assert_equal("0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2550
|
+
l = LongDecimal("-0.01")
|
2551
|
+
r = l.round_to_scale(1, rounding_mode)
|
2552
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2553
|
+
assert_equal("-0.01", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2554
|
+
l = LongDecimal("-0.000001")
|
2555
|
+
r = l.round_to_scale(1, rounding_mode)
|
2556
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2557
|
+
assert_equal("-0.000001", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2558
|
+
r = l.round_to_scale(0, rounding_mode)
|
2559
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2560
|
+
l = LongDecimal("-0.099999")
|
2561
|
+
r = l.round_to_scale(1, rounding_mode)
|
2562
|
+
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2563
|
+
assert_equal("-0.099999", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2564
|
+
|
2565
|
+
l = LongDecimal("0.7937005259")
|
2566
|
+
r = l.round_to_scale(0, rounding_mode)
|
2567
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2568
|
+
assert_equal("0.7937005259", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2569
|
+
l = LongDecimal("0.7937005260")
|
2570
|
+
r = l.round_to_scale(0, rounding_mode)
|
2571
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2572
|
+
assert_equal("0.7937005260", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2573
|
+
l = LongDecimal("-0.7937005259")
|
2574
|
+
r = l.round_to_scale(0, rounding_mode)
|
2575
|
+
assert_equal("0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2576
|
+
assert_equal("-0.7937005259", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2577
|
+
l = LongDecimal("-0.7937005260")
|
2578
|
+
r = l.round_to_scale(0, rounding_mode)
|
2579
|
+
assert_equal("-1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2580
|
+
assert_equal("-0.7937005260", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2581
|
+
|
2582
|
+
l = LongDecimal("1.6509636244")
|
2583
|
+
r = l.round_to_scale(0, rounding_mode)
|
2584
|
+
assert_equal("1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2585
|
+
assert_equal("1.6509636244", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2586
|
+
l = LongDecimal("1.6509636245")
|
2587
|
+
r = l.round_to_scale(0, rounding_mode)
|
2588
|
+
assert_equal("2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2589
|
+
assert_equal("1.6509636245", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2590
|
+
|
2591
|
+
l = LongDecimal("2.20")
|
2592
|
+
r = l.round_to_scale(1, rounding_mode)
|
2593
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2594
|
+
assert_equal("2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2595
|
+
l = LongDecimal("2.21")
|
2596
|
+
r = l.round_to_scale(1, rounding_mode)
|
2597
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2598
|
+
assert_equal("2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2599
|
+
l = LongDecimal("2.25")
|
2600
|
+
r = l.round_to_scale(1, rounding_mode)
|
2601
|
+
assert_equal("2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2602
|
+
assert_equal("2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2603
|
+
l = LongDecimal("2.29")
|
2604
|
+
r = l.round_to_scale(1, rounding_mode)
|
2605
|
+
assert_equal("2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2606
|
+
assert_equal("2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2607
|
+
l = LongDecimal("-2.20")
|
2608
|
+
r = l.round_to_scale(1, rounding_mode)
|
2609
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2610
|
+
assert_equal("-2.20", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2611
|
+
l = LongDecimal("-2.21")
|
2612
|
+
r = l.round_to_scale(1, rounding_mode)
|
2613
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2614
|
+
assert_equal("-2.21", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2615
|
+
l = LongDecimal("-2.25")
|
2616
|
+
r = l.round_to_scale(1, rounding_mode)
|
2617
|
+
assert_equal("-2.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2618
|
+
assert_equal("-2.25", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2619
|
+
l = LongDecimal("-2.29")
|
2620
|
+
r = l.round_to_scale(1, rounding_mode)
|
2621
|
+
assert_equal("-2.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2622
|
+
assert_equal("-2.29", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2623
|
+
l = LongDecimal("2.24")
|
2624
|
+
r = l.round_to_scale(4, rounding_mode)
|
2625
|
+
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
2626
|
+
end
|
2627
|
+
end
|
2628
|
+
end
|
2629
|
+
|
1437
2630
|
#
|
1438
2631
|
# test rounding with ROUND_UNNECESSARY
|
1439
2632
|
#
|
1440
2633
|
def test_round_to_scale_unnecessary
|
1441
2634
|
print "\ntest_round_to_scale_unnecessary [#{Time.now}]: "
|
1442
2635
|
l = LongDecimal("2.24")
|
1443
|
-
r = l.round_to_scale(4,
|
2636
|
+
r = l.round_to_scale(4, ROUND_UNNECESSARY)
|
1444
2637
|
assert_equal("2.2400", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1445
2638
|
l = LongDecimal("2.2400")
|
1446
|
-
r = l.round_to_scale(2,
|
2639
|
+
r = l.round_to_scale(2, ROUND_UNNECESSARY)
|
1447
2640
|
assert_equal("2.24", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
1448
2641
|
begin
|
1449
2642
|
l = LongDecimal("2.24")
|
1450
|
-
r = l.round_to_scale(1,
|
2643
|
+
r = l.round_to_scale(1, ROUND_UNNECESSARY)
|
1451
2644
|
assert_fail("should not have succeeded l=#{l.inspect} r=#{r.inspect}")
|
1452
2645
|
rescue ArgumentError
|
1453
2646
|
# ignored
|
@@ -1458,23 +2651,23 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1458
2651
|
# test rounding of int to remainder set
|
1459
2652
|
#
|
1460
2653
|
def test_int_round_to_one_allowed_remainder
|
1461
|
-
print "\ntest_int_round_to_one_allowed_remainder [#{Time.now}] (
|
2654
|
+
print "\ntest_int_round_to_one_allowed_remainder [#{Time.now}] (20sec): "
|
1462
2655
|
$stdout.flush
|
1463
2656
|
2.upto 20 do |modulus|
|
1464
2657
|
0.upto modulus-1 do |r|
|
2658
|
+
print "."
|
1465
2659
|
n = 3*modulus
|
1466
2660
|
(-n).upto n do |i|
|
1467
2661
|
text = "i=#{i} n=#{n} m=#{modulus} r=#{r}"
|
1468
|
-
print ":"
|
1469
2662
|
$stdout.flush
|
1470
2663
|
|
1471
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2664
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_UP, ZERO_ROUND_TO_PLUS)
|
1472
2665
|
assert(i_rounded.abs >= i.abs, "i_r=#{i_rounded} " + text)
|
1473
2666
|
if (i == 0) then
|
1474
2667
|
assert(i_rounded >= 0, "i_r=#{i_rounded} " + text)
|
1475
2668
|
end
|
1476
2669
|
|
1477
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2670
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_DOWN, ZERO_ROUND_TO_PLUS)
|
1478
2671
|
if (i > 0)
|
1479
2672
|
assert(i_rounded <= i, "i_r=#{i_rounded} " + text)
|
1480
2673
|
elsif (i < 0)
|
@@ -1485,39 +2678,75 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1485
2678
|
raise("i=#{i} i_r=#{i_rounded}")
|
1486
2679
|
end
|
1487
2680
|
|
1488
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2681
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_CEILING, ZERO_ROUND_TO_PLUS)
|
1489
2682
|
assert(i_rounded >= i, "i_r=#{i_rounded} " + text)
|
1490
2683
|
|
1491
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2684
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_FLOOR, ZERO_ROUND_TO_PLUS)
|
1492
2685
|
assert(i_rounded <= i, "i_r=#{i_rounded} " + text)
|
1493
2686
|
|
1494
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2687
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_PLUS)
|
1495
2688
|
dd = 2*(i_rounded - i).abs
|
1496
2689
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1497
2690
|
if (i_rounded.abs < i.abs || i_rounded.sgn == - i.sgn)
|
1498
2691
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1499
2692
|
end
|
1500
2693
|
|
1501
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2694
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_PLUS)
|
1502
2695
|
dd = 2*(i_rounded - i).abs
|
1503
2696
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1504
2697
|
if (i_rounded.abs > i.abs && i_rounded.sgn == i.sgn)
|
1505
2698
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1506
2699
|
end
|
1507
2700
|
|
1508
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2701
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_HALF_CEILING, ZERO_ROUND_TO_PLUS)
|
1509
2702
|
dd = 2*(i_rounded - i).abs
|
1510
2703
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1511
2704
|
if (i_rounded < i)
|
1512
2705
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1513
2706
|
end
|
1514
2707
|
|
1515
|
-
i_rounded = check_round_to_one_remainder(i, r, modulus,
|
2708
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, ROUND_HALF_FLOOR, ZERO_ROUND_TO_PLUS)
|
1516
2709
|
dd = 2*(i_rounded - i).abs
|
1517
2710
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1518
2711
|
if (i_rounded > i && i != 0)
|
1519
2712
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1520
2713
|
end
|
2714
|
+
|
2715
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2716
|
+
unless (rounding_mode.major == MAJOR_GEOMETRIC \
|
2717
|
+
|| rounding_mode.major == MAJOR_HARMONIC \
|
2718
|
+
|| rounding_mode.major == MAJOR_QUADRATIC \
|
2719
|
+
|| rounding_mode.major == MAJOR_CUBIC)
|
2720
|
+
next;
|
2721
|
+
end
|
2722
|
+
if (rounding_mode.minor == MINOR_EVEN || rounding_mode.minor == MINOR_ODD)
|
2723
|
+
next
|
2724
|
+
end
|
2725
|
+
i_rounded = check_round_to_one_remainder(i, r, modulus, rounding_mode, ZERO_ROUND_TO_PLUS)
|
2726
|
+
end
|
2727
|
+
end
|
2728
|
+
end
|
2729
|
+
end
|
2730
|
+
end
|
2731
|
+
|
2732
|
+
#
|
2733
|
+
# test rounding of int to remainder set
|
2734
|
+
#
|
2735
|
+
def test_zero_round_to_zero_as_allowed_remainder
|
2736
|
+
print "\ntest_zero_round_to_zero_as_allowed_remainder [#{Time.now}]: "
|
2737
|
+
|
2738
|
+
2.upto 20 do |modulus|
|
2739
|
+
text = "m=#{modulus}"
|
2740
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2741
|
+
if (rounding_mode.minor == MINOR_EVEN || rounding_mode.minor == MINOR_ODD)
|
2742
|
+
next
|
2743
|
+
end
|
2744
|
+
if (rounding_mode.major == MAJOR_UNNECESSARY)
|
2745
|
+
next
|
2746
|
+
end
|
2747
|
+
ALL_ZERO_MODES.each do |zero_mode|
|
2748
|
+
zero_rounded = 0.round_to_allowed_remainders([0], modulus, rounding_mode, zero_mode)
|
2749
|
+
assert_equal(0, zero_rounded)
|
1521
2750
|
end
|
1522
2751
|
end
|
1523
2752
|
end
|
@@ -1532,104 +2761,91 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1532
2761
|
0.upto modulus-1 do |r|
|
1533
2762
|
text = "m=#{modulus} r=#{r}"
|
1534
2763
|
|
1535
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2764
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_UP, ZERO_ROUND_TO_PLUS)
|
1536
2765
|
assert(zero_r >= 0, "0_r=#{zero_r} " + text)
|
1537
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2766
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_UP, ZERO_ROUND_TO_MINUS)
|
1538
2767
|
assert(zero_r <= 0, "0_r=#{zero_r} " + text)
|
1539
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2768
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_UP, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
1540
2769
|
dd = 2*zero_r.abs
|
1541
2770
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1542
2771
|
if (zero_r < 0)
|
1543
2772
|
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1544
2773
|
end
|
1545
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2774
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_UP, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
1546
2775
|
dd = 2*zero_r.abs
|
1547
2776
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1548
2777
|
if (zero_r > 0)
|
1549
2778
|
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1550
2779
|
end
|
1551
2780
|
|
1552
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2781
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_DOWN, ZERO_ROUND_TO_PLUS)
|
1553
2782
|
assert(zero_r >= 0, "0_r=#{zero_r} " + text)
|
1554
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2783
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_DOWN, ZERO_ROUND_TO_MINUS)
|
1555
2784
|
assert(zero_r <= 0, "0_r=#{zero_r} " + text)
|
1556
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2785
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
1557
2786
|
dd = 2*zero_r.abs
|
1558
2787
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1559
2788
|
if (zero_r < 0)
|
1560
2789
|
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1561
2790
|
end
|
1562
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2791
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
1563
2792
|
dd = 2*zero_r.abs
|
1564
2793
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1565
2794
|
if (zero_r > 0)
|
1566
2795
|
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1567
2796
|
end
|
1568
2797
|
|
1569
|
-
|
2798
|
+
# ceiling always rounds toward positive infinity, so 0 does not need any special handling and zero_rounding_mode is ignored
|
2799
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_CEILING, ZERO_ROUND_UNNECESSARY)
|
1570
2800
|
assert(zero_r >= 0, "0_r=#{zero_r} " + text)
|
1571
2801
|
|
1572
|
-
|
2802
|
+
# ceiling always rounds toward negative infinity, so 0 does not need any special handling and zero_rounding_mode is ignored
|
2803
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_FLOOR, ZERO_ROUND_UNNECESSARY)
|
1573
2804
|
assert(zero_r <= 0, "0_r=#{zero_r} " + text)
|
1574
2805
|
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1609
|
-
if (zero_r < 0)
|
1610
|
-
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1611
|
-
end
|
1612
|
-
zero_r = check_round_to_one_remainder(0, r, modulus, LongDecimalRoundingMode::ROUND_HALF_DOWN, LongDecimalRoundingMode::ZERO_ROUND_TO_MINUS)
|
1613
|
-
dd = 2*zero_r.abs
|
1614
|
-
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1615
|
-
if (zero_r > 0)
|
1616
|
-
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1617
|
-
end
|
1618
|
-
zero_r = check_round_to_one_remainder(0, r, modulus, LongDecimalRoundingMode::ROUND_HALF_DOWN, LongDecimalRoundingMode::ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
1619
|
-
dd = 2*zero_r.abs
|
1620
|
-
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1621
|
-
if (zero_r > 0)
|
1622
|
-
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
2806
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
2807
|
+
unless (rounding_mode.minor == MINOR_UP || rounding_mode.minor == MINOR_DOWN)
|
2808
|
+
next
|
2809
|
+
end
|
2810
|
+
unless (rounding_mode.major == MAJOR_HALF \
|
2811
|
+
|| rounding_mode.major == MAJOR_HARMONIC \
|
2812
|
+
|| rounding_mode.major == MAJOR_GEOMETRIC \
|
2813
|
+
|| rounding_mode.major == MAJOR_QUADRATIC \
|
2814
|
+
|| rounding_mode.major == MAJOR_CUBIC)
|
2815
|
+
next
|
2816
|
+
end
|
2817
|
+
[ZERO_ROUND_TO_PLUS,ZERO_ROUND_TO_CLOSEST_PREFER_PLUS].each do |zero_rounding_mode|
|
2818
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, rounding_mode, zero_rounding_mode)
|
2819
|
+
if (rounding_mode.major == MAJOR_HALF)
|
2820
|
+
dd = 2*zero_r.abs
|
2821
|
+
text2 = text + " dd=#{dd} rm=#{rounding_mode} zm=#{zero_rounding_mode}"
|
2822
|
+
assert(dd <= modulus, "0_r=#{zero_r} " + text2)
|
2823
|
+
if (zero_r < 0)
|
2824
|
+
assert(dd < modulus, "0_r=#{zero_r} " + text2)
|
2825
|
+
end
|
2826
|
+
end
|
2827
|
+
end
|
2828
|
+
[ZERO_ROUND_TO_MINUS,ZERO_ROUND_TO_CLOSEST_PREFER_MINUS].each do |zero_rounding_mode|
|
2829
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, rounding_mode, ZERO_ROUND_TO_MINUS)
|
2830
|
+
if (rounding_mode.major == MAJOR_HALF)
|
2831
|
+
dd = 2*zero_r.abs
|
2832
|
+
text2 = text + " dd=#{dd} rm=#{rounding_mode}"
|
2833
|
+
assert(dd <= modulus, "0_r=#{zero_r} " + text2)
|
2834
|
+
if (zero_r > 0)
|
2835
|
+
assert(dd < modulus, "0_r=#{zero_r} " + text2)
|
2836
|
+
end
|
2837
|
+
end
|
2838
|
+
end
|
1623
2839
|
end
|
1624
2840
|
|
1625
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2841
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_HALF_CEILING, ZERO_ROUND_UNNECESSARY)
|
1626
2842
|
dd = 2*zero_r.abs
|
1627
2843
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1628
2844
|
if (zero_r < 0)
|
1629
2845
|
assert(dd < modulus, "0_r=#{zero_r} " + text)
|
1630
2846
|
end
|
1631
2847
|
|
1632
|
-
zero_r = check_round_to_one_remainder(0, r, modulus,
|
2848
|
+
zero_r = check_round_to_one_remainder(0, r, modulus, ROUND_HALF_FLOOR, ZERO_ROUND_UNNECESSARY)
|
1633
2849
|
dd = 2*zero_r.abs
|
1634
2850
|
assert(dd <= modulus, "0_r=#{zero_r} " + text)
|
1635
2851
|
if (zero_r > 0)
|
@@ -1641,19 +2857,20 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1641
2857
|
|
1642
2858
|
def check_round_to_one_remainder(i, r, modulus, rounding_mode, zero_rounding_mode)
|
1643
2859
|
|
1644
|
-
zero_modes = [ LongDecimalRoundingMode::ZERO_ROUND_TO_PLUS,\
|
1645
|
-
LongDecimalRoundingMode::ZERO_ROUND_TO_MINUS,\
|
1646
|
-
LongDecimalRoundingMode::ZERO_ROUND_TO_CLOSEST_PREFER_PLUS,\
|
1647
|
-
LongDecimalRoundingMode::ZERO_ROUND_TO_CLOSEST_PREFER_MINUS,\
|
1648
|
-
LongDecimalRoundingMode::ZERO_ROUND_UNNECESSARY ];
|
1649
2860
|
remainders = [ r ]
|
1650
2861
|
i_rounded = i.round_to_allowed_remainders(remainders, modulus, rounding_mode, zero_rounding_mode)
|
1651
|
-
|
1652
|
-
|
2862
|
+
text = "i_r=#{i_rounded} i=#{i} m=#{modulus} r=#{r} mode=#{rounding_mode} zm=#{zero_rounding_mode}"
|
2863
|
+
|
2864
|
+
# if i is not zero or zero_rounding_mode is ZERO_ROUND_UNNECESSARY, using another zero_rounding_mode should not influence the result:
|
2865
|
+
if (i != 0 || zero_rounding_mode == ZERO_ROUND_UNNECESSARY)
|
2866
|
+
ALL_ZERO_MODES.each do |zm|
|
1653
2867
|
assert_equal(i_rounded, i.round_to_allowed_remainders(remainders, modulus, rounding_mode, zm), "i=#{i} i_r=#{i_rounded} m=#{modulus} zm=#{zm} r=#{r}")
|
1654
2868
|
end
|
1655
2869
|
end
|
1656
|
-
|
2870
|
+
|
2871
|
+
# make sure that the result is congruent r modulo the modulus and within less than modulus away from i:
|
2872
|
+
assert(! (i_rounded.nil?), text)
|
2873
|
+
assert_equal(0, (i_rounded - r) % modulus, text)
|
1657
2874
|
assert(i - modulus < i_rounded)
|
1658
2875
|
assert(i_rounded < i + modulus)
|
1659
2876
|
i_rounded
|
@@ -1663,94 +2880,148 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1663
2880
|
# test rounding of int to remainder set
|
1664
2881
|
#
|
1665
2882
|
def test_int_round_to_allowed_remainders
|
1666
|
-
print "\ntest_int_round_to_allowed_remainders [#{Time.now}] (
|
2883
|
+
print "\ntest_int_round_to_allowed_remainders [#{Time.now}] (15 sec): "
|
1667
2884
|
$stdout.flush
|
1668
2885
|
# 2.upto 8 do |modulus|
|
1669
2886
|
2.upto 7 do |modulus|
|
1670
2887
|
xx = (1<< modulus) - 1
|
1671
2888
|
xx.times do |x|
|
1672
2889
|
remainders = make_set(x + 1, modulus)
|
2890
|
+
min_remainder = remainders.min
|
2891
|
+
max_remainder = remainders.max
|
2892
|
+
max_neg_remainder = max_remainder - modulus
|
2893
|
+
closest_remainder_prefer_plus = nil
|
2894
|
+
closest_remainder_prefer_minus = nil
|
2895
|
+
minus_remainder = if (min_remainder == 0)
|
2896
|
+
min_remainder
|
2897
|
+
else
|
2898
|
+
max_neg_remainder
|
2899
|
+
end
|
2900
|
+
if (-max_neg_remainder > min_remainder)
|
2901
|
+
closest_remainder_prefer_minus = min_remainder
|
2902
|
+
closest_remainder_prefer_plus = min_remainder
|
2903
|
+
elsif (-max_neg_remainder < min_remainder)
|
2904
|
+
closest_remainder_prefer_minus = max_neg_remainder
|
2905
|
+
closest_remainder_prefer_plus = max_neg_remainder
|
2906
|
+
else
|
2907
|
+
closest_remainder_prefer_minus = max_neg_remainder
|
2908
|
+
closest_remainder_prefer_plus = min_remainder
|
2909
|
+
end
|
2910
|
+
|
1673
2911
|
text0 = "m=#{modulus} x=#{x} s=#{remainders.inspect}"
|
1674
|
-
|
1675
|
-
print ":"
|
2912
|
+
print "."
|
1676
2913
|
$stdout.flush
|
1677
2914
|
n = 3*modulus
|
1678
2915
|
(-n).upto n do |i|
|
1679
2916
|
text = "i=#{i} n=#{n} " + text0
|
1680
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2917
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_UP, ZERO_ROUND_TO_PLUS)
|
2918
|
+
assert(above.empty?)
|
2919
|
+
assert(below.empty?)
|
1681
2920
|
assert(i_rounded.abs >= i.abs, "i_r=#{i_rounded} " + text)
|
1682
2921
|
if (i == 0) then
|
1683
2922
|
assert(i_rounded >= 0, "i_r=#{i_rounded} " + text)
|
1684
|
-
assert_equal(
|
2923
|
+
assert_equal(min_remainder, i_rounded)
|
2924
|
+
# assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1685
2925
|
elsif (i > 0) then
|
1686
2926
|
# rounded away from 0, so for positive i to value >= i
|
1687
|
-
assert_equal(above.length, 0, text)
|
2927
|
+
# assert_equal(above.length, 0, text)
|
1688
2928
|
assert(i_rounded >= i, "i_r=#{i_rounded} " + text)
|
2929
|
+
assert(i_rounded >= min_remainder)
|
1689
2930
|
else
|
1690
2931
|
# i < 0
|
1691
|
-
# rounded away from 0, so for
|
2932
|
+
# rounded away from 0, so for negative i to value <= i
|
1692
2933
|
assert_equal(below.length, 0, text)
|
1693
2934
|
assert(i_rounded <= i, "i_r=#{i_rounded} " + text)
|
2935
|
+
assert(i_rounded <= max_neg_remainder)
|
1694
2936
|
end
|
1695
2937
|
|
1696
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2938
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_DOWN, ZERO_ROUND_TO_PLUS)
|
2939
|
+
assert(above.empty?)
|
2940
|
+
assert(below.empty?)
|
1697
2941
|
if (i > 0)
|
1698
2942
|
assert(i_rounded <= i, "i_r=#{i_rounded} " + text)
|
1699
|
-
|
2943
|
+
assert(i_rounded >= max_neg_remainder, "i_r=#{i_rounded} " + text)
|
2944
|
+
# assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1700
2945
|
elsif (i < 0)
|
1701
2946
|
assert(i_rounded >= i, "i_r=#{i_rounded} " + text)
|
1702
|
-
|
2947
|
+
assert(i_rounded <= min_remainder, "i_r=#{i_rounded} " + text)
|
2948
|
+
# assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1703
2949
|
elsif (i == 0) then
|
1704
2950
|
assert(i_rounded >= 0, "i_r=#{i_rounded} " + text)
|
1705
|
-
assert_equal(
|
2951
|
+
assert_equal(min_remainder, i_rounded, "i_r=#{i_rounded} " + text)
|
2952
|
+
# assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1706
2953
|
else
|
1707
2954
|
raise("i=#{i} i_r=#{i_rounded}")
|
1708
2955
|
end
|
1709
2956
|
|
1710
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2957
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_CEILING, ZERO_ROUND_TO_PLUS)
|
2958
|
+
assert(above.empty?)
|
2959
|
+
assert(below.empty?)
|
1711
2960
|
assert(i_rounded >= i, "i_r=#{i_rounded} " + text)
|
1712
|
-
assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
2961
|
+
# assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1713
2962
|
|
1714
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2963
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_FLOOR, ZERO_ROUND_TO_PLUS)
|
2964
|
+
assert(above.empty?)
|
2965
|
+
assert(below.empty?)
|
1715
2966
|
assert(i_rounded <= i, "i_r=#{i_rounded} " + text)
|
1716
|
-
assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
2967
|
+
# assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1717
2968
|
|
1718
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2969
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_PLUS)
|
2970
|
+
assert(above.empty?)
|
2971
|
+
assert(below.empty?)
|
1719
2972
|
dd = 2*(i_rounded - i).abs
|
1720
2973
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1721
2974
|
if (i_rounded.abs < i.abs || i_rounded.sgn == - i.sgn)
|
1722
2975
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1723
2976
|
end
|
1724
|
-
assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1725
|
-
assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1726
2977
|
|
1727
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2978
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_PLUS)
|
2979
|
+
assert(above.empty?)
|
2980
|
+
assert(below.empty?)
|
1728
2981
|
dd = 2*(i_rounded - i).abs
|
1729
2982
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1730
2983
|
if (i_rounded.abs > i.abs && i_rounded.sgn == i.sgn)
|
1731
2984
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1732
2985
|
end
|
1733
|
-
assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1734
|
-
assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1735
2986
|
|
1736
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2987
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_HALF_CEILING, ZERO_ROUND_TO_PLUS)
|
2988
|
+
assert(above.empty?)
|
2989
|
+
assert(below.empty?)
|
1737
2990
|
dd = 2*(i_rounded - i).abs
|
1738
2991
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1739
2992
|
if (i_rounded < i)
|
1740
2993
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1741
2994
|
end
|
1742
|
-
assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1743
|
-
assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1744
2995
|
|
1745
|
-
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus,
|
2996
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, ROUND_HALF_FLOOR, ZERO_ROUND_TO_PLUS)
|
2997
|
+
assert(above.empty?)
|
2998
|
+
assert(below.empty?)
|
1746
2999
|
dd = 2*(i_rounded - i).abs
|
1747
3000
|
assert(dd <= modulus, "i_r=#{i_rounded} " + text)
|
1748
3001
|
if (i_rounded > i && i != 0)
|
1749
3002
|
assert(dd < modulus, "i_r=#{i_rounded} " + text)
|
1750
3003
|
end
|
1751
|
-
assert_equal(below.length, 0, "i_r=#{i_rounded} " + text)
|
1752
|
-
assert_equal(above.length, 0, "i_r=#{i_rounded} " + text)
|
1753
3004
|
|
3005
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
3006
|
+
unless (rounding_mode.major == MAJOR_GEOMETRIC \
|
3007
|
+
|| rounding_mode.major == MAJOR_HARMONIC \
|
3008
|
+
|| rounding_mode.major == MAJOR_QUADRATIC \
|
3009
|
+
|| rounding_mode.major == MAJOR_CUBIC)
|
3010
|
+
next;
|
3011
|
+
end
|
3012
|
+
if (rounding_mode.minor == MINOR_EVEN || rounding_mode.minor == MINOR_ODD)
|
3013
|
+
next
|
3014
|
+
end
|
3015
|
+
i_rounded, set, above, below = check_round_to_remainders(i, remainders, modulus, rounding_mode, ZERO_ROUND_TO_PLUS)
|
3016
|
+
if (rounding_mode.major != MAJOR_CUBIC)
|
3017
|
+
if (i < 0)
|
3018
|
+
assert(i_rounded <= 0, "i_r=#{i_rounded} " + text)
|
3019
|
+
end
|
3020
|
+
if (i > 0)
|
3021
|
+
assert(i_rounded >= 0, "i_r=#{i_rounded} " + text)
|
3022
|
+
end
|
3023
|
+
end
|
3024
|
+
end
|
1754
3025
|
end
|
1755
3026
|
end
|
1756
3027
|
end
|
@@ -1760,134 +3031,271 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1760
3031
|
# test rounding of 0 to remainder set
|
1761
3032
|
#
|
1762
3033
|
def test_zero_round_to_allowed_remainders
|
1763
|
-
print "\ntest_zero_round_to_allowed_remainders [#{Time.now}]
|
3034
|
+
print "\ntest_zero_round_to_allowed_remainders [#{Time.now}]: "
|
1764
3035
|
$stdout.flush
|
1765
3036
|
|
1766
3037
|
2.upto 7 do |modulus|
|
1767
3038
|
xx = (1<< modulus) - 1
|
1768
3039
|
xx.times do |x|
|
1769
3040
|
remainders = make_set(x + 1, modulus)
|
3041
|
+
|
3042
|
+
min_remainder = remainders.min
|
3043
|
+
max_remainder = remainders.max
|
3044
|
+
max_neg_remainder = max_remainder - modulus
|
3045
|
+
closest_remainder_prefer_plus = nil
|
3046
|
+
closest_remainder_prefer_minus = nil
|
3047
|
+
minus_remainder = if (min_remainder == 0)
|
3048
|
+
min_remainder
|
3049
|
+
else
|
3050
|
+
max_neg_remainder
|
3051
|
+
end
|
3052
|
+
if (-max_neg_remainder > min_remainder)
|
3053
|
+
closest_remainder_prefer_minus = min_remainder
|
3054
|
+
closest_remainder_prefer_plus = min_remainder
|
3055
|
+
elsif (-max_neg_remainder < min_remainder)
|
3056
|
+
closest_remainder_prefer_minus = max_neg_remainder
|
3057
|
+
closest_remainder_prefer_plus = max_neg_remainder
|
3058
|
+
else
|
3059
|
+
closest_remainder_prefer_minus = max_neg_remainder
|
3060
|
+
closest_remainder_prefer_plus = min_remainder
|
3061
|
+
end
|
3062
|
+
|
1770
3063
|
text = "m=#{modulus} x=#{x} s=#{remainders.inspect}"
|
1771
|
-
# puts text
|
1772
3064
|
print "."
|
1773
3065
|
$stdout.flush
|
1774
3066
|
|
1775
3067
|
# ROUND_UP and ROUND_DOWN have the same effect for 0
|
1776
|
-
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus,
|
1777
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3068
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_UP, ZERO_ROUND_TO_PLUS)
|
3069
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_DOWN, ZERO_ROUND_TO_PLUS)
|
3070
|
+
assert(above1.empty?)
|
3071
|
+
assert(below1.empty?)
|
3072
|
+
assert(above2.empty?)
|
3073
|
+
assert(below2.empty?)
|
1778
3074
|
assert_equal(zero_r1, zero_r2, text)
|
1779
|
-
assert_equal(above1, above2, text)
|
1780
|
-
assert_equal(below1, below2, text)
|
1781
3075
|
assert(zero_r1 >= 0, "0_r=#{zero_r1} " + text)
|
1782
|
-
assert_equal(
|
1783
|
-
|
1784
|
-
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus,
|
1785
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3076
|
+
assert_equal(min_remainder, zero_r1, "0_r=#{zero_r1} " + text)
|
3077
|
+
|
3078
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_UP, ZERO_ROUND_TO_MINUS)
|
3079
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_DOWN, ZERO_ROUND_TO_MINUS)
|
3080
|
+
assert(above1.empty?)
|
3081
|
+
assert(below1.empty?)
|
3082
|
+
assert(above2.empty?)
|
3083
|
+
assert(below2.empty?)
|
1786
3084
|
assert_equal(zero_r1, zero_r2, text)
|
1787
|
-
assert_equal(above1, above2, text)
|
1788
|
-
assert_equal(below1, below2, text)
|
1789
3085
|
assert(zero_r1 <= 0, "0_r=#{zero_r1} " + text)
|
1790
|
-
assert_equal(
|
1791
|
-
|
1792
|
-
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus,
|
1793
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3086
|
+
assert_equal(minus_remainder, zero_r1, "0_r=#{zero_r1} " + text)
|
3087
|
+
|
3088
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_UP, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3089
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3090
|
+
assert(above1.empty?)
|
3091
|
+
assert(below1.empty?)
|
3092
|
+
assert(above2.empty?)
|
3093
|
+
assert(below2.empty?)
|
1794
3094
|
assert_equal(zero_r1, zero_r2, text)
|
1795
|
-
assert_equal(
|
1796
|
-
assert_equal(below1, below2, text)
|
3095
|
+
assert_equal(closest_remainder_prefer_plus, zero_r1, text)
|
1797
3096
|
dd = 2*zero_r1.abs
|
1798
3097
|
assert(dd <= modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1799
3098
|
if (zero_r1 < 0)
|
1800
3099
|
assert(dd < modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1801
3100
|
end
|
1802
|
-
if (below1.length > 0)
|
1803
|
-
|
1804
|
-
end
|
1805
|
-
if (above1.length > 0)
|
1806
|
-
|
1807
|
-
end
|
1808
|
-
|
1809
|
-
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus,
|
1810
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3101
|
+
#if (below1.length > 0)
|
3102
|
+
# assert(below1.max.abs >= zero_r1.abs, text)
|
3103
|
+
#end
|
3104
|
+
#if (above1.length > 0)
|
3105
|
+
# assert(above1.min.abs >= zero_r1.abs, text)
|
3106
|
+
#end
|
3107
|
+
|
3108
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_UP, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
3109
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
3110
|
+
assert(above1.empty?)
|
3111
|
+
assert(below1.empty?)
|
3112
|
+
assert(above2.empty?)
|
3113
|
+
assert(below2.empty?)
|
1811
3114
|
assert_equal(zero_r1, zero_r2, text)
|
1812
|
-
assert_equal(
|
1813
|
-
assert_equal(
|
3115
|
+
assert_equal(closest_remainder_prefer_minus, zero_r1, text)
|
3116
|
+
# assert_equal(above1, above2, text)
|
3117
|
+
# assert_equal(below1, below2, text)
|
1814
3118
|
dd = 2*zero_r1.abs
|
1815
3119
|
assert(dd <= modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1816
3120
|
if (zero_r1 > 0)
|
1817
3121
|
assert(dd < modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1818
3122
|
end
|
1819
|
-
if (below1.length > 0)
|
1820
|
-
|
1821
|
-
end
|
1822
|
-
assert_equal(above1.length, 0, text)
|
1823
|
-
|
1824
|
-
|
1825
|
-
assert(
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
3123
|
+
#if (below1.length > 0)
|
3124
|
+
# assert(below1.max.abs >= zero_r1.abs, text)
|
3125
|
+
#end
|
3126
|
+
#assert_equal(above1.length, 0, text)
|
3127
|
+
|
3128
|
+
zero_rounded, set0, above0, below0 = check_round_to_remainders(0, remainders, modulus, ROUND_CEILING, ZERO_ROUND_UNNECESSARY)
|
3129
|
+
assert(above0.empty?)
|
3130
|
+
assert(below0.empty?)
|
3131
|
+
assert(zero_rounded >= 0, "0_r=#{zero_rounded} " + text)
|
3132
|
+
assert_equal(min_remainder, zero_rounded, text)
|
3133
|
+
# assert_equal(above0.length, 0, text)
|
3134
|
+
|
3135
|
+
zero_rounded, set0, above0, below0 = check_round_to_remainders(0, remainders, modulus, ROUND_FLOOR, ZERO_ROUND_UNNECESSARY)
|
3136
|
+
assert(above0.empty?)
|
3137
|
+
assert(below0.empty?)
|
3138
|
+
assert(zero_rounded <= 0, "0_r=#{zero_rounded} " + text)
|
3139
|
+
assert_equal(minus_remainder, zero_rounded, text)
|
3140
|
+
# assert_equal(below0.length, 0, text)
|
3141
|
+
|
3142
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_PLUS)
|
3143
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_PLUS)
|
3144
|
+
assert(above1.empty?)
|
3145
|
+
assert(below1.empty?)
|
3146
|
+
assert(above2.empty?)
|
3147
|
+
assert(below2.empty?)
|
1834
3148
|
assert_equal(zero_r1, zero_r2, text)
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
3149
|
+
if (zero_r1 < 0)
|
3150
|
+
assert_equal(max_neg_remainder, zero_r1)
|
3151
|
+
else
|
3152
|
+
assert_equal(min_remainder, zero_r1)
|
3153
|
+
end
|
3154
|
+
# assert_equal(above1, above2, text)
|
3155
|
+
# assert_equal(below1, below2, text)
|
3156
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3157
|
+
assert(above1.empty?)
|
3158
|
+
assert(below1.empty?)
|
3159
|
+
assert(above2.empty?)
|
3160
|
+
assert(below2.empty?)
|
1838
3161
|
assert_equal(zero_r1, zero_r2, text)
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
3162
|
+
if (zero_r1 < 0)
|
3163
|
+
assert_equal(max_neg_remainder, zero_r1)
|
3164
|
+
else
|
3165
|
+
assert_equal(min_remainder, zero_r1)
|
3166
|
+
end
|
3167
|
+
#assert_equal(above1, above2, text)
|
3168
|
+
#assert_equal(below1, below2, text)
|
3169
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3170
|
+
assert(above1.empty?)
|
3171
|
+
assert(below1.empty?)
|
3172
|
+
assert(above2.empty?)
|
3173
|
+
assert(below2.empty?)
|
1842
3174
|
assert_equal(zero_r1, zero_r2, text)
|
1843
|
-
assert_equal(above1, above2, text)
|
1844
|
-
assert_equal(below1, below2, text)
|
3175
|
+
# assert_equal(above1, above2, text)
|
3176
|
+
# assert_equal(below1, below2, text)
|
1845
3177
|
dd = 2*zero_r1.abs
|
1846
3178
|
assert(dd <= modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1847
3179
|
if (zero_r1 < 0)
|
1848
3180
|
assert(dd < modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
3181
|
+
assert_equal(max_neg_remainder, zero_r1)
|
3182
|
+
else
|
3183
|
+
assert_equal(min_remainder, zero_r1)
|
1849
3184
|
end
|
1850
|
-
assert_equal(below1.length, 0, text)
|
1851
|
-
assert_equal(above1.length, 0, text)
|
1852
|
-
|
1853
|
-
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus,
|
1854
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3185
|
+
# assert_equal(below1.length, 0, text)
|
3186
|
+
# assert_equal(above1.length, 0, text)
|
3187
|
+
|
3188
|
+
zero_r1, set1, above1, below1 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_MINUS)
|
3189
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_MINUS)
|
3190
|
+
assert(above1.empty?)
|
3191
|
+
assert(below1.empty?)
|
3192
|
+
assert(above2.empty?)
|
3193
|
+
assert(below2.empty?)
|
1855
3194
|
assert_equal(zero_r1, zero_r2, text)
|
1856
|
-
|
1857
|
-
|
1858
|
-
|
3195
|
+
if (zero_r1 < 0)
|
3196
|
+
assert_equal(max_neg_remainder, zero_r1)
|
3197
|
+
else
|
3198
|
+
assert_equal(min_remainder, zero_r1)
|
3199
|
+
end
|
3200
|
+
# assert_equal(above1, above2, text)
|
3201
|
+
# assert_equal(below1, below2, text)
|
3202
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_UP, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
3203
|
+
assert(above1.empty?)
|
3204
|
+
assert(below1.empty?)
|
3205
|
+
assert(above2.empty?)
|
3206
|
+
assert(below2.empty?)
|
1859
3207
|
assert_equal(zero_r1, zero_r2, text)
|
1860
|
-
assert_equal(above1, above2, text)
|
1861
|
-
assert_equal(below1, below2, text)
|
1862
|
-
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus,
|
3208
|
+
# assert_equal(above1, above2, text)
|
3209
|
+
# assert_equal(below1, below2, text)
|
3210
|
+
zero_r2, set2, above2, below2 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_DOWN, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
3211
|
+
assert(above1.empty?)
|
3212
|
+
assert(below1.empty?)
|
3213
|
+
assert(above2.empty?)
|
3214
|
+
assert(below2.empty?)
|
1863
3215
|
assert_equal(zero_r1, zero_r2, text)
|
1864
|
-
assert_equal(above1, above2, text)
|
1865
|
-
assert_equal(below1, below2, text)
|
3216
|
+
# assert_equal(above1, above2, text)
|
3217
|
+
# assert_equal(below1, below2, text)
|
1866
3218
|
dd = 2*zero_r1.abs
|
1867
3219
|
assert(dd <= modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
1868
3220
|
if (zero_r1 > 0)
|
1869
3221
|
assert(dd < modulus, "0_r=#{zero_r1} dd=#{dd} " + text)
|
3222
|
+
assert_equal(min_remainder, zero_r1)
|
3223
|
+
else
|
3224
|
+
assert_equal(minus_remainder, zero_r1)
|
1870
3225
|
end
|
1871
|
-
assert_equal(below1.length, 0, text)
|
1872
|
-
assert_equal(above1.length, 0, text)
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
assert(
|
1877
|
-
|
1878
|
-
|
3226
|
+
# assert_equal(below1.length, 0, text)
|
3227
|
+
# assert_equal(above1.length, 0, text)
|
3228
|
+
|
3229
|
+
zero_rounded, set0, above0, below0 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_CEILING, ZERO_ROUND_UNNECESSARY)
|
3230
|
+
assert(above0.empty?)
|
3231
|
+
assert(below0.empty?)
|
3232
|
+
dd = 2*zero_rounded.abs
|
3233
|
+
assert(dd <= modulus, "0_r=#{zero_rounded} dd=#{dd} " + text)
|
3234
|
+
if (zero_rounded < 0)
|
3235
|
+
assert_equal(max_neg_remainder, zero_rounded)
|
3236
|
+
assert(dd < modulus, "0_r=#{zero_rounded} dd=#{dd} " + text)
|
3237
|
+
else
|
3238
|
+
assert_equal(min_remainder, zero_rounded)
|
1879
3239
|
end
|
1880
|
-
assert_equal(below0.length, 0, text)
|
1881
|
-
assert_equal(above0.length, 0, text)
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
assert(
|
1886
|
-
|
1887
|
-
|
3240
|
+
# assert_equal(below0.length, 0, text)
|
3241
|
+
# assert_equal(above0.length, 0, text)
|
3242
|
+
|
3243
|
+
zero_rounded, set0, above0, below0 = check_round_to_remainders(0, remainders, modulus, ROUND_HALF_FLOOR, ZERO_ROUND_UNNECESSARY)
|
3244
|
+
assert(above0.empty?)
|
3245
|
+
assert(below0.empty?)
|
3246
|
+
dd = 2*zero_rounded.abs
|
3247
|
+
assert(dd <= modulus, "0_r=#{zero_rounded} " + text)
|
3248
|
+
if (zero_rounded > 0)
|
3249
|
+
assert_equal(min_remainder, zero_r1)
|
3250
|
+
assert(dd < modulus, "0_r=#{zero_rounded} " + text)
|
3251
|
+
else
|
3252
|
+
assert_equal(minus_remainder, zero_r1)
|
3253
|
+
end
|
3254
|
+
# assert_equal(below0.length, 0, text)
|
3255
|
+
# assert_equal(above0.length, 0, text)
|
3256
|
+
|
3257
|
+
ALL_ROUNDING_MODES.each do |rounding_mode|
|
3258
|
+
unless (rounding_mode.major == MAJOR_GEOMETRIC \
|
3259
|
+
|| rounding_mode.major == MAJOR_HARMONIC \
|
3260
|
+
|| rounding_mode.major == MAJOR_QUADRATIC \
|
3261
|
+
|| rounding_mode.major == MAJOR_CUBIC)
|
3262
|
+
next;
|
3263
|
+
end
|
3264
|
+
if (rounding_mode.minor == MINOR_EVEN || rounding_mode.minor == MINOR_ODD)
|
3265
|
+
next
|
3266
|
+
end
|
3267
|
+
|
3268
|
+
if (rounding_mode == ROUND_CUBIC_CEILING)
|
3269
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_UNNECESSARY)
|
3270
|
+
if (min_remainder+max_neg_remainder > 0)
|
3271
|
+
assert_equal(minus_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3272
|
+
else
|
3273
|
+
assert_equal(min_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3274
|
+
end
|
3275
|
+
elsif (rounding_mode == ROUND_CUBIC_FLOOR)
|
3276
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_UNNECESSARY)
|
3277
|
+
if (min_remainder+max_neg_remainder < 0)
|
3278
|
+
assert_equal(min_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3279
|
+
else
|
3280
|
+
assert_equal(minus_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3281
|
+
end
|
3282
|
+
elsif ((rounding_mode == ROUND_CUBIC_DOWN || rounding_mode == ROUND_CUBIC_UP) && min_remainder+max_neg_remainder != 0)
|
3283
|
+
assert_equal(closest_remainder_prefer_minus, closest_remainder_prefer_plus)
|
3284
|
+
i_rounded = 0.round_to_allowed_remainders(remainders, modulus, rounding_mode, ZERO_ROUND_UNNECESSARY)
|
3285
|
+
assert_equal(closest_remainder_prefer_plus, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3286
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_UNNECESSARY)
|
3287
|
+
assert_equal(closest_remainder_prefer_plus, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3288
|
+
else
|
3289
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_TO_PLUS)
|
3290
|
+
assert_equal(min_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3291
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_TO_MINUS)
|
3292
|
+
assert_equal(minus_remainder, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3293
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3294
|
+
assert_equal(closest_remainder_prefer_plus, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3295
|
+
i_rounded, set, above, below = check_round_to_remainders(0, remainders, modulus, rounding_mode, ZERO_ROUND_TO_CLOSEST_PREFER_MINUS)
|
3296
|
+
assert_equal(closest_remainder_prefer_minus, i_rounded, text+" i_rounded=#{i_rounded} rounding_mode=#{rounding_mode}")
|
3297
|
+
end
|
1888
3298
|
end
|
1889
|
-
assert_equal(below0.length, 0, text)
|
1890
|
-
assert_equal(above0.length, 0, text)
|
1891
3299
|
|
1892
3300
|
end
|
1893
3301
|
end
|
@@ -1907,27 +3315,36 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1907
3315
|
|
1908
3316
|
def check_round_to_remainders(i, remainders, modulus, rounding_mode, zero_rounding_mode)
|
1909
3317
|
|
1910
|
-
|
1911
|
-
|
1912
|
-
LongDecimalRoundingMode::ZERO_ROUND_TO_CLOSEST_PREFER_PLUS,\
|
1913
|
-
LongDecimalRoundingMode::ZERO_ROUND_TO_CLOSEST_PREFER_MINUS,\
|
1914
|
-
LongDecimalRoundingMode::ZERO_ROUND_UNNECESSARY ];
|
3318
|
+
# do the rounding
|
3319
|
+
i_rounded = i.round_to_allowed_remainders(remainders.clone, modulus, rounding_mode, zero_rounding_mode)
|
1915
3320
|
|
1916
|
-
|
1917
|
-
if (i != 0 || zero_rounding_mode ==
|
1918
|
-
|
1919
|
-
assert_equal(i_rounded, i.round_to_allowed_remainders(remainders, modulus, rounding_mode, zm), "i=#{i} i_r=#{i_rounded} m=#{modulus} zm=#{zm}")
|
3321
|
+
# make sure that the zero_rounding_mode does not matter if i is not zero or if ZERO_ROUND_UNNECESSARY is provided
|
3322
|
+
if (i != 0 || zero_rounding_mode == ZERO_ROUND_UNNECESSARY)
|
3323
|
+
ALL_ZERO_MODES.each do |zm|
|
3324
|
+
assert_equal(i_rounded, i.round_to_allowed_remainders(remainders.clone, modulus, rounding_mode, zm), "i=#{i} i_r=#{i_rounded} m=#{modulus} zm=#{zm}")
|
1920
3325
|
end
|
1921
3326
|
end
|
3327
|
+
|
3328
|
+
# make sure there is exactly one remainder matching
|
1922
3329
|
one_remainder = remainders.select do |r|
|
1923
3330
|
(i_rounded - r) % modulus == 0
|
1924
3331
|
end
|
1925
|
-
assert_equal(1, one_remainder.length, "i_r=#{i_rounded} i=#{i} m=#{modulus} r=#{remainders} or=#{one_remainder.to_s}")
|
3332
|
+
assert_equal(1, one_remainder.length, "i_r=#{i_rounded} i=#{i} m=#{modulus} r=#{remainders} or=#{one_remainder.to_s} rm=#{rounding_mode} zm=#{zero_rounding_mode}")
|
3333
|
+
|
3334
|
+
# make sure that i_rounded is less than modulus away from i
|
1926
3335
|
assert(i - modulus < i_rounded)
|
1927
3336
|
assert(i_rounded < i + modulus)
|
3337
|
+
|
3338
|
+
# run the test with each single remainder:
|
1928
3339
|
set = remainders.map do |r|
|
1929
|
-
|
3340
|
+
if (zero_rounding_mode == ZERO_ROUND_UNNECESSARY)
|
3341
|
+
check_round_to_one_remainder(i, r, modulus, rounding_mode, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3342
|
+
else
|
3343
|
+
check_round_to_one_remainder(i, r, modulus, rounding_mode, zero_rounding_mode)
|
3344
|
+
end
|
1930
3345
|
end
|
3346
|
+
|
3347
|
+
# find members which are closer than i_rounded above and below from the results of rounding to a single remainder
|
1931
3348
|
closer_above = []
|
1932
3349
|
closer_below = []
|
1933
3350
|
found = false
|
@@ -1941,16 +3358,20 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1941
3358
|
assert(i_rounded < i_r)
|
1942
3359
|
if closer
|
1943
3360
|
closer_above.push(i_r)
|
3361
|
+
# raise ArgumentError, "i=#{i} ir=#{i_r} i_rounded=#{i_rounded} remainders=#{remainders.inspect} m=#{modulus} rm=#{rounding_mode} zm=#{zero_rounding_mode} closer_above"
|
1944
3362
|
end
|
1945
3363
|
else
|
1946
3364
|
# i_r < i
|
1947
3365
|
assert(i_r < i_rounded)
|
1948
3366
|
if closer
|
1949
3367
|
closer_below.push(i_r)
|
3368
|
+
# raise ArgumentError, "i=#{i} ir=#{i_r} i_rounded=#{i_rounded} remainders=#{remainders.inspect} m=#{modulus} rm=#{rounding_mode} zm=#{zero_rounding_mode} closer_below"
|
1950
3369
|
end
|
1951
3370
|
end
|
1952
3371
|
end
|
1953
3372
|
end
|
3373
|
+
# assert(closer_below.empty?)
|
3374
|
+
# assert(closer_above.empty?)
|
1954
3375
|
return i_rounded, set, closer_above, closer_below
|
1955
3376
|
|
1956
3377
|
end # check_round_to_remainders
|
@@ -1959,6 +3380,228 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1959
3380
|
#
|
1960
3381
|
# ROUND_UNNECESSARY/ROUND_HALF_EVEN
|
1961
3382
|
|
3383
|
+
def test_minor_major_rounding_modes
|
3384
|
+
print "\ntest_minor_major_rounding_modes [#{Time.now}]: "
|
3385
|
+
$stdout.flush
|
3386
|
+
[[ ROUND_CEILING, MAJOR_CEILING, MINOR_UNUSED ],
|
3387
|
+
[ ROUND_UNNECESSARY, MAJOR_UNNECESSARY, MINOR_UNUSED ],
|
3388
|
+
[ ROUND_UP, MAJOR_UP, MINOR_UNUSED ],
|
3389
|
+
[ ROUND_DOWN, MAJOR_DOWN, MINOR_UNUSED ],
|
3390
|
+
[ ROUND_FLOOR, MAJOR_FLOOR, MINOR_UNUSED ],
|
3391
|
+
[ ROUND_CUBIC_CEILING, MAJOR_CUBIC, MINOR_CEILING ],
|
3392
|
+
[ ROUND_CUBIC_DOWN, MAJOR_CUBIC, MINOR_DOWN ],
|
3393
|
+
[ ROUND_CUBIC_EVEN, MAJOR_CUBIC, MINOR_EVEN ],
|
3394
|
+
[ ROUND_CUBIC_ODD, MAJOR_CUBIC, MINOR_ODD ],
|
3395
|
+
[ ROUND_CUBIC_FLOOR, MAJOR_CUBIC, MINOR_FLOOR ],
|
3396
|
+
[ ROUND_CUBIC_UP, MAJOR_CUBIC, MINOR_UP ],
|
3397
|
+
[ ROUND_GEOMETRIC_CEILING, MAJOR_GEOMETRIC, MINOR_CEILING ],
|
3398
|
+
[ ROUND_GEOMETRIC_DOWN, MAJOR_GEOMETRIC, MINOR_DOWN ],
|
3399
|
+
[ ROUND_GEOMETRIC_EVEN, MAJOR_GEOMETRIC, MINOR_EVEN ],
|
3400
|
+
[ ROUND_GEOMETRIC_ODD, MAJOR_GEOMETRIC, MINOR_ODD ],
|
3401
|
+
[ ROUND_GEOMETRIC_FLOOR, MAJOR_GEOMETRIC, MINOR_FLOOR ],
|
3402
|
+
[ ROUND_GEOMETRIC_UP, MAJOR_GEOMETRIC, MINOR_UP ],
|
3403
|
+
[ ROUND_HALF_CEILING, MAJOR_HALF, MINOR_CEILING ],
|
3404
|
+
[ ROUND_HALF_DOWN, MAJOR_HALF, MINOR_DOWN ],
|
3405
|
+
[ ROUND_HALF_EVEN, MAJOR_HALF, MINOR_EVEN ],
|
3406
|
+
[ ROUND_HALF_ODD, MAJOR_HALF, MINOR_ODD ],
|
3407
|
+
[ ROUND_HALF_FLOOR, MAJOR_HALF, MINOR_FLOOR ],
|
3408
|
+
[ ROUND_HALF_UP, MAJOR_HALF, MINOR_UP ],
|
3409
|
+
[ ROUND_HARMONIC_CEILING, MAJOR_HARMONIC, MINOR_CEILING ],
|
3410
|
+
[ ROUND_HARMONIC_DOWN, MAJOR_HARMONIC, MINOR_DOWN ],
|
3411
|
+
[ ROUND_HARMONIC_EVEN, MAJOR_HARMONIC, MINOR_EVEN ],
|
3412
|
+
[ ROUND_HARMONIC_ODD, MAJOR_HARMONIC, MINOR_ODD ],
|
3413
|
+
[ ROUND_HARMONIC_FLOOR, MAJOR_HARMONIC, MINOR_FLOOR ],
|
3414
|
+
[ ROUND_HARMONIC_UP, MAJOR_HARMONIC, MINOR_UP ],
|
3415
|
+
[ ROUND_QUADRATIC_CEILING, MAJOR_QUADRATIC, MINOR_CEILING ],
|
3416
|
+
[ ROUND_QUADRATIC_DOWN, MAJOR_QUADRATIC, MINOR_DOWN ],
|
3417
|
+
[ ROUND_QUADRATIC_EVEN, MAJOR_QUADRATIC, MINOR_EVEN ],
|
3418
|
+
[ ROUND_QUADRATIC_ODD, MAJOR_QUADRATIC, MINOR_ODD ],
|
3419
|
+
[ ROUND_QUADRATIC_FLOOR, MAJOR_QUADRATIC, MINOR_FLOOR ],
|
3420
|
+
[ ROUND_QUADRATIC_UP, MAJOR_QUADRATIC, MINOR_UP ]].each do |triple|
|
3421
|
+
mode = triple[0]
|
3422
|
+
major = triple[1]
|
3423
|
+
minor = triple[2]
|
3424
|
+
found = MODE_LOOKUP[[major, minor]]
|
3425
|
+
assert_equal(mode, found)
|
3426
|
+
assert_same(mode, found)
|
3427
|
+
assert_equal(major, mode.major)
|
3428
|
+
assert_equal(major, found.major)
|
3429
|
+
assert_equal(minor, mode.minor)
|
3430
|
+
assert_equal(minor, found.minor)
|
3431
|
+
end
|
3432
|
+
end
|
3433
|
+
|
3434
|
+
# test remainder rounding half with exact boundary
|
3435
|
+
#
|
3436
|
+
def test_int_round_half_int_param_to_two_allowed_remainders
|
3437
|
+
print "\ntest_int_round_half_to_two_allowed_remainders [#{Time.now}]: "
|
3438
|
+
$stdout.flush
|
3439
|
+
check_int_round_major_to_two_allowed_remainders([[1, 3], [1, 5], [2, 4], [2, 6], [3, 5], [3, 7]], true, MAJOR_HALF) do |x, y|
|
3440
|
+
LongMath.arithmetic_mean(0, ROUND_FLOOR, x, y).to_i
|
3441
|
+
end
|
3442
|
+
end
|
3443
|
+
|
3444
|
+
# test remainder rounding half with exact boundary
|
3445
|
+
#
|
3446
|
+
def test_int_round_half_non_int_param_to_two_allowed_remainders
|
3447
|
+
print "\ntest_int_round_half_to_two_allowed_remainders [#{Time.now}] (long): "
|
3448
|
+
$stdout.flush
|
3449
|
+
check_int_round_major_to_two_allowed_remainders([[1, 2], [1, 4], [1, 6], [2, 3], [2, 5], [2, 7], [3, 4], [3, 6], [3, 8], [4, 5], [4, 7]], false, MAJOR_HALF) do |x, y|
|
3450
|
+
LongMath.arithmetic_mean(0, ROUND_FLOOR, x, y).to_i
|
3451
|
+
end
|
3452
|
+
end
|
3453
|
+
|
3454
|
+
# test remainder rounding geometric with exact boundary
|
3455
|
+
#
|
3456
|
+
def test_int_round_geometric_int_param_to_two_allowed_remainders
|
3457
|
+
print "\ntest_int_round_geometric_to_two_allowed_remainders [#{Time.now}]: "
|
3458
|
+
$stdout.flush
|
3459
|
+
check_int_round_major_to_two_allowed_remainders([[1, 4], [1, 9], [2, 8]], true, MAJOR_GEOMETRIC) do |x, y|
|
3460
|
+
LongMath.geometric_mean(0, ROUND_FLOOR, x, y).to_i
|
3461
|
+
end
|
3462
|
+
end
|
3463
|
+
|
3464
|
+
# test remainder rounding geometric with exact boundary
|
3465
|
+
#
|
3466
|
+
def test_int_round_geometric_non_int_param_to_two_allowed_remainders
|
3467
|
+
print "\ntest_int_round_geometric_to_two_allowed_remainders [#{Time.now}] (long): "
|
3468
|
+
$stdout.flush
|
3469
|
+
check_int_round_major_to_two_allowed_remainders([[1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [1, 8], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7]], false, MAJOR_GEOMETRIC) do |x, y|
|
3470
|
+
LongMath.geometric_mean(0, ROUND_FLOOR, x, y).to_i
|
3471
|
+
end
|
3472
|
+
end
|
3473
|
+
|
3474
|
+
# test remainder rounding harmonic with exact boundary
|
3475
|
+
#
|
3476
|
+
def test_int_round_harmonic_int_param_to_two_allowed_remainders
|
3477
|
+
print "\ntest_int_round_harmonic_to_two_allowed_remainders [#{Time.now}]: "
|
3478
|
+
$stdout.flush
|
3479
|
+
check_int_round_major_to_two_allowed_remainders([[2, 6], [3, 6]], true, MAJOR_HARMONIC) do |x, y|
|
3480
|
+
LongMath.harmonic_mean(0, ROUND_FLOOR, x, y).to_i
|
3481
|
+
end
|
3482
|
+
end
|
3483
|
+
|
3484
|
+
# test remainder rounding harmonic with exact boundary
|
3485
|
+
#
|
3486
|
+
def test_int_round_harmonic_non_int_param_to_two_allowed_remainders
|
3487
|
+
print "\ntest_int_round_harmonic_to_two_allowed_remainders [#{Time.now}] (long): "
|
3488
|
+
$stdout.flush
|
3489
|
+
check_int_round_major_to_two_allowed_remainders([[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [2, 3], [2, 4], [2, 5], [2, 7]], false, MAJOR_HARMONIC) do |x, y|
|
3490
|
+
LongMath.harmonic_mean(0, ROUND_FLOOR, x, y).to_i
|
3491
|
+
end
|
3492
|
+
end
|
3493
|
+
|
3494
|
+
# test remainder rounding quadratic with exact boundary
|
3495
|
+
#
|
3496
|
+
def test_int_round_quadratic_int_param_to_two_allowed_remainders
|
3497
|
+
print "\ntest_int_round_quadratic_to_two_allowed_remainders [#{Time.now}]: "
|
3498
|
+
$stdout.flush
|
3499
|
+
check_int_round_major_to_two_allowed_remainders([[1, 7]], true, MAJOR_QUADRATIC) do |x, y|
|
3500
|
+
LongMath.quadratic_mean(0, ROUND_FLOOR, x, y).to_i
|
3501
|
+
end
|
3502
|
+
end
|
3503
|
+
|
3504
|
+
# test remainder rounding quadratic with exact boundary
|
3505
|
+
#
|
3506
|
+
def test_int_round_quadratic_non_int_param_to_two_allowed_remainders
|
3507
|
+
print "\ntest_int_round_quadratic_to_two_allowed_remainders [#{Time.now}] (long): "
|
3508
|
+
$stdout.flush
|
3509
|
+
check_int_round_major_to_two_allowed_remainders([[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 8], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7]], false, MAJOR_QUADRATIC) do |x, y|
|
3510
|
+
LongMath.quadratic_mean(0, ROUND_FLOOR, x, y).to_i
|
3511
|
+
end
|
3512
|
+
end
|
3513
|
+
|
3514
|
+
# test remainder rounding cubic with exact boundary
|
3515
|
+
#
|
3516
|
+
def test_int_round_cubic_non_int_param_to_two_allowed_remainders
|
3517
|
+
print "\ntest_int_round_cubic_non_int_param_to_two_allowed_remainders [#{Time.now}]: "
|
3518
|
+
$stdout.flush
|
3519
|
+
check_int_round_major_to_two_allowed_remainders([[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [2, 3], [2, 4], [2, 5], [2, 7]], false, MAJOR_CUBIC) do |x, y|
|
3520
|
+
LongMath.cubic_mean(0, ROUND_FLOOR, x, y).to_i
|
3521
|
+
end
|
3522
|
+
end
|
3523
|
+
|
3524
|
+
# TODO: round to zero, away from zero to negative, away from zero to positive, across zero to negative, across zero to positive
|
3525
|
+
|
3526
|
+
# test remainder rounding geometric
|
3527
|
+
#
|
3528
|
+
def check_int_round_major_to_two_allowed_remainders(remainder_sets, boundary_exact_integral, major_mode, &block)
|
3529
|
+
|
3530
|
+
mode_up = MODE_LOOKUP[[major_mode, MINOR_UP]]
|
3531
|
+
mode_down = MODE_LOOKUP[[major_mode, MINOR_DOWN]]
|
3532
|
+
mode_floor = MODE_LOOKUP[[major_mode, MINOR_FLOOR]]
|
3533
|
+
mode_ceiling = MODE_LOOKUP[[major_mode, MINOR_CEILING]]
|
3534
|
+
|
3535
|
+
20.upto 25 do |modulus|
|
3536
|
+
remainder_sets.each do |remainders|
|
3537
|
+
print "."
|
3538
|
+
$stdout.flush
|
3539
|
+
|
3540
|
+
lower_remainder = remainders[0]
|
3541
|
+
upper_remainder = remainders[1]
|
3542
|
+
|
3543
|
+
text = "m=#{modulus} x=#{0} s=#{remainders.inspect}"
|
3544
|
+
|
3545
|
+
zero_rounded = 0.round_to_allowed_remainders(remainders, modulus, mode_up, ZERO_ROUND_TO_PLUS)
|
3546
|
+
assert_equal(lower_remainder, zero_rounded, text + " zero_rounded=#{zero_rounded}")
|
3547
|
+
zero_rounded = 0.round_to_allowed_remainders(remainders, modulus, mode_down, ZERO_ROUND_TO_PLUS)
|
3548
|
+
assert_equal(lower_remainder, zero_rounded, text + " zero_rounded=#{zero_rounded}")
|
3549
|
+
zero_rounded = 0.round_to_allowed_remainders(remainders, modulus, mode_up, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3550
|
+
assert_equal(lower_remainder, zero_rounded, text + " zero_rounded=#{zero_rounded}")
|
3551
|
+
zero_rounded = 0.round_to_allowed_remainders(remainders, modulus, mode_down, ZERO_ROUND_TO_CLOSEST_PREFER_PLUS)
|
3552
|
+
assert_equal(lower_remainder, zero_rounded, text + " zero_rounded=#{zero_rounded}")
|
3553
|
+
zero_rounded = 0.round_to_allowed_remainders(remainders, modulus, mode_ceiling, ZERO_ROUND_TO_PLUS)
|
3554
|
+
assert_equal(lower_remainder, zero_rounded, text)
|
3555
|
+
|
3556
|
+
# zero_r1 = 0.round_to_allowed_remainders(remainders, modulus, mode_up, ZERO_ROUND_TO_MINUS)
|
3557
|
+
# zero_r2 = 0.round_to_allowed_remainders(remainders, modulus, mode_down, ZERO_ROUND_TO_MINUS)
|
3558
|
+
# assert_equal(zero_r1, zero_r2, text + " zero_r1=#{zero_r1} zero_r2=#{zero_r2}")
|
3559
|
+
# assert_equal(upper_remainder - modulus, zero_r1, text + " zero_r1=#{zero_r1} zero_r2=#{zero_r2}")
|
3560
|
+
|
3561
|
+
# zero_r0 = 0.round_to_allowed_remainders(remainders, modulus, mode_floor, ZERO_ROUND_TO_MINUS)
|
3562
|
+
# assert_equal(upper_remainder - modulus, zero_r0, "zero_r0=#{zero_r0} < 0 " + text)
|
3563
|
+
|
3564
|
+
param = block.yield(lower_remainder, upper_remainder)
|
3565
|
+
boundary_lower = nil
|
3566
|
+
boundary_upper = nil
|
3567
|
+
if (boundary_exact_integral)
|
3568
|
+
boundary_lower = param - 1
|
3569
|
+
else
|
3570
|
+
boundary_lower = param
|
3571
|
+
end
|
3572
|
+
boundary_upper = param +1
|
3573
|
+
|
3574
|
+
1.upto boundary_lower do |x|
|
3575
|
+
ALL_ROUNDING_MODES.each do |rm|
|
3576
|
+
if (rm.major == major_mode && rm.minor != MINOR_EVEN && rm.minor != MINOR_ODD)
|
3577
|
+
text = "m=#{modulus} x=#{x} rm=#{rm} s=#{remainders.inspect} param=#{param}"
|
3578
|
+
one_r0 = x.round_to_allowed_remainders(remainders, modulus, rm, ZERO_ROUND_UNNECESSARY)
|
3579
|
+
assert_equal(lower_remainder, one_r0, text)
|
3580
|
+
end
|
3581
|
+
end
|
3582
|
+
end
|
3583
|
+
if (boundary_exact_integral)
|
3584
|
+
ALL_ROUNDING_MODES.each do |rm|
|
3585
|
+
if (rm.major == major_mode && rm.minor != MINOR_EVEN && rm.minor != MINOR_ODD)
|
3586
|
+
text = "m=#{modulus} x=#{param} rm=#{rm} s=#{remainders.inspect}"
|
3587
|
+
x_r0 = param.round_to_allowed_remainders(remainders, modulus, rm, ZERO_ROUND_UNNECESSARY)
|
3588
|
+
assert(remainders.member?(x_r0), "x_r0=#{x_r0} " + text)
|
3589
|
+
end
|
3590
|
+
end
|
3591
|
+
end
|
3592
|
+
boundary_upper.upto upper_remainder do |x|
|
3593
|
+
ALL_ROUNDING_MODES.each do |rm|
|
3594
|
+
if (rm.major == major_mode && rm.minor != MINOR_EVEN && rm.minor != MINOR_ODD)
|
3595
|
+
text = "m=#{modulus} x=#{x} rm=#{rm} s=#{remainders.inspect}"
|
3596
|
+
other_r0 = x.round_to_allowed_remainders(remainders, modulus, rm, ZERO_ROUND_UNNECESSARY)
|
3597
|
+
assert_equal(upper_remainder, other_r0, text)
|
3598
|
+
end
|
3599
|
+
end
|
3600
|
+
end
|
3601
|
+
end
|
3602
|
+
end
|
3603
|
+
end
|
3604
|
+
|
1962
3605
|
#
|
1963
3606
|
# test conversion to String
|
1964
3607
|
#
|
@@ -1995,53 +3638,53 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
1995
3638
|
l = LongDecimal(224, 0)
|
1996
3639
|
s = l.to_s(5)
|
1997
3640
|
assert_equal("224.00000", s, "l=#{l.inspect} 5")
|
1998
|
-
s = l.to_s(5,
|
3641
|
+
s = l.to_s(5, ROUND_UNNECESSARY, 16)
|
1999
3642
|
assert_equal("e0.00000", s, "l=#{l.inspect} 5 ROUND_UNNECESSARY 16")
|
2000
3643
|
|
2001
3644
|
l = LongDecimal(224, 1)
|
2002
|
-
s = l.to_s(0,
|
3645
|
+
s = l.to_s(0, ROUND_HALF_UP)
|
2003
3646
|
assert_equal("22", s, "l=#{l.inspect} 0 ROUND_HALF_UP")
|
2004
|
-
s = l.to_s(5,
|
3647
|
+
s = l.to_s(5, ROUND_HALF_UP)
|
2005
3648
|
assert_equal("22.40000", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2006
|
-
s = l.to_s(5,
|
3649
|
+
s = l.to_s(5, ROUND_HALF_UP, 16)
|
2007
3650
|
assert_equal("16.66666", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2008
|
-
s = l.to_s(5,
|
3651
|
+
s = l.to_s(5, ROUND_HALF_DOWN, 16)
|
2009
3652
|
assert_equal("16.66666", s, "l=#{l.inspect} 5 ROUND_HALF_DOWN")
|
2010
3653
|
|
2011
3654
|
l = LongDecimal(224, 2)
|
2012
|
-
s = l.to_s(0,
|
3655
|
+
s = l.to_s(0, ROUND_HALF_UP)
|
2013
3656
|
assert_equal("2", s, "l=#{l.inspect} 0 ROUND_HALF_UP")
|
2014
|
-
s = l.to_s(5,
|
3657
|
+
s = l.to_s(5, ROUND_HALF_UP)
|
2015
3658
|
assert_equal("2.24000", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2016
|
-
s = l.to_s(5,
|
3659
|
+
s = l.to_s(5, ROUND_HALF_UP, 16)
|
2017
3660
|
assert_equal("2.3d70a", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2018
|
-
s = l.to_s(5,
|
3661
|
+
s = l.to_s(5, ROUND_HALF_DOWN, 16)
|
2019
3662
|
assert_equal("2.3d70a", s, "l=#{l.inspect} 5 ROUND_HALF_DOWN")
|
2020
3663
|
|
2021
3664
|
l = LongDecimal(-224, 0)
|
2022
3665
|
s = l.to_s(5)
|
2023
3666
|
assert_equal("-224.00000", s, "l=#{l.inspect} 5")
|
2024
|
-
s = l.to_s(5,
|
3667
|
+
s = l.to_s(5, ROUND_UNNECESSARY, 16)
|
2025
3668
|
assert_equal("-e0.00000", s, "l=#{l.inspect} 5 ROUND_UNNECESSARY 16")
|
2026
3669
|
|
2027
3670
|
l = LongDecimal(-224, 1)
|
2028
|
-
s = l.to_s(0,
|
3671
|
+
s = l.to_s(0, ROUND_HALF_UP)
|
2029
3672
|
assert_equal("-22", s, "l=#{l.inspect} 0 ROUND_HALF_UP")
|
2030
|
-
s = l.to_s(5,
|
3673
|
+
s = l.to_s(5, ROUND_HALF_UP)
|
2031
3674
|
assert_equal("-22.40000", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2032
|
-
s = l.to_s(5,
|
3675
|
+
s = l.to_s(5, ROUND_HALF_UP, 16)
|
2033
3676
|
assert_equal("-16.66666", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2034
|
-
s = l.to_s(5,
|
3677
|
+
s = l.to_s(5, ROUND_HALF_DOWN, 16)
|
2035
3678
|
assert_equal("-16.66666", s, "l=#{l.inspect} 5 ROUND_HALF_DOWN")
|
2036
3679
|
|
2037
3680
|
l = LongDecimal(-224, 2)
|
2038
|
-
s = l.to_s(0,
|
3681
|
+
s = l.to_s(0, ROUND_HALF_UP)
|
2039
3682
|
assert_equal("-2", s, "l=#{l.inspect} 0 ROUND_HALF_UP")
|
2040
|
-
s = l.to_s(5,
|
3683
|
+
s = l.to_s(5, ROUND_HALF_UP)
|
2041
3684
|
assert_equal("-2.24000", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2042
|
-
s = l.to_s(5,
|
3685
|
+
s = l.to_s(5, ROUND_HALF_UP, 16)
|
2043
3686
|
assert_equal("-2.3d70a", s, "l=#{l.inspect} 5 ROUND_HALF_UP")
|
2044
|
-
s = l.to_s(5,
|
3687
|
+
s = l.to_s(5, ROUND_HALF_DOWN, 16)
|
2045
3688
|
assert_equal("-2.3d70a", s, "l=#{l.inspect} 5 ROUND_HALF_DOWN")
|
2046
3689
|
end
|
2047
3690
|
|
@@ -2070,11 +3713,11 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
2070
3713
|
def test_to_ld
|
2071
3714
|
print "\ntest_to_ld [#{Time.now}]: "
|
2072
3715
|
x = LongDecimal(123, 100)
|
2073
|
-
y = x.to_ld(20,
|
3716
|
+
y = x.to_ld(20, ROUND_UP)
|
2074
3717
|
z = LongDecimal(1, 20)
|
2075
3718
|
assert_kind_of(LongDecimal, y, "must be ld")
|
2076
3719
|
assert_equal(y, z, "x=#{x} y=#{y}")
|
2077
|
-
y = x.to_ld(20,
|
3720
|
+
y = x.to_ld(20, ROUND_HALF_UP)
|
2078
3721
|
z = LongDecimal(0, 20)
|
2079
3722
|
assert_kind_of(LongDecimal, y, "must be ld")
|
2080
3723
|
assert_equal(y, z, "x=#{x} y=#{y}")
|
@@ -2340,11 +3983,11 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
2340
3983
|
print "\ntest_int_mul [#{Time.now}] (90 sec): "
|
2341
3984
|
$stdout.flush
|
2342
3985
|
65.times do |i|
|
2343
|
-
x0 = (1<<i)-1
|
3986
|
+
x0 = (1 << i)-1
|
2344
3987
|
3.times do |k|
|
2345
3988
|
x = x0+k
|
2346
3989
|
65.times do |j|
|
2347
|
-
y0 = (1<<j)-1
|
3990
|
+
y0 = (1 << j)-1
|
2348
3991
|
3.times do |l|
|
2349
3992
|
y = y0+l
|
2350
3993
|
z = x*y
|
@@ -2366,7 +4009,7 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
2366
4009
|
# test multiplication which uses internally multiplication of fixnum, which is buggy in JRuby and has been fixed here.
|
2367
4010
|
#
|
2368
4011
|
def test_mul2
|
2369
|
-
print "\ntest_mul2 [#{Time.now}]
|
4012
|
+
print "\ntest_mul2 [#{Time.now}]: "
|
2370
4013
|
$stdout.flush
|
2371
4014
|
|
2372
4015
|
map = {}
|
@@ -2383,7 +4026,6 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
2383
4026
|
end
|
2384
4027
|
end
|
2385
4028
|
map.each do |x, x1|
|
2386
|
-
# puts "x=#{x} x1=#{x1}"
|
2387
4029
|
print ":"
|
2388
4030
|
$stdout.flush
|
2389
4031
|
map.each do |y, y1|
|
@@ -2765,248 +4407,248 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
2765
4407
|
|
2766
4408
|
y = LongDecimal(3, 1) # 0.3 dy=0 sy=1
|
2767
4409
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = -3 -> use 0
|
2768
|
-
z = x.divide(y,
|
2769
|
-
zz = Rational(224, 30).to_ld(0,
|
4410
|
+
z = x.divide(y, ROUND_DOWN)
|
4411
|
+
zz = Rational(224, 30).to_ld(0, ROUND_DOWN)
|
2770
4412
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2771
4413
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2772
|
-
z = x.divide(y,
|
2773
|
-
zz = Rational(224, 30).to_ld(0,
|
4414
|
+
z = x.divide(y, ROUND_UP)
|
4415
|
+
zz = Rational(224, 30).to_ld(0, ROUND_UP)
|
2774
4416
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2775
4417
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2776
4418
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -1 -> use 0
|
2777
|
-
z = y.divide(x,
|
2778
|
-
zz = Rational(30, 224).to_ld(0,
|
4419
|
+
z = y.divide(x, ROUND_DOWN)
|
4420
|
+
zz = Rational(30, 224).to_ld(0, ROUND_DOWN)
|
2779
4421
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2780
4422
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2781
|
-
z = y.divide(x,
|
2782
|
-
zz = Rational(30, 224).to_ld(0,
|
4423
|
+
z = y.divide(x, ROUND_UP)
|
4424
|
+
zz = Rational(30, 224).to_ld(0, ROUND_UP)
|
2783
4425
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2784
4426
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2785
4427
|
|
2786
4428
|
# x= 2.24 dx=1 sx=2
|
2787
4429
|
y = LongDecimal(30000000, 8) # 0.30000000 dy=0 sy=8
|
2788
4430
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = -1 -> use 0
|
2789
|
-
z = x.divide(y,
|
2790
|
-
zz = Rational(224, 30).to_ld(0,
|
4431
|
+
z = x.divide(y, ROUND_DOWN)
|
4432
|
+
zz = Rational(224, 30).to_ld(0, ROUND_DOWN)
|
2791
4433
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2792
4434
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2793
|
-
z = x.divide(y,
|
2794
|
-
zz = Rational(224, 30).to_ld(0,
|
4435
|
+
z = x.divide(y, ROUND_UP)
|
4436
|
+
zz = Rational(224, 30).to_ld(0, ROUND_UP)
|
2795
4437
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2796
4438
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2797
4439
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = 1
|
2798
|
-
z = y.divide(x,
|
2799
|
-
zz = Rational(30, 224).to_ld(1,
|
4440
|
+
z = y.divide(x, ROUND_DOWN)
|
4441
|
+
zz = Rational(30, 224).to_ld(1, ROUND_DOWN)
|
2800
4442
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2801
4443
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2802
|
-
z = y.divide(x,
|
2803
|
-
zz = Rational(30, 224).to_ld(1,
|
4444
|
+
z = y.divide(x, ROUND_UP)
|
4445
|
+
zz = Rational(30, 224).to_ld(1, ROUND_UP)
|
2804
4446
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2805
4447
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2806
4448
|
|
2807
4449
|
# x= 2.24 dx=1 sx=2
|
2808
4450
|
y = LongDecimal(3, 4) # 0.0003 dy=-4 sy=4
|
2809
4451
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = -8 -> use 0
|
2810
|
-
z = x.divide(y,
|
2811
|
-
zz = Rational(22400, 3).to_ld(0,
|
4452
|
+
z = x.divide(y, ROUND_DOWN)
|
4453
|
+
zz = Rational(22400, 3).to_ld(0, ROUND_DOWN)
|
2812
4454
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2813
4455
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2814
|
-
z = x.divide(y,
|
2815
|
-
zz = Rational(22400, 3).to_ld(0,
|
4456
|
+
z = x.divide(y, ROUND_UP)
|
4457
|
+
zz = Rational(22400, 3).to_ld(0, ROUND_UP)
|
2816
4458
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2817
4459
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2818
4460
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = 2
|
2819
|
-
z = y.divide(x,
|
2820
|
-
zz = Rational(3, 22400).to_ld(2,
|
4461
|
+
z = y.divide(x, ROUND_DOWN)
|
4462
|
+
zz = Rational(3, 22400).to_ld(2, ROUND_DOWN)
|
2821
4463
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2822
4464
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2823
|
-
z = y.divide(x,
|
2824
|
-
zz = Rational(3, 22400).to_ld(2,
|
4465
|
+
z = y.divide(x, ROUND_UP)
|
4466
|
+
zz = Rational(3, 22400).to_ld(2, ROUND_UP)
|
2825
4467
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2826
4468
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2827
4469
|
|
2828
4470
|
# x= 2.24 dx=1 sx=2
|
2829
4471
|
y = LongDecimal(3333, 2) # 33.33 dy=2 sy=2
|
2830
4472
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 1
|
2831
|
-
z = x.divide(y,
|
2832
|
-
zz = Rational(224, 3333).to_ld(1,
|
4473
|
+
z = x.divide(y, ROUND_DOWN)
|
4474
|
+
zz = Rational(224, 3333).to_ld(1, ROUND_DOWN)
|
2833
4475
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2834
4476
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2835
|
-
z = x.divide(y,
|
2836
|
-
zz = Rational(224, 3333).to_ld(1,
|
4477
|
+
z = x.divide(y, ROUND_UP)
|
4478
|
+
zz = Rational(224, 3333).to_ld(1, ROUND_UP)
|
2837
4479
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2838
4480
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2839
4481
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -1 -> use 0
|
2840
|
-
z = y.divide(x,
|
2841
|
-
zz = Rational(3333, 224).to_ld(0,
|
4482
|
+
z = y.divide(x, ROUND_DOWN)
|
4483
|
+
zz = Rational(3333, 224).to_ld(0, ROUND_DOWN)
|
2842
4484
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2843
4485
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2844
|
-
z = y.divide(x,
|
2845
|
-
zz = Rational(3333, 224).to_ld(0,
|
4486
|
+
z = y.divide(x, ROUND_UP)
|
4487
|
+
zz = Rational(3333, 224).to_ld(0, ROUND_UP)
|
2846
4488
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2847
4489
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2848
4490
|
|
2849
4491
|
# x= 2.24 dx=1 sx=2
|
2850
4492
|
y = LongDecimal(33333, 2) # 333.33 dy=3 sy=2
|
2851
4493
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 2
|
2852
|
-
z = x.divide(y,
|
2853
|
-
zz = Rational(224, 33333).to_ld(2,
|
4494
|
+
z = x.divide(y, ROUND_DOWN)
|
4495
|
+
zz = Rational(224, 33333).to_ld(2, ROUND_DOWN)
|
2854
4496
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2855
4497
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2856
|
-
z = x.divide(y,
|
2857
|
-
zz = Rational(224, 33333).to_ld(2,
|
4498
|
+
z = x.divide(y, ROUND_UP)
|
4499
|
+
zz = Rational(224, 33333).to_ld(2, ROUND_UP)
|
2858
4500
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2859
4501
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2860
4502
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -2 -> use 0
|
2861
|
-
z = y.divide(x,
|
2862
|
-
zz = Rational(33333, 224).to_ld(0,
|
4503
|
+
z = y.divide(x, ROUND_DOWN)
|
4504
|
+
zz = Rational(33333, 224).to_ld(0, ROUND_DOWN)
|
2863
4505
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2864
4506
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2865
|
-
z = y.divide(x,
|
2866
|
-
zz = Rational(33333, 224).to_ld(0,
|
4507
|
+
z = y.divide(x, ROUND_UP)
|
4508
|
+
zz = Rational(33333, 224).to_ld(0, ROUND_UP)
|
2867
4509
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2868
4510
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2869
4511
|
|
2870
4512
|
# x= 2.24 dx=1 sx=2
|
2871
4513
|
y = LongDecimal(33333, 3) # 33.333 dy=2 sy=3
|
2872
4514
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 1
|
2873
|
-
z = x.divide(y,
|
2874
|
-
zz = Rational(2240, 33333).to_ld(1,
|
4515
|
+
z = x.divide(y, ROUND_DOWN)
|
4516
|
+
zz = Rational(2240, 33333).to_ld(1, ROUND_DOWN)
|
2875
4517
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2876
4518
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2877
|
-
z = x.divide(y,
|
2878
|
-
zz = Rational(2240, 33333).to_ld(1,
|
4519
|
+
z = x.divide(y, ROUND_UP)
|
4520
|
+
zz = Rational(2240, 33333).to_ld(1, ROUND_UP)
|
2879
4521
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2880
4522
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2881
4523
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -1 -> use 0
|
2882
|
-
z = y.divide(x,
|
2883
|
-
zz = Rational(33333, 2240).to_ld(0,
|
4524
|
+
z = y.divide(x, ROUND_DOWN)
|
4525
|
+
zz = Rational(33333, 2240).to_ld(0, ROUND_DOWN)
|
2884
4526
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2885
4527
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2886
|
-
z = y.divide(x,
|
2887
|
-
zz = Rational(33333, 2240).to_ld(0,
|
4528
|
+
z = y.divide(x, ROUND_UP)
|
4529
|
+
zz = Rational(33333, 2240).to_ld(0, ROUND_UP)
|
2888
4530
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2889
4531
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2890
4532
|
|
2891
4533
|
# x= 2.24 dx=1 sx=2
|
2892
4534
|
y = LongDecimal(3333, 3) # 3.333 dy=1 sy=3
|
2893
4535
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 0
|
2894
|
-
z = x.divide(y,
|
2895
|
-
zz = Rational(2240, 3333).to_ld(0,
|
4536
|
+
z = x.divide(y, ROUND_DOWN)
|
4537
|
+
zz = Rational(2240, 3333).to_ld(0, ROUND_DOWN)
|
2896
4538
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2897
4539
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2898
|
-
z = x.divide(y,
|
2899
|
-
zz = Rational(2240, 3333).to_ld(0,
|
4540
|
+
z = x.divide(y, ROUND_UP)
|
4541
|
+
zz = Rational(2240, 3333).to_ld(0, ROUND_UP)
|
2900
4542
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2901
4543
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2902
4544
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = 0
|
2903
|
-
z = y.divide(x,
|
2904
|
-
zz = Rational(3333, 2240).to_ld(0,
|
4545
|
+
z = y.divide(x, ROUND_DOWN)
|
4546
|
+
zz = Rational(3333, 2240).to_ld(0, ROUND_DOWN)
|
2905
4547
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2906
4548
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2907
|
-
z = y.divide(x,
|
2908
|
-
zz = Rational(3333, 2240).to_ld(0,
|
4549
|
+
z = y.divide(x, ROUND_UP)
|
4550
|
+
zz = Rational(3333, 2240).to_ld(0, ROUND_UP)
|
2909
4551
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2910
4552
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2911
4553
|
|
2912
4554
|
# x= 2.24 dx=1 sx=2
|
2913
4555
|
y = LongDecimal(123456789, 3) # 123456.789 dy=6 sy=3
|
2914
4556
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 5
|
2915
|
-
z = x.divide(y,
|
2916
|
-
zz = Rational(2240, 123456789).to_ld(5,
|
4557
|
+
z = x.divide(y, ROUND_DOWN)
|
4558
|
+
zz = Rational(2240, 123456789).to_ld(5, ROUND_DOWN)
|
2917
4559
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2918
4560
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2919
|
-
z = x.divide(y,
|
2920
|
-
zz = Rational(2240, 123456789).to_ld(5,
|
4561
|
+
z = x.divide(y, ROUND_UP)
|
4562
|
+
zz = Rational(2240, 123456789).to_ld(5, ROUND_UP)
|
2921
4563
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2922
4564
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2923
4565
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -5 -> use 0
|
2924
|
-
z = y.divide(x,
|
2925
|
-
zz = Rational(123456789, 2240).to_ld(0,
|
4566
|
+
z = y.divide(x, ROUND_DOWN)
|
4567
|
+
zz = Rational(123456789, 2240).to_ld(0, ROUND_DOWN)
|
2926
4568
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2927
4569
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2928
|
-
z = y.divide(x,
|
2929
|
-
zz = Rational(123456789, 2240).to_ld(0,
|
4570
|
+
z = y.divide(x, ROUND_UP)
|
4571
|
+
zz = Rational(123456789, 2240).to_ld(0, ROUND_UP)
|
2930
4572
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2931
4573
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2932
4574
|
|
2933
4575
|
# x= 2.24 dx=1 sx=2
|
2934
4576
|
y = 5.to_ld # 5 dy=1 sy=0
|
2935
4577
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = -2 -> use 0
|
2936
|
-
z = x.divide(y,
|
2937
|
-
zz = Rational(224, 500).to_ld(0,
|
4578
|
+
z = x.divide(y, ROUND_DOWN)
|
4579
|
+
zz = Rational(224, 500).to_ld(0, ROUND_DOWN)
|
2938
4580
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2939
4581
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2940
|
-
z = x.divide(y,
|
2941
|
-
zz = Rational(224, 500).to_ld(0,
|
4582
|
+
z = x.divide(y, ROUND_UP)
|
4583
|
+
zz = Rational(224, 500).to_ld(0, ROUND_UP)
|
2942
4584
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2943
4585
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2944
4586
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = -2 -> use 0
|
2945
|
-
z = y.divide(x,
|
2946
|
-
zz = Rational(500, 224).to_ld(0,
|
4587
|
+
z = y.divide(x, ROUND_DOWN)
|
4588
|
+
zz = Rational(500, 224).to_ld(0, ROUND_DOWN)
|
2947
4589
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2948
4590
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2949
|
-
z = y.divide(x,
|
2950
|
-
zz = Rational(500, 224).to_ld(0,
|
4591
|
+
z = y.divide(x, ROUND_UP)
|
4592
|
+
zz = Rational(500, 224).to_ld(0, ROUND_UP)
|
2951
4593
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2952
4594
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2953
4595
|
|
2954
4596
|
# x= 2.24 dx=1 sx=2
|
2955
4597
|
y = 5.001.to_ld # dy=1 sy=3
|
2956
4598
|
# 2dy+sy+sx-max(dx+sx,dy+sy)-3 = 0
|
2957
|
-
z = x.divide(y,
|
2958
|
-
zz = Rational(224, 500).to_ld(0,
|
4599
|
+
z = x.divide(y, ROUND_DOWN)
|
4600
|
+
zz = Rational(224, 500).to_ld(0, ROUND_DOWN)
|
2959
4601
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2960
4602
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2961
|
-
z = x.divide(y,
|
2962
|
-
zz = Rational(224, 500).to_ld(0,
|
4603
|
+
z = x.divide(y, ROUND_UP)
|
4604
|
+
zz = Rational(224, 500).to_ld(0, ROUND_UP)
|
2963
4605
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2964
4606
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2965
4607
|
# 2dx+sx+sy-max(dx+sx,dy+sy)-3 = 0
|
2966
|
-
z = y.divide(x,
|
2967
|
-
zz = Rational(500, 224).to_ld(0,
|
4608
|
+
z = y.divide(x, ROUND_DOWN)
|
4609
|
+
zz = Rational(500, 224).to_ld(0, ROUND_DOWN)
|
2968
4610
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2969
4611
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2970
|
-
z = y.divide(x,
|
2971
|
-
zz = Rational(500, 224).to_ld(0,
|
4612
|
+
z = y.divide(x, ROUND_UP)
|
4613
|
+
zz = Rational(500, 224).to_ld(0, ROUND_UP)
|
2972
4614
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2973
4615
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2974
4616
|
|
2975
4617
|
y = Rational(5, 3).to_ld
|
2976
4618
|
# y is has no scale, use scale of x
|
2977
|
-
z = x.divide(y,
|
2978
|
-
zz = Rational(224*3, 500).to_ld(0,
|
4619
|
+
z = x.divide(y, ROUND_DOWN)
|
4620
|
+
zz = Rational(224*3, 500).to_ld(0, ROUND_DOWN)
|
2979
4621
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2980
4622
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2981
|
-
z = x.divide(y,
|
2982
|
-
zz = Rational(224*3, 500).to_ld(0,
|
4623
|
+
z = x.divide(y, ROUND_UP)
|
4624
|
+
zz = Rational(224*3, 500).to_ld(0, ROUND_UP)
|
2983
4625
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2984
4626
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2985
4627
|
# y is has no scale, use scale of x
|
2986
|
-
z = y.divide(x,
|
2987
|
-
zz = Rational(500, 224*3).to_ld(0,
|
4628
|
+
z = y.divide(x, ROUND_DOWN)
|
4629
|
+
zz = Rational(500, 224*3).to_ld(0, ROUND_DOWN)
|
2988
4630
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2989
4631
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2990
|
-
z = y.divide(x,
|
2991
|
-
zz = Rational(500, 224*3).to_ld(0,
|
4632
|
+
z = y.divide(x, ROUND_UP)
|
4633
|
+
zz = Rational(500, 224*3).to_ld(0, ROUND_UP)
|
2992
4634
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2993
4635
|
assert_equal(zz, z, "z=#{z.inspect}")
|
2994
4636
|
|
2995
4637
|
y = LongDecimalQuot(Rational(5, 3), 3).to_ld
|
2996
|
-
z = x.divide(y,
|
2997
|
-
zz = Rational(224*3, 500).to_ld(0,
|
4638
|
+
z = x.divide(y, ROUND_DOWN)
|
4639
|
+
zz = Rational(224*3, 500).to_ld(0, ROUND_DOWN)
|
2998
4640
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
2999
4641
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3000
|
-
z = x.divide(y,
|
3001
|
-
zz = Rational(224*3, 500).to_ld(0,
|
4642
|
+
z = x.divide(y, ROUND_UP)
|
4643
|
+
zz = Rational(224*3, 500).to_ld(0, ROUND_UP)
|
3002
4644
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3003
4645
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3004
|
-
z = y.divide(x,
|
3005
|
-
zz = Rational(500, 224*3).to_ld(0,
|
4646
|
+
z = y.divide(x, ROUND_DOWN)
|
4647
|
+
zz = Rational(500, 224*3).to_ld(0, ROUND_DOWN)
|
3006
4648
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3007
4649
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3008
|
-
z = y.divide(x,
|
3009
|
-
zz = Rational(500, 224*3).to_ld(0,
|
4650
|
+
z = y.divide(x, ROUND_UP)
|
4651
|
+
zz = Rational(500, 224*3).to_ld(0, ROUND_UP)
|
3010
4652
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3011
4653
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3012
4654
|
end
|
@@ -3020,7 +4662,7 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3020
4662
|
|
3021
4663
|
y = Complex(5, 3)
|
3022
4664
|
puts("x=#{x.inspect} y=#{y.inspect}")
|
3023
|
-
z = x.divide(y,
|
4665
|
+
z = x.divide(y, ROUND_DOWN)
|
3024
4666
|
zz = 2.24 / Complex(5, 3)
|
3025
4667
|
assert_kind_of(Complex, z, "z=#{z.inspect}")
|
3026
4668
|
assert((zz-z).abs < 1e-9, "z=#{z.inspect}")
|
@@ -3035,219 +4677,219 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3035
4677
|
x = LongDecimal(224, 2) # 2.24
|
3036
4678
|
|
3037
4679
|
y = LongDecimal(3, 1) # 0.3
|
3038
|
-
z = x.divide_s(y, 1,
|
3039
|
-
zz = Rational(224, 30).to_ld(1,
|
4680
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4681
|
+
zz = Rational(224, 30).to_ld(1, ROUND_DOWN)
|
3040
4682
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3041
4683
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3042
|
-
z = x.divide_s(y, 1,
|
3043
|
-
zz = Rational(224, 30).to_ld(1,
|
4684
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4685
|
+
zz = Rational(224, 30).to_ld(1, ROUND_UP)
|
3044
4686
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3045
4687
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3046
|
-
z = y.divide_s(x, 1,
|
3047
|
-
zz = Rational(30, 224).to_ld(1,
|
4688
|
+
z = y.divide_s(x, 1, ROUND_DOWN)
|
4689
|
+
zz = Rational(30, 224).to_ld(1, ROUND_DOWN)
|
3048
4690
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3049
4691
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3050
|
-
z = y.divide_s(x, 1,
|
3051
|
-
zz = Rational(30, 224).to_ld(1,
|
4692
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4693
|
+
zz = Rational(30, 224).to_ld(1, ROUND_UP)
|
3052
4694
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3053
4695
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3054
4696
|
|
3055
4697
|
y = LongDecimal(30000000, 8) # 0.30000000
|
3056
|
-
z = x.divide_s(y, 1,
|
3057
|
-
zz = Rational(224, 30).to_ld(1,
|
4698
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4699
|
+
zz = Rational(224, 30).to_ld(1, ROUND_DOWN)
|
3058
4700
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3059
4701
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3060
|
-
z = x.divide_s(y, 1,
|
3061
|
-
zz = Rational(224, 30).to_ld(1,
|
4702
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4703
|
+
zz = Rational(224, 30).to_ld(1, ROUND_UP)
|
3062
4704
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3063
4705
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3064
|
-
z = y.divide_s(x, 2,
|
3065
|
-
zz = Rational(30, 224).to_ld(2,
|
4706
|
+
z = y.divide_s(x, 2, ROUND_DOWN)
|
4707
|
+
zz = Rational(30, 224).to_ld(2, ROUND_DOWN)
|
3066
4708
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3067
4709
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3068
|
-
z = y.divide_s(x, 20,
|
3069
|
-
zz = Rational(30, 224).to_ld(20,
|
4710
|
+
z = y.divide_s(x, 20, ROUND_UP)
|
4711
|
+
zz = Rational(30, 224).to_ld(20, ROUND_UP)
|
3070
4712
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3071
4713
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3072
4714
|
|
3073
4715
|
y = LongDecimal(3, 4) # 0.0003
|
3074
|
-
z = x.divide_s(y, 2,
|
3075
|
-
zz = Rational(22400, 3).to_ld(2,
|
4716
|
+
z = x.divide_s(y, 2, ROUND_DOWN)
|
4717
|
+
zz = Rational(22400, 3).to_ld(2, ROUND_DOWN)
|
3076
4718
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3077
4719
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3078
|
-
z = x.divide_s(y, 2,
|
3079
|
-
zz = Rational(22400, 3).to_ld(2,
|
4720
|
+
z = x.divide_s(y, 2, ROUND_UP)
|
4721
|
+
zz = Rational(22400, 3).to_ld(2, ROUND_UP)
|
3080
4722
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3081
4723
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3082
|
-
z = y.divide_s(x, 3,
|
3083
|
-
zz = Rational(3, 22400).to_ld(3,
|
4724
|
+
z = y.divide_s(x, 3, ROUND_DOWN)
|
4725
|
+
zz = Rational(3, 22400).to_ld(3, ROUND_DOWN)
|
3084
4726
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3085
4727
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3086
|
-
z = y.divide_s(x, 2,
|
3087
|
-
zz = Rational(3, 22400).to_ld(2,
|
4728
|
+
z = y.divide_s(x, 2, ROUND_UP)
|
4729
|
+
zz = Rational(3, 22400).to_ld(2, ROUND_UP)
|
3088
4730
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3089
4731
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3090
4732
|
|
3091
4733
|
y = LongDecimal(3333, 2) # 33.33
|
3092
|
-
z = x.divide_s(y, 4,
|
3093
|
-
zz = Rational(224, 3333).to_ld(4,
|
4734
|
+
z = x.divide_s(y, 4, ROUND_DOWN)
|
4735
|
+
zz = Rational(224, 3333).to_ld(4, ROUND_DOWN)
|
3094
4736
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3095
4737
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3096
|
-
z = x.divide_s(y, 30,
|
3097
|
-
zz = Rational(224, 3333).to_ld(30,
|
4738
|
+
z = x.divide_s(y, 30, ROUND_UP)
|
4739
|
+
zz = Rational(224, 3333).to_ld(30, ROUND_UP)
|
3098
4740
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3099
4741
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3100
|
-
z = y.divide_s(x, 4,
|
3101
|
-
zz = Rational(3333, 224).to_ld(4,
|
4742
|
+
z = y.divide_s(x, 4, ROUND_DOWN)
|
4743
|
+
zz = Rational(3333, 224).to_ld(4, ROUND_DOWN)
|
3102
4744
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3103
4745
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3104
|
-
z = y.divide_s(x, 1,
|
3105
|
-
zz = Rational(3333, 224).to_ld(1,
|
4746
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4747
|
+
zz = Rational(3333, 224).to_ld(1, ROUND_UP)
|
3106
4748
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3107
4749
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3108
4750
|
|
3109
4751
|
y = LongDecimal(33333, 2) # 333.33
|
3110
|
-
z = x.divide_s(y, 3,
|
3111
|
-
zz = Rational(224, 33333).to_ld(3,
|
4752
|
+
z = x.divide_s(y, 3, ROUND_DOWN)
|
4753
|
+
zz = Rational(224, 33333).to_ld(3, ROUND_DOWN)
|
3112
4754
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3113
4755
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3114
|
-
z = x.divide_s(y, 3,
|
3115
|
-
zz = Rational(224, 33333).to_ld(3,
|
4756
|
+
z = x.divide_s(y, 3, ROUND_UP)
|
4757
|
+
zz = Rational(224, 33333).to_ld(3, ROUND_UP)
|
3116
4758
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3117
4759
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3118
|
-
z = y.divide_s(x, 33,
|
3119
|
-
zz = Rational(33333, 224).to_ld(33,
|
4760
|
+
z = y.divide_s(x, 33, ROUND_DOWN)
|
4761
|
+
zz = Rational(33333, 224).to_ld(33, ROUND_DOWN)
|
3120
4762
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3121
4763
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3122
|
-
z = y.divide_s(x, 4,
|
3123
|
-
zz = Rational(33333, 224).to_ld(4,
|
4764
|
+
z = y.divide_s(x, 4, ROUND_UP)
|
4765
|
+
zz = Rational(33333, 224).to_ld(4, ROUND_UP)
|
3124
4766
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3125
4767
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3126
4768
|
|
3127
4769
|
y = LongDecimal(33333, 3) # 33.333
|
3128
|
-
z = x.divide_s(y, 2,
|
3129
|
-
zz = Rational(2240, 33333).to_ld(2,
|
4770
|
+
z = x.divide_s(y, 2, ROUND_DOWN)
|
4771
|
+
zz = Rational(2240, 33333).to_ld(2, ROUND_DOWN)
|
3130
4772
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3131
4773
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3132
|
-
z = x.divide_s(y, 2,
|
3133
|
-
zz = Rational(2240, 33333).to_ld(2,
|
4774
|
+
z = x.divide_s(y, 2, ROUND_UP)
|
4775
|
+
zz = Rational(2240, 33333).to_ld(2, ROUND_UP)
|
3134
4776
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3135
4777
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3136
|
-
z = y.divide_s(x, 2,
|
3137
|
-
zz = Rational(33333, 2240).to_ld(2,
|
4778
|
+
z = y.divide_s(x, 2, ROUND_DOWN)
|
4779
|
+
zz = Rational(33333, 2240).to_ld(2, ROUND_DOWN)
|
3138
4780
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3139
4781
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3140
|
-
z = y.divide_s(x, 1,
|
3141
|
-
zz = Rational(33333, 2240).to_ld(1,
|
4782
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4783
|
+
zz = Rational(33333, 2240).to_ld(1, ROUND_UP)
|
3142
4784
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3143
4785
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3144
4786
|
|
3145
4787
|
y = LongDecimal(3333, 3) # 3.333
|
3146
|
-
z = x.divide_s(y, 1,
|
3147
|
-
zz = Rational(2240, 3333).to_ld(1,
|
4788
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4789
|
+
zz = Rational(2240, 3333).to_ld(1, ROUND_DOWN)
|
3148
4790
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3149
4791
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3150
|
-
z = x.divide_s(y, 1,
|
3151
|
-
zz = Rational(2240, 3333).to_ld(1,
|
4792
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4793
|
+
zz = Rational(2240, 3333).to_ld(1, ROUND_UP)
|
3152
4794
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3153
4795
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3154
|
-
z = y.divide_s(x, 1,
|
3155
|
-
zz = Rational(3333, 2240).to_ld(1,
|
4796
|
+
z = y.divide_s(x, 1, ROUND_DOWN)
|
4797
|
+
zz = Rational(3333, 2240).to_ld(1, ROUND_DOWN)
|
3156
4798
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3157
4799
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3158
|
-
z = y.divide_s(x, 1,
|
3159
|
-
zz = Rational(3333, 2240).to_ld(1,
|
4800
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4801
|
+
zz = Rational(3333, 2240).to_ld(1, ROUND_UP)
|
3160
4802
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3161
4803
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3162
4804
|
|
3163
4805
|
y = LongDecimal(123456789, 3) # 123456.789
|
3164
|
-
z = x.divide_s(y, 3,
|
3165
|
-
zz = Rational(2240, 123456789).to_ld(3,
|
4806
|
+
z = x.divide_s(y, 3, ROUND_DOWN)
|
4807
|
+
zz = Rational(2240, 123456789).to_ld(3, ROUND_DOWN)
|
3166
4808
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3167
4809
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3168
|
-
z = x.divide_s(y, 7,
|
3169
|
-
zz = Rational(2240, 123456789).to_ld(7,
|
4810
|
+
z = x.divide_s(y, 7, ROUND_UP)
|
4811
|
+
zz = Rational(2240, 123456789).to_ld(7, ROUND_UP)
|
3170
4812
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3171
4813
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3172
|
-
z = y.divide_s(x, 0,
|
3173
|
-
zz = Rational(123456789, 2240).to_ld(0,
|
4814
|
+
z = y.divide_s(x, 0, ROUND_DOWN)
|
4815
|
+
zz = Rational(123456789, 2240).to_ld(0, ROUND_DOWN)
|
3174
4816
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3175
4817
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3176
|
-
z = y.divide_s(x, 9,
|
3177
|
-
zz = Rational(123456789, 2240).to_ld(9,
|
4818
|
+
z = y.divide_s(x, 9, ROUND_UP)
|
4819
|
+
zz = Rational(123456789, 2240).to_ld(9, ROUND_UP)
|
3178
4820
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3179
4821
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3180
4822
|
|
3181
4823
|
y = 5.to_ld # 5
|
3182
|
-
z = x.divide_s(y, 1,
|
3183
|
-
zz = Rational(224, 500).to_ld(1,
|
4824
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4825
|
+
zz = Rational(224, 500).to_ld(1, ROUND_DOWN)
|
3184
4826
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3185
4827
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3186
|
-
z = x.divide_s(y, 1,
|
3187
|
-
zz = Rational(224, 500).to_ld(1,
|
4828
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4829
|
+
zz = Rational(224, 500).to_ld(1, ROUND_UP)
|
3188
4830
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3189
4831
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3190
|
-
z = y.divide_s(x, 1,
|
3191
|
-
zz = Rational(500, 224).to_ld(1,
|
4832
|
+
z = y.divide_s(x, 1, ROUND_DOWN)
|
4833
|
+
zz = Rational(500, 224).to_ld(1, ROUND_DOWN)
|
3192
4834
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3193
4835
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3194
|
-
z = y.divide_s(x, 1,
|
3195
|
-
zz = Rational(500, 224).to_ld(1,
|
4836
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4837
|
+
zz = Rational(500, 224).to_ld(1, ROUND_UP)
|
3196
4838
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3197
4839
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3198
4840
|
|
3199
4841
|
y = 5.001.to_ld # 5.001
|
3200
|
-
z = x.divide_s(y, 1,
|
3201
|
-
zz = Rational(224, 500).to_ld(1,
|
4842
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4843
|
+
zz = Rational(224, 500).to_ld(1, ROUND_DOWN)
|
3202
4844
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3203
4845
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3204
|
-
z = x.divide_s(y, 1,
|
3205
|
-
zz = Rational(224, 500).to_ld(1,
|
4846
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4847
|
+
zz = Rational(224, 500).to_ld(1, ROUND_UP)
|
3206
4848
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3207
4849
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3208
|
-
z = y.divide_s(x, 1,
|
3209
|
-
zz = Rational(500, 224).to_ld(1,
|
4850
|
+
z = y.divide_s(x, 1, ROUND_DOWN)
|
4851
|
+
zz = Rational(500, 224).to_ld(1, ROUND_DOWN)
|
3210
4852
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3211
4853
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3212
|
-
z = y.divide_s(x, 1,
|
3213
|
-
zz = Rational(500, 224).to_ld(1,
|
4854
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4855
|
+
zz = Rational(500, 224).to_ld(1, ROUND_UP)
|
3214
4856
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3215
4857
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3216
4858
|
|
3217
4859
|
y = Rational(5, 3).to_ld(3) # 1.667
|
3218
|
-
z = x.divide_s(y, 4,
|
3219
|
-
zz = Rational(2240, 1667).to_ld(4,
|
4860
|
+
z = x.divide_s(y, 4, ROUND_DOWN)
|
4861
|
+
zz = Rational(2240, 1667).to_ld(4, ROUND_DOWN)
|
3220
4862
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3221
4863
|
assert_equal(zz, z, "x=#{x} y=#{y} z=#{z} z=#{z.inspect}")
|
3222
|
-
z = x.divide_s(y, 1,
|
3223
|
-
zz = Rational(2240, 1667).to_ld(1,
|
4864
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4865
|
+
zz = Rational(2240, 1667).to_ld(1, ROUND_UP)
|
3224
4866
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3225
4867
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3226
4868
|
# y is has no scale, use scale of x
|
3227
|
-
z = y.divide_s(x, 1,
|
3228
|
-
zz = Rational(1667, 2240).to_ld(1,
|
4869
|
+
z = y.divide_s(x, 1, ROUND_DOWN)
|
4870
|
+
zz = Rational(1667, 2240).to_ld(1, ROUND_DOWN)
|
3229
4871
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3230
4872
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3231
|
-
z = y.divide_s(x, 1,
|
3232
|
-
zz = Rational(1667, 2240).to_ld(1,
|
4873
|
+
z = y.divide_s(x, 1, ROUND_UP)
|
4874
|
+
zz = Rational(1667, 2240).to_ld(1, ROUND_UP)
|
3233
4875
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3234
4876
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3235
4877
|
|
3236
4878
|
y = LongDecimalQuot(Rational(5, 3), 3).to_ld
|
3237
|
-
z = x.divide_s(y, 1,
|
3238
|
-
zz = Rational(2240, 1667).to_ld(1,
|
4879
|
+
z = x.divide_s(y, 1, ROUND_DOWN)
|
4880
|
+
zz = Rational(2240, 1667).to_ld(1, ROUND_DOWN)
|
3239
4881
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3240
4882
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3241
|
-
z = x.divide_s(y, 1,
|
3242
|
-
zz = Rational(2240, 1667).to_ld(1,
|
4883
|
+
z = x.divide_s(y, 1, ROUND_UP)
|
4884
|
+
zz = Rational(2240, 1667).to_ld(1, ROUND_UP)
|
3243
4885
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3244
4886
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3245
|
-
z = y.divide_s(x, 2,
|
3246
|
-
zz = Rational(1667, 2240).to_ld(2,
|
4887
|
+
z = y.divide_s(x, 2, ROUND_DOWN)
|
4888
|
+
zz = Rational(1667, 2240).to_ld(2, ROUND_DOWN)
|
3247
4889
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3248
4890
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3249
|
-
z = y.divide_s(x, 224,
|
3250
|
-
zz = Rational(1667, 2240).to_ld(224,
|
4891
|
+
z = y.divide_s(x, 224, ROUND_UP)
|
4892
|
+
zz = Rational(1667, 2240).to_ld(224, ROUND_UP)
|
3251
4893
|
assert_kind_of(LongDecimal, z, "z=#{z.inspect}")
|
3252
4894
|
assert_equal(zz, z, "z=#{z.inspect}")
|
3253
4895
|
end
|
@@ -3260,7 +4902,7 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3260
4902
|
x = LongDecimal(224, 2) # 2.24
|
3261
4903
|
|
3262
4904
|
y = Complex(5, 3)
|
3263
|
-
z = x.divide_s(y, 2,
|
4905
|
+
z = x.divide_s(y, 2, ROUND_DOWN)
|
3264
4906
|
zz = 2.24 / Complex(5, 3)
|
3265
4907
|
assert_kind_of(Complex, z, "z=#{z.inspect}")
|
3266
4908
|
assert((zz-z).abs < 1e-9, "z=#{z.inspect}")
|
@@ -3848,79 +5490,79 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3848
5490
|
def test_sqrt
|
3849
5491
|
print "\ntest_sqrt [#{Time.now}]: "
|
3850
5492
|
x = LongDecimal.zero!(101)
|
3851
|
-
y = check_sqrt(x, 120,
|
5493
|
+
y = check_sqrt(x, 120, ROUND_UNNECESSARY, 0, 0, "zero")
|
3852
5494
|
assert(y.zero?, "sqrt(0)")
|
3853
5495
|
|
3854
5496
|
x = LongDecimal.one!(101)
|
3855
|
-
y = check_sqrt(x, 120,
|
5497
|
+
y = check_sqrt(x, 120, ROUND_UNNECESSARY, 0, 0, "one")
|
3856
5498
|
assert(y.one?, "sqrt(1)")
|
3857
5499
|
|
3858
5500
|
x = LongDecimal.two!(101)
|
3859
|
-
y0 = check_sqrt(x, 120,
|
5501
|
+
y0 = check_sqrt(x, 120, ROUND_DOWN, 0, 1, "two")
|
3860
5502
|
assert(y0.square < x, "y0*y0")
|
3861
5503
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3862
|
-
y1 = check_sqrt(x, 120,
|
3863
|
-
y2 = check_sqrt(x, 120,
|
5504
|
+
y1 = check_sqrt(x, 120, ROUND_HALF_EVEN, -1, 1, "two")
|
5505
|
+
y2 = check_sqrt(x, 120, ROUND_UP, -1, 0, "two")
|
3864
5506
|
assert(y2.pred.square < x, "y2.pred.square")
|
3865
5507
|
assert(y2.square > x, "y2*y2")
|
3866
5508
|
assert(y0 <= y1, "y0 y1")
|
3867
5509
|
assert(y1 <= y2, "y1 y2")
|
3868
5510
|
|
3869
|
-
y0 = check_sqrt(x, 140,
|
5511
|
+
y0 = check_sqrt(x, 140, ROUND_DOWN, 0, 1, "two")
|
3870
5512
|
assert(y0.square < x, "y0*y0")
|
3871
5513
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3872
|
-
y1 = check_sqrt(x, 140,
|
3873
|
-
y2 = check_sqrt(x, 140,
|
5514
|
+
y1 = check_sqrt(x, 140, ROUND_HALF_EVEN, -1, 1, "two")
|
5515
|
+
y2 = check_sqrt(x, 140, ROUND_UP, -1, 0, "two")
|
3874
5516
|
assert(y2.pred.square < x, "y2.pred.square")
|
3875
5517
|
assert(y2.square > x, "y2*y2")
|
3876
5518
|
assert(y0 <= y1, "y0 y1")
|
3877
5519
|
assert(y1 <= y2, "y1 y2")
|
3878
5520
|
|
3879
|
-
y0 = check_sqrt(x, 160,
|
5521
|
+
y0 = check_sqrt(x, 160, ROUND_DOWN, 0, 1, "two")
|
3880
5522
|
assert(y0.square < x, "y0*y0")
|
3881
5523
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3882
|
-
y1 = check_sqrt(x, 160,
|
3883
|
-
y2 = check_sqrt(x, 160,
|
5524
|
+
y1 = check_sqrt(x, 160, ROUND_HALF_EVEN, -1, 1, "two")
|
5525
|
+
y2 = check_sqrt(x, 160, ROUND_UP, -1, 0, "two")
|
3884
5526
|
assert(y2.pred.square < x, "y2.pred.square")
|
3885
5527
|
assert(y2.square > x, "y2*y2")
|
3886
5528
|
assert(y0 <= y1, "y0 y1")
|
3887
5529
|
assert(y1 <= y2, "y1 y2")
|
3888
5530
|
|
3889
|
-
y0 = check_sqrt(x, 120,
|
5531
|
+
y0 = check_sqrt(x, 120, ROUND_DOWN, 0, 1, "two")
|
3890
5532
|
assert(y0.square < x, "y0*y0")
|
3891
5533
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3892
|
-
y1 = check_sqrt(x, 120,
|
3893
|
-
y2 = check_sqrt(x, 120,
|
5534
|
+
y1 = check_sqrt(x, 120, ROUND_HALF_EVEN, -1, 1, "two")
|
5535
|
+
y2 = check_sqrt(x, 120, ROUND_UP, -1, 0, "two")
|
3894
5536
|
assert(y2.pred.square < x, "y2.pred.square")
|
3895
5537
|
assert(y2.square > x, "y2*y2")
|
3896
5538
|
assert(y0 <= y1, "y0 y1")
|
3897
5539
|
assert(y1 <= y2, "y1 y2")
|
3898
5540
|
|
3899
|
-
y0 = check_sqrt(x, 100,
|
5541
|
+
y0 = check_sqrt(x, 100, ROUND_DOWN, 0, 1, "two")
|
3900
5542
|
assert(y0.square < x, "y0*y0")
|
3901
5543
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3902
|
-
y1 = check_sqrt(x, 100,
|
3903
|
-
y2 = check_sqrt(x, 100,
|
5544
|
+
y1 = check_sqrt(x, 100, ROUND_HALF_EVEN, -1, 1, "two")
|
5545
|
+
y2 = check_sqrt(x, 100, ROUND_UP, -1, 0, "two")
|
3904
5546
|
assert(y2.pred.square < x, "y2.pred.square")
|
3905
5547
|
assert(y2.square > x, "y2*y2")
|
3906
5548
|
assert(y0 <= y1, "y0 y1")
|
3907
5549
|
assert(y1 <= y2, "y1 y2")
|
3908
5550
|
|
3909
5551
|
x = 3.to_ld
|
3910
|
-
y0 = check_sqrt(x, 120,
|
5552
|
+
y0 = check_sqrt(x, 120, ROUND_DOWN, 0, 1, "three")
|
3911
5553
|
assert(y0.square < x, "y0*y0")
|
3912
5554
|
assert(y0.succ.square > x, "(y0.succ).square")
|
3913
|
-
y1 = check_sqrt(x, 120,
|
3914
|
-
y2 = check_sqrt(x, 120,
|
5555
|
+
y1 = check_sqrt(x, 120, ROUND_HALF_EVEN, -1, 1, "three")
|
5556
|
+
y2 = check_sqrt(x, 120, ROUND_UP, -1, 0, "three")
|
3915
5557
|
assert(y2.pred.square < x, "y2.pred.square")
|
3916
5558
|
assert(y2.square > x, "y2*y2")
|
3917
5559
|
assert(y0 <= y1, "y0 y1")
|
3918
5560
|
assert(y1 <= y2, "y1 y2")
|
3919
5561
|
|
3920
5562
|
x = 4.to_ld(101)
|
3921
|
-
y0 = check_sqrt(x, 120,
|
3922
|
-
y1 = check_sqrt(x, 120,
|
3923
|
-
y2 = check_sqrt(x, 120,
|
5563
|
+
y0 = check_sqrt(x, 120, ROUND_DOWN, 0, 0, "four")
|
5564
|
+
y1 = check_sqrt(x, 120, ROUND_HALF_EVEN, 0, 0, "four")
|
5565
|
+
y2 = check_sqrt(x, 120, ROUND_UP, 0, 0, "four")
|
3924
5566
|
assert_equal(y0, y1, "y0 y1")
|
3925
5567
|
assert_equal(y1, y2, "y1 y2")
|
3926
5568
|
end
|
@@ -3964,21 +5606,21 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3964
5606
|
def test_non_ld_sqrt
|
3965
5607
|
print "\ntest_non_ld_sqrt [#{Time.now}]: "
|
3966
5608
|
xi = 77
|
3967
|
-
yi = LongMath.sqrt(xi, 31,
|
3968
|
-
zi = yi.square.round_to_scale(30,
|
5609
|
+
yi = LongMath.sqrt(xi, 31, ROUND_HALF_EVEN)
|
5610
|
+
zi = yi.square.round_to_scale(30, ROUND_HALF_EVEN)
|
3969
5611
|
assert(zi.is_int?, "zi=#{zi.to_s}")
|
3970
5612
|
assert_equal(xi, zi.to_i, "zi")
|
3971
5613
|
|
3972
5614
|
xf = 77.0
|
3973
|
-
yf = LongMath.sqrt(xf, 31,
|
3974
|
-
zf = yf.square.round_to_scale(30,
|
5615
|
+
yf = LongMath.sqrt(xf, 31, ROUND_HALF_EVEN)
|
5616
|
+
zf = yf.square.round_to_scale(30, ROUND_HALF_EVEN)
|
3975
5617
|
assert(zf.is_int?, "zf")
|
3976
5618
|
assert_equal(xf, zf.to_f, "zf")
|
3977
5619
|
assert_equal(yi, yf, "i-f")
|
3978
5620
|
|
3979
5621
|
xr = Rational(224, 227)
|
3980
|
-
yr = LongMath.sqrt(xr, 31,
|
3981
|
-
zr = yr.square.round_to_scale(30,
|
5622
|
+
yr = LongMath.sqrt(xr, 31, ROUND_HALF_EVEN)
|
5623
|
+
zr = yr.square.round_to_scale(30, ROUND_HALF_EVEN)
|
3982
5624
|
assert((zr-xr).abs <= zr.unit, "zr-xr")
|
3983
5625
|
end
|
3984
5626
|
|
@@ -3988,79 +5630,79 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
3988
5630
|
def test_cbrt
|
3989
5631
|
print "\ntest_cbrt [#{Time.now}]: "
|
3990
5632
|
x = LongDecimal.zero!(101)
|
3991
|
-
y = check_cbrt(x, 120,
|
5633
|
+
y = check_cbrt(x, 120, ROUND_UNNECESSARY, 0, 0, "zero")
|
3992
5634
|
assert(y.zero?, "cbrt(0)")
|
3993
5635
|
|
3994
5636
|
x = LongDecimal.one!(101)
|
3995
|
-
y = check_cbrt(x, 120,
|
5637
|
+
y = check_cbrt(x, 120, ROUND_UNNECESSARY, 0, 0, "one")
|
3996
5638
|
assert(y.one?, "cbrt(1)")
|
3997
5639
|
|
3998
5640
|
x = LongDecimal.two!(101)
|
3999
|
-
y0 = check_cbrt(x, 120,
|
5641
|
+
y0 = check_cbrt(x, 120, ROUND_DOWN, 0, 1, "two")
|
4000
5642
|
assert(y0.cube < x, "y0**3")
|
4001
5643
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4002
|
-
y1 = check_cbrt(x, 120,
|
4003
|
-
y2 = check_cbrt(x, 120,
|
5644
|
+
y1 = check_cbrt(x, 120, ROUND_HALF_EVEN, -1, 1, "two")
|
5645
|
+
y2 = check_cbrt(x, 120, ROUND_UP, -1, 0, "two")
|
4004
5646
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4005
5647
|
assert(y2.cube > x, "y2**3")
|
4006
5648
|
assert(y0 <= y1, "y0 y1")
|
4007
5649
|
assert(y1 <= y2, "y1 y2")
|
4008
5650
|
|
4009
|
-
y0 = check_cbrt(x, 140,
|
5651
|
+
y0 = check_cbrt(x, 140, ROUND_DOWN, 0, 1, "two")
|
4010
5652
|
assert(y0.cube < x, "y0**3")
|
4011
5653
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4012
|
-
y1 = check_cbrt(x, 140,
|
4013
|
-
y2 = check_cbrt(x, 140,
|
5654
|
+
y1 = check_cbrt(x, 140, ROUND_HALF_EVEN, -1, 1, "two")
|
5655
|
+
y2 = check_cbrt(x, 140, ROUND_UP, -1, 0, "two")
|
4014
5656
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4015
5657
|
assert(y2.cube > x, "y2**3")
|
4016
5658
|
assert(y0 <= y1, "y0 y1")
|
4017
5659
|
assert(y1 <= y2, "y1 y2")
|
4018
5660
|
|
4019
|
-
y0 = check_cbrt(x, 160,
|
5661
|
+
y0 = check_cbrt(x, 160, ROUND_DOWN, 0, 1, "two")
|
4020
5662
|
assert(y0.cube < x, "y0**3")
|
4021
5663
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4022
|
-
y1 = check_cbrt(x, 160,
|
4023
|
-
y2 = check_cbrt(x, 160,
|
5664
|
+
y1 = check_cbrt(x, 160, ROUND_HALF_EVEN, -1, 1, "two")
|
5665
|
+
y2 = check_cbrt(x, 160, ROUND_UP, -1, 0, "two")
|
4024
5666
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4025
5667
|
assert(y2.cube > x, "y2**3")
|
4026
5668
|
assert(y0 <= y1, "y0 y1")
|
4027
5669
|
assert(y1 <= y2, "y1 y2")
|
4028
5670
|
|
4029
|
-
y0 = check_cbrt(x, 120,
|
5671
|
+
y0 = check_cbrt(x, 120, ROUND_DOWN, 0, 1, "two")
|
4030
5672
|
assert(y0.cube < x, "y0**3")
|
4031
5673
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4032
|
-
y1 = check_cbrt(x, 120,
|
4033
|
-
y2 = check_cbrt(x, 120,
|
5674
|
+
y1 = check_cbrt(x, 120, ROUND_HALF_EVEN, -1, 1, "two")
|
5675
|
+
y2 = check_cbrt(x, 120, ROUND_UP, -1, 0, "two")
|
4034
5676
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4035
5677
|
assert(y2.cube > x, "y2**3")
|
4036
5678
|
assert(y0 <= y1, "y0 y1")
|
4037
5679
|
assert(y1 <= y2, "y1 y2")
|
4038
5680
|
|
4039
|
-
y0 = check_cbrt(x, 100,
|
5681
|
+
y0 = check_cbrt(x, 100, ROUND_DOWN, 0, 1, "two")
|
4040
5682
|
assert(y0.cube < x, "y0**3")
|
4041
5683
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4042
|
-
y1 = check_cbrt(x, 100,
|
4043
|
-
y2 = check_cbrt(x, 100,
|
5684
|
+
y1 = check_cbrt(x, 100, ROUND_HALF_EVEN, -1, 1, "two")
|
5685
|
+
y2 = check_cbrt(x, 100, ROUND_UP, -1, 0, "two")
|
4044
5686
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4045
5687
|
assert(y2.cube > x, "y2**3")
|
4046
5688
|
assert(y0 <= y1, "y0 y1")
|
4047
5689
|
assert(y1 <= y2, "y1 y2")
|
4048
5690
|
|
4049
5691
|
x = 3.to_ld
|
4050
|
-
y0 = check_cbrt(x, 120,
|
5692
|
+
y0 = check_cbrt(x, 120, ROUND_DOWN, 0, 1, "three")
|
4051
5693
|
assert(y0.cube < x, "y0**3")
|
4052
5694
|
assert(y0.succ.cube > x, "(y0.succ).cube")
|
4053
|
-
y1 = check_cbrt(x, 120,
|
4054
|
-
y2 = check_cbrt(x, 120,
|
5695
|
+
y1 = check_cbrt(x, 120, ROUND_HALF_EVEN, -1, 1, "three")
|
5696
|
+
y2 = check_cbrt(x, 120, ROUND_UP, -1, 0, "three")
|
4055
5697
|
assert(y2.pred.cube < x, "y2.pred.cube")
|
4056
5698
|
assert(y2.cube > x, "y2**3")
|
4057
5699
|
assert(y0 <= y1, "y0 y1")
|
4058
5700
|
assert(y1 <= y2, "y1 y2")
|
4059
5701
|
|
4060
5702
|
x = 8.to_ld(101)
|
4061
|
-
y0 = check_cbrt(x, 120,
|
4062
|
-
y1 = check_cbrt(x, 120,
|
4063
|
-
y2 = check_cbrt(x, 120,
|
5703
|
+
y0 = check_cbrt(x, 120, ROUND_DOWN, 0, 0, "eight")
|
5704
|
+
y1 = check_cbrt(x, 120, ROUND_HALF_EVEN, 0, 0, "eight")
|
5705
|
+
y2 = check_cbrt(x, 120, ROUND_UP, 0, 0, "eight")
|
4064
5706
|
assert_equal(y0, y1, "y0 y1")
|
4065
5707
|
assert_equal(y1, y2, "y1 y2")
|
4066
5708
|
end
|
@@ -4104,21 +5746,21 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4104
5746
|
def test_non_ld_cbrt
|
4105
5747
|
print "\ntest_non_ld_cbrt [#{Time.now}]: "
|
4106
5748
|
xi = 77
|
4107
|
-
yi = LongMath.cbrt(xi, 32,
|
4108
|
-
zi = yi.cube.round_to_scale(30,
|
5749
|
+
yi = LongMath.cbrt(xi, 32, ROUND_HALF_EVEN)
|
5750
|
+
zi = yi.cube.round_to_scale(30, ROUND_HALF_EVEN)
|
4109
5751
|
assert(zi.is_int?, "zi=#{zi.to_s}")
|
4110
5752
|
assert_equal(xi, zi.to_i, "zi")
|
4111
5753
|
|
4112
5754
|
xf = 77.0
|
4113
|
-
yf = LongMath.cbrt(xf, 32,
|
4114
|
-
zf = yf.cube.round_to_scale(30,
|
5755
|
+
yf = LongMath.cbrt(xf, 32, ROUND_HALF_EVEN)
|
5756
|
+
zf = yf.cube.round_to_scale(30, ROUND_HALF_EVEN)
|
4115
5757
|
assert(zf.is_int?, "zf")
|
4116
5758
|
assert_equal(xf, zf.to_f, "zf")
|
4117
5759
|
assert_equal(yi, yf, "i-f")
|
4118
5760
|
|
4119
5761
|
xr = Rational(224, 227)
|
4120
|
-
yr = LongMath.cbrt(xr, 32,
|
4121
|
-
zr = yr.cube.round_to_scale(30,
|
5762
|
+
yr = LongMath.cbrt(xr, 32, ROUND_HALF_EVEN)
|
5763
|
+
zr = yr.cube.round_to_scale(30, ROUND_HALF_EVEN)
|
4122
5764
|
assert((zr-xr).abs <= zr.unit, "zr-xr")
|
4123
5765
|
end
|
4124
5766
|
|
@@ -4461,31 +6103,31 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4461
6103
|
# 0.99555555555555...
|
4462
6104
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4463
6105
|
# 1.0
|
4464
|
-
r = l.round_to_scale(1,
|
6106
|
+
r = l.round_to_scale(1, ROUND_UP)
|
4465
6107
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4466
6108
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4467
6109
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4468
6110
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4469
|
-
r = l.round_to_scale(1,
|
6111
|
+
r = l.round_to_scale(1, ROUND_UP)
|
4470
6112
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4471
6113
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4472
6114
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4473
6115
|
# 0.00444444444444444
|
4474
6116
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4475
|
-
r = l.round_to_scale(1,
|
6117
|
+
r = l.round_to_scale(1, ROUND_UP)
|
4476
6118
|
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4477
6119
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4478
6120
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4479
6121
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4480
|
-
r = l.round_to_scale(1,
|
6122
|
+
r = l.round_to_scale(1, ROUND_UP)
|
4481
6123
|
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4482
6124
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4483
6125
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4484
6126
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4485
|
-
r = l.round_to_scale(4,
|
6127
|
+
r = l.round_to_scale(4, ROUND_UP)
|
4486
6128
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4487
6129
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4488
|
-
r = l.round_to_scale(4,
|
6130
|
+
r = l.round_to_scale(4, ROUND_UP)
|
4489
6131
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4490
6132
|
end
|
4491
6133
|
|
@@ -4498,31 +6140,31 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4498
6140
|
# 0.99555555555555...
|
4499
6141
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4500
6142
|
# 0.9
|
4501
|
-
r = l.round_to_scale(1,
|
6143
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
4502
6144
|
assert_equal("0.9", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4503
6145
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4504
6146
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4505
6147
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4506
|
-
r = l.round_to_scale(1,
|
6148
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
4507
6149
|
assert_equal("-0.9", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4508
6150
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4509
6151
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4510
6152
|
# 0.00444444444444444
|
4511
6153
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4512
|
-
r = l.round_to_scale(1,
|
6154
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
4513
6155
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4514
6156
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4515
6157
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4516
6158
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4517
|
-
r = l.round_to_scale(1,
|
6159
|
+
r = l.round_to_scale(1, ROUND_DOWN)
|
4518
6160
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4519
6161
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4520
6162
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4521
6163
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4522
|
-
r = l.round_to_scale(4,
|
6164
|
+
r = l.round_to_scale(4, ROUND_DOWN)
|
4523
6165
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4524
6166
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4525
|
-
r = l.round_to_scale(4,
|
6167
|
+
r = l.round_to_scale(4, ROUND_DOWN)
|
4526
6168
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4527
6169
|
end
|
4528
6170
|
|
@@ -4534,31 +6176,31 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4534
6176
|
|
4535
6177
|
# 0.99555555555555...
|
4536
6178
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4537
|
-
r = l.round_to_scale(1,
|
6179
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
4538
6180
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4539
6181
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4540
6182
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4541
6183
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4542
|
-
r = l.round_to_scale(1,
|
6184
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
4543
6185
|
assert_equal("-0.9", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4544
6186
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4545
6187
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4546
6188
|
# 0.00444444444444444
|
4547
6189
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4548
|
-
r = l.round_to_scale(1,
|
6190
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
4549
6191
|
assert_equal("0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4550
6192
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4551
6193
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4552
6194
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4553
|
-
r = l.round_to_scale(1,
|
6195
|
+
r = l.round_to_scale(1, ROUND_CEILING)
|
4554
6196
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4555
6197
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4556
6198
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4557
6199
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4558
|
-
r = l.round_to_scale(4,
|
6200
|
+
r = l.round_to_scale(4, ROUND_CEILING)
|
4559
6201
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4560
6202
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4561
|
-
r = l.round_to_scale(4,
|
6203
|
+
r = l.round_to_scale(4, ROUND_CEILING)
|
4562
6204
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4563
6205
|
end
|
4564
6206
|
|
@@ -4570,31 +6212,31 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4570
6212
|
|
4571
6213
|
# 0.99555555555555...
|
4572
6214
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4573
|
-
r = l.round_to_scale(1,
|
6215
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
4574
6216
|
assert_equal("0.9", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4575
6217
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4576
6218
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4577
6219
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4578
|
-
r = l.round_to_scale(1,
|
6220
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
4579
6221
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4580
6222
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4581
6223
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4582
6224
|
# 0.00444444444444444
|
4583
6225
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4584
|
-
r = l.round_to_scale(1,
|
6226
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
4585
6227
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4586
6228
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4587
6229
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4588
6230
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4589
|
-
r = l.round_to_scale(1,
|
6231
|
+
r = l.round_to_scale(1, ROUND_FLOOR)
|
4590
6232
|
assert_equal("-0.1", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4591
6233
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4592
6234
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4593
6235
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4594
|
-
r = l.round_to_scale(4,
|
6236
|
+
r = l.round_to_scale(4, ROUND_FLOOR)
|
4595
6237
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4596
6238
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4597
|
-
r = l.round_to_scale(4,
|
6239
|
+
r = l.round_to_scale(4, ROUND_FLOOR)
|
4598
6240
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4599
6241
|
end
|
4600
6242
|
|
@@ -4606,40 +6248,40 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4606
6248
|
|
4607
6249
|
# 0.99555555555555...
|
4608
6250
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4609
|
-
r = l.round_to_scale(1,
|
6251
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4610
6252
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4611
6253
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4612
6254
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4613
6255
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4614
|
-
r = l.round_to_scale(1,
|
6256
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4615
6257
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4616
6258
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4617
6259
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4618
6260
|
# 0.00444444444444444
|
4619
6261
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4620
|
-
r = l.round_to_scale(1,
|
6262
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4621
6263
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4622
6264
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4623
6265
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4624
6266
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4625
|
-
r = l.round_to_scale(1,
|
6267
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4626
6268
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4627
6269
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4628
6270
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4629
6271
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4630
|
-
r = l.round_to_scale(4,
|
6272
|
+
r = l.round_to_scale(4, ROUND_HALF_UP)
|
4631
6273
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4632
6274
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4633
|
-
r = l.round_to_scale(4,
|
6275
|
+
r = l.round_to_scale(4, ROUND_HALF_UP)
|
4634
6276
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4635
6277
|
# 56.25
|
4636
6278
|
l = LongDecimalQuot(Rational(225, 4), 0)
|
4637
|
-
r = l.round_to_scale(1,
|
6279
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4638
6280
|
assert_equal("56.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4639
6281
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4640
6282
|
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4641
6283
|
l = LongDecimalQuot(Rational(-225, 4), 0)
|
4642
|
-
r = l.round_to_scale(1,
|
6284
|
+
r = l.round_to_scale(1, ROUND_HALF_UP)
|
4643
6285
|
assert_equal("-56.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4644
6286
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4645
6287
|
assert_equal("-225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
@@ -4653,40 +6295,40 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4653
6295
|
|
4654
6296
|
# 0.99555555555555...
|
4655
6297
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4656
|
-
r = l.round_to_scale(1,
|
6298
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4657
6299
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4658
6300
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4659
6301
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4660
6302
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4661
|
-
r = l.round_to_scale(1,
|
6303
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4662
6304
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4663
6305
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4664
6306
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4665
6307
|
# 0.00444444444444444
|
4666
6308
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4667
|
-
r = l.round_to_scale(1,
|
6309
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4668
6310
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4669
6311
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4670
6312
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4671
6313
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4672
|
-
r = l.round_to_scale(1,
|
6314
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4673
6315
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4674
6316
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4675
6317
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4676
6318
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4677
|
-
r = l.round_to_scale(4,
|
6319
|
+
r = l.round_to_scale(4, ROUND_HALF_DOWN)
|
4678
6320
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4679
6321
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4680
|
-
r = l.round_to_scale(4,
|
6322
|
+
r = l.round_to_scale(4, ROUND_HALF_DOWN)
|
4681
6323
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4682
6324
|
# 56.25
|
4683
6325
|
l = LongDecimalQuot(Rational(225, 4), 0)
|
4684
|
-
r = l.round_to_scale(1,
|
6326
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4685
6327
|
assert_equal("56.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4686
6328
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4687
6329
|
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4688
6330
|
l = LongDecimalQuot(Rational(-225, 4), 0)
|
4689
|
-
r = l.round_to_scale(1,
|
6331
|
+
r = l.round_to_scale(1, ROUND_HALF_DOWN)
|
4690
6332
|
assert_equal("-56.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4691
6333
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4692
6334
|
assert_equal("-225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
@@ -4700,40 +6342,40 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4700
6342
|
|
4701
6343
|
# 0.99555555555555...
|
4702
6344
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4703
|
-
r = l.round_to_scale(1,
|
6345
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4704
6346
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4705
6347
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4706
6348
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4707
6349
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4708
|
-
r = l.round_to_scale(1,
|
6350
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4709
6351
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4710
6352
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4711
6353
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4712
6354
|
# 0.00444444444444444
|
4713
6355
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4714
|
-
r = l.round_to_scale(1,
|
6356
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4715
6357
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4716
6358
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4717
6359
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4718
6360
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4719
|
-
r = l.round_to_scale(1,
|
6361
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4720
6362
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4721
6363
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4722
6364
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4723
6365
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4724
|
-
r = l.round_to_scale(4,
|
6366
|
+
r = l.round_to_scale(4, ROUND_HALF_CEILING)
|
4725
6367
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4726
6368
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4727
|
-
r = l.round_to_scale(4,
|
6369
|
+
r = l.round_to_scale(4, ROUND_HALF_CEILING)
|
4728
6370
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4729
6371
|
# 56.25
|
4730
6372
|
l = LongDecimalQuot(Rational(225, 4), 0)
|
4731
|
-
r = l.round_to_scale(1,
|
6373
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4732
6374
|
assert_equal("56.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4733
6375
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4734
6376
|
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4735
6377
|
l = LongDecimalQuot(Rational(-225, 4), 0)
|
4736
|
-
r = l.round_to_scale(1,
|
6378
|
+
r = l.round_to_scale(1, ROUND_HALF_CEILING)
|
4737
6379
|
assert_equal("-56.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4738
6380
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4739
6381
|
assert_equal("-225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
@@ -4747,40 +6389,40 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4747
6389
|
|
4748
6390
|
# 0.99555555555555...
|
4749
6391
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4750
|
-
r = l.round_to_scale(1,
|
6392
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4751
6393
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4752
6394
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4753
6395
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4754
6396
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4755
|
-
r = l.round_to_scale(1,
|
6397
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4756
6398
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4757
6399
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4758
6400
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4759
6401
|
# 0.00444444444444444
|
4760
6402
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4761
|
-
r = l.round_to_scale(1,
|
6403
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4762
6404
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4763
6405
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4764
6406
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4765
6407
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4766
|
-
r = l.round_to_scale(1,
|
6408
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4767
6409
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4768
6410
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4769
6411
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4770
6412
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4771
|
-
r = l.round_to_scale(4,
|
6413
|
+
r = l.round_to_scale(4, ROUND_HALF_FLOOR)
|
4772
6414
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4773
6415
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4774
|
-
r = l.round_to_scale(4,
|
6416
|
+
r = l.round_to_scale(4, ROUND_HALF_FLOOR)
|
4775
6417
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4776
6418
|
# 56.25
|
4777
6419
|
l = LongDecimalQuot(Rational(225, 4), 0)
|
4778
|
-
r = l.round_to_scale(1,
|
6420
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4779
6421
|
assert_equal("56.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4780
6422
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4781
6423
|
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4782
6424
|
l = LongDecimalQuot(Rational(-225, 4), 0)
|
4783
|
-
r = l.round_to_scale(1,
|
6425
|
+
r = l.round_to_scale(1, ROUND_HALF_FLOOR)
|
4784
6426
|
assert_equal("-56.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4785
6427
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4786
6428
|
assert_equal("-225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
@@ -4794,56 +6436,112 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4794
6436
|
|
4795
6437
|
# 0.99555555555555...
|
4796
6438
|
l = LongDecimalQuot(Rational(224, 225), 0)
|
4797
|
-
r = l.round_to_scale(1,
|
6439
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4798
6440
|
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4799
6441
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4800
6442
|
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4801
6443
|
l = LongDecimalQuot(-Rational(224, 225), 0)
|
4802
|
-
r = l.round_to_scale(1,
|
6444
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4803
6445
|
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4804
6446
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4805
6447
|
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4806
6448
|
# 0.00444444444444444
|
4807
6449
|
l = LongDecimalQuot(Rational(1, 225), 0)
|
4808
|
-
r = l.round_to_scale(1,
|
6450
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4809
6451
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4810
6452
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4811
6453
|
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4812
6454
|
l = LongDecimalQuot(Rational(-1, 225), 0)
|
4813
|
-
r = l.round_to_scale(1,
|
6455
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4814
6456
|
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4815
6457
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4816
6458
|
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4817
6459
|
l = LongDecimalQuot(Rational(1, 1), 1000)
|
4818
|
-
r = l.round_to_scale(4,
|
6460
|
+
r = l.round_to_scale(4, ROUND_HALF_EVEN)
|
4819
6461
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4820
6462
|
l = LongDecimalQuot(Rational(1, 1), 1)
|
4821
|
-
r = l.round_to_scale(4,
|
6463
|
+
r = l.round_to_scale(4, ROUND_HALF_EVEN)
|
4822
6464
|
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4823
6465
|
# 56.25
|
4824
6466
|
l = LongDecimalQuot(Rational(225, 4), 0)
|
4825
|
-
r = l.round_to_scale(1,
|
6467
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4826
6468
|
assert_equal("56.2", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4827
6469
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4828
6470
|
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4829
6471
|
# 56.75
|
4830
6472
|
l = LongDecimalQuot(Rational(227, 4), 0)
|
4831
|
-
r = l.round_to_scale(1,
|
6473
|
+
r = l.round_to_scale(1, ROUND_HALF_EVEN)
|
4832
6474
|
assert_equal("56.8", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4833
6475
|
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
4834
6476
|
assert_equal("227/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4835
6477
|
end
|
4836
6478
|
|
6479
|
+
#
|
6480
|
+
# test rounding of LongDecimalQuot with ROUND_HALF_ODD
|
6481
|
+
#
|
6482
|
+
def test_ldq_round_to_scale_half_odd
|
6483
|
+
print "\ntest_ldq_round_to_scale_half_odd [#{Time.now}]: "
|
6484
|
+
|
6485
|
+
# 0.99555555555555...
|
6486
|
+
l = LongDecimalQuot(Rational(224, 225), 0)
|
6487
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6488
|
+
assert_equal("1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6489
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6490
|
+
assert_equal("224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6491
|
+
l = LongDecimalQuot(-Rational(224, 225), 0)
|
6492
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6493
|
+
assert_equal("-1.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6494
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6495
|
+
assert_equal("-224/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6496
|
+
# 0.00444444444444444
|
6497
|
+
l = LongDecimalQuot(Rational(1, 225), 0)
|
6498
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6499
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6500
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6501
|
+
assert_equal("1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6502
|
+
l = LongDecimalQuot(Rational(-1, 225), 0)
|
6503
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6504
|
+
assert_equal("0.0", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6505
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6506
|
+
assert_equal("-1/225[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6507
|
+
l = LongDecimalQuot(Rational(1, 1), 1000)
|
6508
|
+
r = l.round_to_scale(4, ROUND_HALF_ODD)
|
6509
|
+
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6510
|
+
l = LongDecimalQuot(Rational(1, 1), 1)
|
6511
|
+
r = l.round_to_scale(4, ROUND_HALF_ODD)
|
6512
|
+
assert_equal("1.0000", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6513
|
+
# 56.25
|
6514
|
+
l = LongDecimalQuot(Rational(225, 4), 0)
|
6515
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6516
|
+
assert_equal("56.3", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6517
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6518
|
+
assert_equal("225/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6519
|
+
# 56.75
|
6520
|
+
l = LongDecimalQuot(Rational(227, 4), 0)
|
6521
|
+
r = l.round_to_scale(1, ROUND_HALF_ODD)
|
6522
|
+
assert_equal("56.7", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6523
|
+
assert_kind_of(LongDecimal, r, "must be LongDecimal")
|
6524
|
+
assert_equal("227/4[0]", l.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
6525
|
+
end
|
6526
|
+
|
6527
|
+
# TODO: do tests with rational numbers that approach square and cube roots with continuos fractions
|
6528
|
+
# TODO: do tests for harmonic that are exact matches
|
6529
|
+
# def test_ldq_round_to_scale_geometric_common
|
6530
|
+
# def test_ldq_round_to_scale_harmonic_common
|
6531
|
+
# def test_ldq_round_to_scale_harmonic_*
|
6532
|
+
# def test_ldq_round_to_scale_quadratic_common
|
6533
|
+
# def test_ldq_round_to_scale_cubic_common
|
6534
|
+
|
4837
6535
|
#
|
4838
6536
|
# test rounding of LongDecimalQuot with ROUND_UNNECESSARY
|
4839
6537
|
#
|
4840
6538
|
def test_ldq_round_to_scale_unnecessary
|
4841
6539
|
print "\ntest_ldq_round_to_scale_unnecessary [#{Time.now}]: "
|
4842
6540
|
l = LongDecimalQuot(Rational(225, 4), 5)
|
4843
|
-
r = l.round_to_scale(2,
|
6541
|
+
r = l.round_to_scale(2, ROUND_UNNECESSARY)
|
4844
6542
|
assert_equal("56.25", r.to_s, "l=#{l.inspect} r=#{r.inspect}")
|
4845
6543
|
begin
|
4846
|
-
r = l.round_to_scale(1,
|
6544
|
+
r = l.round_to_scale(1, ROUND_UNNECESSARY)
|
4847
6545
|
assert_fail("should not have succeeded l=#{l.inspect} r=#{r.inspect}")
|
4848
6546
|
rescue ArgumentError
|
4849
6547
|
# ignored
|
@@ -4979,7 +6677,7 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
4979
6677
|
y = x.to_ld
|
4980
6678
|
assert_kind_of(LongDecimal, y, "must be ld")
|
4981
6679
|
assert_equal(10, y.scale, "scale is 10")
|
4982
|
-
assert_equal(LongDecimal("1.5555555556"), y, "1.55...")
|
6680
|
+
assert_equal(LongDecimal("1.5555555556"), y, "1.55... y=#{y} x=#{x}")
|
4983
6681
|
x = LongDecimalQuot(Rational(7, 20), 1)
|
4984
6682
|
y = x.to_ld
|
4985
6683
|
assert_kind_of(LongDecimal, y, "must be ld")
|
@@ -5588,21 +7286,173 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
5588
7286
|
assert(! (y.eql? x), "! #{y.inspect} eql? #{x.inspect}")
|
5589
7287
|
end
|
5590
7288
|
|
7289
|
+
def test_means_one_param
|
7290
|
+
print "\ntest_means_one_param [#{Time.now}]: "
|
7291
|
+
[ -1, 0, 1, 7, -1.0, 0.0, 1.0, Math::PI, Rational(-1, 1), Rational(0, 1), Rational(1,1), Rational(2,3), LongDecimal(-10000000000, 10), LongDecimal(-1, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ].each do |x|
|
7292
|
+
# , Complex(-1, -2), Complex(0,0), Complex(Rational(3, 2), LongDecimal(4, 3))
|
7293
|
+
12.times do |prec|
|
7294
|
+
ALL_ROUNDING_MODES.each do |rm|
|
7295
|
+
if (rm == ROUND_UNNECESSARY)
|
7296
|
+
next
|
7297
|
+
end
|
7298
|
+
xx = x.to_ld(prec, rm)
|
7299
|
+
text = "x=#{x} xx=#{xx} rm=#{rm} prec=#{prec}"
|
7300
|
+
assert_equal(xx, LongMath.arithmetic_mean(prec, rm, x), text)
|
7301
|
+
assert_equal(xx, LongMath.geometric_mean(prec, rm, x), text)
|
7302
|
+
if (x.sgn != 0)
|
7303
|
+
assert_equal(xx, LongMath.harmonic_mean(prec, rm, x), text)
|
7304
|
+
end
|
7305
|
+
assert_equal(xx, LongMath.quadratic_mean(prec, rm, x), text)
|
7306
|
+
assert_equal(xx, LongMath.cubic_mean(prec, rm, x), text)
|
7307
|
+
end
|
7308
|
+
end
|
7309
|
+
end
|
7310
|
+
end
|
7311
|
+
|
7312
|
+
def test_means_two_param
|
7313
|
+
print "\ntest_means_two_param [#{Time.now}] (20 sec): "
|
7314
|
+
arr = [ 0, 1, 2, 7, 0.0, 1.0, 2.0, Math::PI, Rational(40, 9), Rational(0, 1), Rational(1,1), Rational(2,3), LongDecimal(3333333333333333, 10), LongDecimal(33, 10), LongDecimal(0, 10), LongDecimal(10000000000, 10), LongMath.pi(100) ]
|
7315
|
+
arr.each do |x|
|
7316
|
+
arr.each do |y|
|
7317
|
+
print "."
|
7318
|
+
12.times do |prec|
|
7319
|
+
ALL_ROUNDING_MODES.each do |rm|
|
7320
|
+
if (rm == ROUND_UNNECESSARY)
|
7321
|
+
next
|
7322
|
+
end
|
7323
|
+
xx = x.to_ld(prec, rm)
|
7324
|
+
yy = y.to_ld(prec, rm)
|
7325
|
+
mi = [xx, yy].min
|
7326
|
+
am = LongMath.arithmetic_mean(prec, rm, x, y)
|
7327
|
+
gm = LongMath.geometric_mean(prec, rm, x, y)
|
7328
|
+
if (x.sgn == 0 || y.sgn == 0)
|
7329
|
+
hm = gm
|
7330
|
+
else
|
7331
|
+
hm = LongMath.harmonic_mean(prec, rm, x, y)
|
7332
|
+
end
|
7333
|
+
qm = LongMath.quadratic_mean(prec, rm, x, y)
|
7334
|
+
cm = LongMath.cubic_mean(prec, rm, x, y)
|
7335
|
+
ma = [xx, yy].max
|
7336
|
+
text = "mi=#{mi} hm=#{hm} gm=#{gm} am=#{am} qm=#{qm} cm=#{cm} ma=#{ma} prec=#{prec} rm=#{rm} x=#{x} y=#{y}"
|
7337
|
+
assert(mi <= hm.succ, text)
|
7338
|
+
assert(hm <= gm.succ, text)
|
7339
|
+
assert(gm <= am.succ, text)
|
7340
|
+
assert(am <= qm.succ, text)
|
7341
|
+
assert(qm <= cm.succ, text)
|
7342
|
+
assert(cm <= ma.succ, text)
|
7343
|
+
if (x == y)
|
7344
|
+
assert_equal(mi, hm, text)
|
7345
|
+
assert_equal(hm, gm, text)
|
7346
|
+
assert_equal(gm, am, text)
|
7347
|
+
assert_equal(am, qm, text)
|
7348
|
+
assert_equal(qm, cm, text)
|
7349
|
+
assert_equal(cm, ma, text)
|
7350
|
+
end
|
7351
|
+
end
|
7352
|
+
end
|
7353
|
+
end
|
7354
|
+
end
|
7355
|
+
end
|
7356
|
+
|
7357
|
+
def test_means_three_param
|
7358
|
+
print "\ntest_means_three_param [#{Time.now}] (4 min): "
|
7359
|
+
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) ]
|
7360
|
+
arr.each do |x|
|
7361
|
+
arr.each do |y|
|
7362
|
+
print ":"
|
7363
|
+
arr.each do |z|
|
7364
|
+
print "."
|
7365
|
+
12.times do |prec|
|
7366
|
+
ALL_ROUNDING_MODES.each do |rm|
|
7367
|
+
if (rm == ROUND_UNNECESSARY)
|
7368
|
+
next
|
7369
|
+
end
|
7370
|
+
xx = x.to_ld(prec, rm)
|
7371
|
+
yy = y.to_ld(prec, rm)
|
7372
|
+
zz = z.to_ld(prec, rm)
|
7373
|
+
mi = [xx, yy, zz].min
|
7374
|
+
am = LongMath.arithmetic_mean(prec, rm, x, y, z)
|
7375
|
+
agm = LongMath.arithmetic_geometric_mean(prec, rm, x, y, z)
|
7376
|
+
gm = LongMath.geometric_mean(prec, rm, x, y, z)
|
7377
|
+
if (x.sgn == 0 || y.sgn == 0 || z.sgn == 0)
|
7378
|
+
hm = gm
|
7379
|
+
hgm = gm
|
7380
|
+
else
|
7381
|
+
hm = LongMath.harmonic_mean(prec, rm, x, y, z)
|
7382
|
+
hgm = LongMath.harmonic_geometric_mean(prec, rm, x, y, z)
|
7383
|
+
end
|
7384
|
+
qm = LongMath.quadratic_mean(prec, rm, x, y, z)
|
7385
|
+
cm = LongMath.cubic_mean(prec, rm, x, y, z)
|
7386
|
+
ma = [xx, yy, zz].max
|
7387
|
+
text = "mi=#{mi} hm=#{hm} gm=#{gm} am=#{am} qm=#{qm} cm=#{cm} ma=#{ma} prec=#{prec} rm=#{rm} x=#{x} y=#{y} z=#{z}"
|
7388
|
+
assert(mi <= hm.succ, text)
|
7389
|
+
assert(hm <= hgm.succ, text)
|
7390
|
+
assert(hgm <= gm.succ, text)
|
7391
|
+
assert(gm <= agm.succ, text)
|
7392
|
+
assert(agm <= am.succ, text)
|
7393
|
+
assert(am <= qm.succ, text)
|
7394
|
+
assert(qm <= cm.succ, text)
|
7395
|
+
assert(cm <= ma.succ, text)
|
7396
|
+
if (x == y && y == z)
|
7397
|
+
assert_equal(mi, hm, text)
|
7398
|
+
assert_equal(hm, hgm, text)
|
7399
|
+
assert_equal(hgm, gm, text)
|
7400
|
+
assert_equal(gm, agm, text)
|
7401
|
+
assert_equal(gm, agm, text)
|
7402
|
+
assert_equal(am, qm, text)
|
7403
|
+
assert_equal(qm, cm, text)
|
7404
|
+
assert_equal(cm, ma, text)
|
7405
|
+
end
|
7406
|
+
end
|
7407
|
+
end
|
7408
|
+
end
|
7409
|
+
end
|
7410
|
+
end
|
7411
|
+
end
|
7412
|
+
|
7413
|
+
def test_arithmetic_means_two_param_known_result
|
7414
|
+
assert_equal(1, LongMath.arithmetic_mean(0, ROUND_UNNECESSARY, 0, 2))
|
7415
|
+
assert_equal(0, LongMath.arithmetic_mean(0, ROUND_UNNECESSARY, -1, 1))
|
7416
|
+
assert_equal(3, LongMath.arithmetic_mean(0, ROUND_UNNECESSARY, 1, 5))
|
7417
|
+
end
|
7418
|
+
|
7419
|
+
def test_geometric_means_two_param_known_result
|
7420
|
+
assert_equal(2, LongMath.geometric_mean(0, ROUND_UNNECESSARY, 1, 4))
|
7421
|
+
assert_equal(10, LongMath.geometric_mean(0, ROUND_UNNECESSARY, 4, 25))
|
7422
|
+
assert_equal(-3, LongMath.geometric_mean(0, ROUND_UNNECESSARY, -1, -9))
|
7423
|
+
end
|
7424
|
+
|
7425
|
+
def test_harmonic_means_two_param_known_result
|
7426
|
+
assert_equal(3, LongMath.harmonic_mean(0, ROUND_UNNECESSARY, 2, 6))
|
7427
|
+
assert_equal(4, LongMath.harmonic_mean(0, ROUND_UNNECESSARY, 3, 6))
|
7428
|
+
assert_equal(-3, LongMath.harmonic_mean(0, ROUND_UNNECESSARY, -2, -6))
|
7429
|
+
end
|
7430
|
+
|
7431
|
+
def test_quadratic_means_two_param_known_result
|
7432
|
+
assert_equal(5, LongMath.quadratic_mean(0, ROUND_UNNECESSARY, 1, 7))
|
7433
|
+
assert_equal(10, LongMath.quadratic_mean(0, ROUND_UNNECESSARY, 2, 14))
|
7434
|
+
assert_equal(15, LongMath.quadratic_mean(0, ROUND_UNNECESSARY, 3, 21))
|
7435
|
+
assert_equal(13, LongMath.quadratic_mean(0, ROUND_UNNECESSARY, 7, 17))
|
7436
|
+
assert_equal(-5, LongMath.quadratic_mean(0, ROUND_UNNECESSARY, -1, -7))
|
7437
|
+
end
|
7438
|
+
|
7439
|
+
|
5591
7440
|
#
|
5592
7441
|
# test mul minverse of RoundingMode
|
5593
7442
|
#
|
5594
7443
|
def test_rm_minverse
|
5595
7444
|
print "\ntest_rm_minverse [#{Time.now}]: "
|
5596
|
-
assert_equal(
|
5597
|
-
assert_equal(
|
5598
|
-
assert_equal(
|
5599
|
-
assert_equal(
|
5600
|
-
assert_equal(
|
5601
|
-
assert_equal(
|
5602
|
-
assert_equal(
|
5603
|
-
assert_equal(
|
5604
|
-
assert_equal(
|
5605
|
-
assert_equal(
|
7445
|
+
assert_equal(ROUND_UP, ROUND_DOWN.minverse)
|
7446
|
+
assert_equal(ROUND_DOWN, ROUND_UP.minverse)
|
7447
|
+
assert_equal(ROUND_CEILING, ROUND_FLOOR.minverse)
|
7448
|
+
assert_equal(ROUND_FLOOR, ROUND_CEILING.minverse)
|
7449
|
+
assert_equal(ROUND_HALF_UP, ROUND_HALF_DOWN.minverse)
|
7450
|
+
assert_equal(ROUND_HALF_DOWN, ROUND_HALF_UP.minverse)
|
7451
|
+
assert_equal(ROUND_HALF_CEILING, ROUND_HALF_FLOOR.minverse)
|
7452
|
+
assert_equal(ROUND_HALF_FLOOR, ROUND_HALF_CEILING.minverse)
|
7453
|
+
assert_equal(ROUND_HALF_EVEN, ROUND_HALF_EVEN.minverse)
|
7454
|
+
assert_equal(ROUND_HALF_ODD, ROUND_HALF_ODD.minverse)
|
7455
|
+
assert_equal(ROUND_UNNECESSARY, ROUND_UNNECESSARY.minverse)
|
5606
7456
|
end
|
5607
7457
|
|
5608
7458
|
#
|
@@ -5610,16 +7460,17 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
5610
7460
|
#
|
5611
7461
|
def test_rm_ainverse
|
5612
7462
|
print "\ntest_rm_ainverse [#{Time.now}]: "
|
5613
|
-
assert_equal(
|
5614
|
-
assert_equal(
|
5615
|
-
assert_equal(
|
5616
|
-
assert_equal(
|
5617
|
-
assert_equal(
|
5618
|
-
assert_equal(
|
5619
|
-
assert_equal(
|
5620
|
-
assert_equal(
|
5621
|
-
assert_equal(
|
5622
|
-
assert_equal(
|
7463
|
+
assert_equal(ROUND_UP, ROUND_UP.ainverse)
|
7464
|
+
assert_equal(ROUND_DOWN, ROUND_DOWN.ainverse)
|
7465
|
+
assert_equal(ROUND_CEILING, ROUND_FLOOR.ainverse)
|
7466
|
+
assert_equal(ROUND_FLOOR, ROUND_CEILING.ainverse)
|
7467
|
+
assert_equal(ROUND_HALF_UP, ROUND_HALF_UP.ainverse)
|
7468
|
+
assert_equal(ROUND_HALF_DOWN, ROUND_HALF_DOWN.ainverse)
|
7469
|
+
assert_equal(ROUND_HALF_CEILING, ROUND_HALF_FLOOR.ainverse)
|
7470
|
+
assert_equal(ROUND_HALF_FLOOR, ROUND_HALF_CEILING.ainverse)
|
7471
|
+
assert_equal(ROUND_HALF_EVEN, ROUND_HALF_EVEN.ainverse)
|
7472
|
+
assert_equal(ROUND_HALF_ODD, ROUND_HALF_ODD.ainverse)
|
7473
|
+
assert_equal(ROUND_UNNECESSARY, ROUND_UNNECESSARY.ainverse)
|
5623
7474
|
end
|
5624
7475
|
|
5625
7476
|
|
@@ -5642,7 +7493,6 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
5642
7493
|
@powers_of_10[x] = zz
|
5643
7494
|
end
|
5644
7495
|
assert_equal(z, zz)
|
5645
|
-
puts("exp=" + x.to_s)
|
5646
7496
|
$stdout.flush
|
5647
7497
|
else
|
5648
7498
|
@powers_of_10[x] ||= z
|
@@ -5662,12 +7512,10 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
5662
7512
|
|
5663
7513
|
12.times do |i|
|
5664
7514
|
check_npower10(3**i)
|
5665
|
-
puts(3**i)
|
5666
7515
|
end
|
5667
7516
|
|
5668
7517
|
12.times do |i|
|
5669
7518
|
check_npower10(3**i)
|
5670
|
-
puts(3**i)
|
5671
7519
|
end
|
5672
7520
|
|
5673
7521
|
y = 1
|
@@ -5682,7 +7530,6 @@ class TestLongDecimal_class < Test::Unit::TestCase # RUNIT::TestCase
|
|
5682
7530
|
x0 = 2046
|
5683
7531
|
y0 = 10**x0
|
5684
7532
|
60.times do |i|
|
5685
|
-
puts "i=" + i.to_s
|
5686
7533
|
$stdout.flush
|
5687
7534
|
y = y0
|
5688
7535
|
x = x0
|