Raymond Toy pushed to branch issue-459-more-accurate-dd-complex-div at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • tests/float.lisp
    ... ... @@ -374,37 +374,32 @@
    374 374
         (min (rerr (realpart computed) (realpart expected))
    
    375 375
     	 (rerr (imagpart computed) (imagpart expected)))))
    
    376 376
     
    
    377
    -(define-test complex-division.double-double
    
    378
    -  (:tag :issues)
    
    379
    -  (loop for k from 1
    
    380
    -	for test in *test-cases*
    
    381
    -	do
    
    382
    -	   (destructuring-bind (x y z-true expected-rel expected-rel-w)
    
    383
    -	       test
    
    384
    -	     (declare (ignore expected-rel z-true))
    
    385
    -	     (flet ((compute-true (a b)
    
    386
    -		      ;; Convert a and b to complex rationals, do the
    
    387
    -		      ;; division and convert back to get the true
    
    388
    -		      ;; expected result.
    
    389
    -		      (coerce
    
    390
    -		       (/ (complex (rational (realpart a))
    
    391
    -				   (rational (imagpart a)))
    
    392
    -			  (complex (rational (realpart b))
    
    393
    -				   (rational (imagpart b))))
    
    394
    -		       '(complex ext:double-double-float))))
    
    395
    -	       (let* ((z (/ (coerce x '(complex ext:double-double-float))
    
    396
    -			    (coerce y '(complex ext:double-double-float))))
    
    397
    -		      (z-true (compute-true x y))
    
    398
    -		      (rel (rel-err z z-true)))
    
    399
    -		 (assert-equal expected-rel-w
    
    400
    -			       rel
    
    401
    -			       k x y z z-true rel))))))
    
    402 377
     (defun do-cdiv-test (x y z-true expected-rel)
    
    403 378
       (let* ((z (/ x y))
    
    404 379
     	 (rel (rel-err z z-true)))
    
    405 380
         (assert-equal expected-rel
    
    406 381
     		  rel
    
    407 382
     		  x y z z-true rel)))
    
    383
    +
    
    384
    +(defun do-cdiv-dd-test (x y z-true expected-rel)
    
    385
    +  (flet ((compute-true (a b)
    
    386
    +	   ;; Convert a and b to complex rationals, do the
    
    387
    +	   ;; division and convert back to get the true
    
    388
    +	   ;; expected result.
    
    389
    +	   (coerce
    
    390
    +	    (/ (complex (rational (realpart a))
    
    391
    +			(rational (imagpart a)))
    
    392
    +	       (complex (rational (realpart b))
    
    393
    +			(rational (imagpart b))))
    
    394
    +	    '(complex ext:double-double-float))))
    
    395
    +    (let* ((z (/ (coerce x '(complex ext:double-double-float))
    
    396
    +		 (coerce y '(complex ext:double-double-float))))
    
    397
    +	   (z-true (compute-true x y))
    
    398
    +	   (rel (rel-err z z-true)))
    
    399
    +      (assert-equal expected-rel
    
    400
    +		    rel
    
    401
    +		    k x y z z-true rel))))
    
    402
    +
    
    408 403
     ;; Issue #456: improve accuracy of division of complex double-floats.
    
    409 404
     ;;
    
    410 405
     ;; Tests for complex division.  Tests 1-10 are from Baudin and Smith.
    
    ... ... @@ -412,10 +407,11 @@
    412 407
     ;; ansi-tests where (/ z z) didn't produce exactly 1.  Tests 13-16 are
    
    413 408
     ;; for examples for improvement iterations 1-4 from McGehearty.
    
    414 409
     (macrolet
    
    415
    -    ((frob (name x y z-true rel)
    
    410
    +    ((frob (name x y z-true rel dd-rel)
    
    416 411
            `(define-test ,name
    
    417 412
     	  (:tag :issues)
    
    418
    -	  (do-cdiv-test ,x ,y ,z-true ,rel))))
    
    413
    +	  (do-cdiv-test ,x ,y ,z-true ,rel)
    
    414
    +	  (do-cdiv-dd-test ,x ,y ,z-true ,dd-rel))))
    
    419 415
       ;; First cases are from Baudin and Smith
    
    420 416
       ;; 1
    
    421 417
       (frob cdiv.baudin-case.1
    
    ... ... @@ -423,61 +419,71 @@
    423 419
     	(complex 1d0 (scale-float 1d0 1023))
    
    424 420
     	(complex (scale-float 1d0 -1023)
    
    425 421
     		 (scale-float -1d0 -1023))
    
    426
    -	53)
    
    422
    +	53
    
    423
    +	106)
    
    427 424
       ;; 2
    
    428 425
       (frob cdiv.baudin-case.2
    
    429 426
     	(complex 1d0 1d0)
    
    430 427
     	(complex (scale-float 1d0 -1023) (scale-float 1d0 -1023))
    
    431 428
     	(complex (scale-float 1d0 1023) 0)
    
    432
    -	53)
    
    429
    +	53
    
    430
    +	106)
    
    433 431
       ;; 3
    
    434 432
       (frob cdiv.baudin-case.3
    
    435 433
     	(complex (scale-float 1d0 1023) (scale-float 1d0 -1023))
    
    436 434
     	(complex (scale-float 1d0 677) (scale-float 1d0 -677))
    
    437 435
     	(complex (scale-float 1d0 346) (scale-float -1d0 -1008))
    
    438
    -	53)
    
    436
    +	53
    
    437
    +	106)
    
    439 438
       ;; 4
    
    440 439
       (frob cdiv.baudin-case.4.overflow
    
    441 440
     	(complex (scale-float 1d0 1023) (scale-float 1d0 1023))
    
    442 441
     	(complex 1d0 1d0)
    
    443 442
     	(complex (scale-float 1d0 1023) 0)
    
    444
    -	53)
    
    443
    +	53
    
    444
    +	106)
    
    445 445
       ;; 5
    
    446 446
       (frob cdiv.baudin-case.5.underflow-ratio
    
    447 447
     	(complex (scale-float 1d0 1020) (scale-float 1d0 -844))
    
    448 448
     	(complex (scale-float 1d0 656) (scale-float 1d0 -780))
    
    449 449
     	(complex (scale-float 1d0 364) (scale-float -1d0 -1072))
    
    450
    -	53)
    
    450
    +	53
    
    451
    +	106)
    
    451 452
       ;; 6
    
    452 453
       (frob cdiv.baudin-case.6.underflow-realpart
    
    453 454
     	(complex (scale-float 1d0 -71) (scale-float 1d0 1021))
    
    454 455
     	(complex (scale-float 1d0 1001) (scale-float 1d0 -323))
    
    455 456
     	(complex (scale-float 1d0 -1072) (scale-float 1d0 20))
    
    456
    -	53)
    
    457
    +	53
    
    458
    +	106)
    
    457 459
       ;; 7
    
    458 460
       (frob cdiv.baudin-case.7.overflow-both-parts
    
    459 461
     	(complex (scale-float 1d0 -347) (scale-float 1d0 -54))
    
    460 462
     	(complex (scale-float 1d0 -1037) (scale-float 1d0 -1058))
    
    461 463
     	(complex 3.898125604559113300d289 8.174961907852353577d295)
    
    462
    -	53)
    
    464
    +	53
    
    465
    +	106)
    
    463 466
       ;; 8
    
    464 467
       (frob cdiv.baudin-case.8
    
    465 468
     	(complex (scale-float 1d0 -1074) (scale-float 1d0 -1074))
    
    466 469
     	(complex (scale-float 1d0 -1073) (scale-float 1d0 -1074))
    
    467 470
     	(complex 0.6d0 0.2d0)
    
    468
    -	53)
    
    471
    +	53
    
    472
    +	106)
    
    469 473
       ;; 9
    
    470 474
       (frob cdiv.baudin-case.9
    
    471 475
     	(complex (scale-float 1d0 1015) (scale-float 1d0 -989))
    
    472 476
     	(complex (scale-float 1d0 1023) (scale-float 1d0 1023))
    
    473 477
     	(complex 0.001953125d0 -0.001953125d0)
    
    474
    -	53)
    
    478
    +	53
    
    479
    +	106)
    
    475 480
       ;; 10
    
    476 481
       (frob cdiv.baudin-case.10.improve-imagpart-accuracy
    
    477 482
     	(complex (scale-float 1d0 -622) (scale-float 1d0 -1071))
    
    478 483
     	(complex (scale-float 1d0 -343) (scale-float 1d0 -798))
    
    479 484
     	(complex 1.02951151789360578d-84 6.97145987515076231d-220)
    
    480
    -	53)
    
    485
    +	53
    
    486
    +	106)
    
    481 487
       ;; 11
    
    482 488
       ;;
    
    483 489
       ;; From Maxima.  This was from a (private) email where Maxima used
    
    ... ... @@ -487,7 +493,8 @@
    487 493
     	#c(1.2d-311 5.7d-312)
    
    488 494
     	#c(3.691993880674614517999740937026568563794896024143749539711267954d301
    
    489 495
     	   -1.753697093319947872394996242210428954266103103602859195409591583d301)
    
    490
    -	52)
    
    496
    +	52
    
    497
    +	107)
    
    491 498
       ;; 12
    
    492 499
       ;;
    
    493 500
       ;; Found by ansi tests. z/z should be exactly 1.
    
    ... ... @@ -495,7 +502,8 @@
    495 502
     	#c(1.565640716292489d19 0.0d0)
    
    496 503
     	#c(1.565640716292489d19 0.0d0)
    
    497 504
     	#c(1d0 0)
    
    498
    -	53)
    
    505
    +	53
    
    506
    +	106)
    
    499 507
       ;; 13
    
    500 508
       ;; Iteration 1.  Without this, we would instead return
    
    501 509
       ;;
    
    ... ... @@ -510,7 +518,8 @@
    510 518
     		 (parse-hex-float "0x1.98b3fbc1677bbp-697"))
    
    511 519
     	(complex (parse-hex-float "0x1.BA8DF8075BCEEp+155")
    
    512 520
     		 (parse-hex-float "-0x1.A4AD628DA5B74p-895"))
    
    513
    -	53)
    
    521
    +	53
    
    522
    +	106)
    
    514 523
       ;; 14
    
    515 524
       ;; Iteration 2.
    
    516 525
       (frob cdiv.mcgehearty-iteration.2
    
    ... ... @@ -520,7 +529,8 @@
    520 529
     		 (parse-hex-float "0x1.417bd33105808p-256"))
    
    521 530
     	(complex (parse-hex-float "0x1.cde593daa4ffep-810")
    
    522 531
     		 (parse-hex-float "-0x1.179b9a63df6d3p-799"))
    
    523
    -	52)
    
    532
    +	52
    
    533
    +	106)
    
    524 534
       ;; 15
    
    525 535
       ;; Iteration 3
    
    526 536
       (frob cdiv.mcgehearty-iteration.3
    
    ... ... @@ -530,7 +540,8 @@
    530 540
     		 (parse-hex-float "0x1.3d80439e9a119p-731"))
    
    531 541
     	(complex (parse-hex-float "-0x1.3b35ed806ae5ap-333")
    
    532 542
     		 (parse-hex-float "-0x0.05e01bcbfd9f6p-1022"))
    
    533
    -	53)
    
    543
    +	53
    
    544
    +	106)
    
    534 545
       ;; 16
    
    535 546
       ;; Iteration 4
    
    536 547
       (frob cdiv.mcgehearty-iteration.4
    
    ... ... @@ -540,7 +551,8 @@
    540 551
     		 (parse-hex-float "0x1.5bd78c9335899p+1021"))
    
    541 552
     	(complex (parse-hex-float "-0x1.423c6ce00c73bp-710")
    
    542 553
     		 (parse-hex-float "0x1.d9edcf45bcb0ep-708"))
    
    543
    -	52))
    
    554
    +	52
    
    555
    +	106))
    
    544 556
     
    
    545 557
     (define-test complex-division.misc
    
    546 558
         (:tag :issue)