@@ -525,6 +525,17 @@ class HalfTest {
525
525
526
526
assertEquals(Half .MIN_VALUE , Half .NEGATIVE_ZERO .nextUp())
527
527
assertEquals(- Half .MIN_VALUE , Half .NEGATIVE_ZERO .nextDown())
528
+
529
+ assertTrue(Half .NaN .nextTowards(HALF_TWO ).isNaN())
530
+ assertTrue(HALF_TWO .nextTowards(Half .NaN ).isNaN())
531
+ assertEquals(HALF_ONE , HALF_ONE .nextTowards(HALF_ONE ))
532
+ assertEquals(- HALF_ONE , (- HALF_ONE ).nextTowards(- HALF_ONE ))
533
+
534
+ assertEquals(Half (1025.0f ), Half (1024.0f ).nextTowards(Half (32768.0f )))
535
+ assertEquals(Half (1023.5f ), Half (1024.0f ).nextTowards(Half (- 32768.0f )))
536
+
537
+ assertEquals(Half (0.50048830f ), Half (0.5f ).nextTowards(Half (32768.0f )))
538
+ assertEquals(Half (0.49975586f ), Half (0.5f ).nextTowards(Half (- 32768.0f )))
528
539
}
529
540
530
541
@Test
@@ -549,8 +560,8 @@ class HalfTest {
549
560
550
561
@Test
551
562
fun multiplication () {
552
- assertTrue((Half ( 2.0f ) * Half .NaN ).isNaN())
553
- assertTrue((Half .NaN * Half ( 2.0f ) ).isNaN())
563
+ assertTrue((HALF_TWO * Half .NaN ).isNaN())
564
+ assertTrue((Half .NaN * HALF_TWO ).isNaN())
554
565
assertTrue((Half .POSITIVE_INFINITY * Half .NaN ).isNaN())
555
566
assertTrue((Half .NaN * Half .POSITIVE_INFINITY ).isNaN())
556
567
assertTrue((Half .NEGATIVE_INFINITY * Half .NaN ).isNaN())
@@ -560,29 +571,29 @@ class HalfTest {
560
571
assertTrue((Half .NEGATIVE_ZERO * Half .NaN ).isNaN())
561
572
assertTrue((Half .NaN * Half .NEGATIVE_ZERO ).isNaN())
562
573
563
- assertTrue((Half ( 2.0f ) * Half .POSITIVE_INFINITY ).isInfinite())
564
- assertTrue((Half .POSITIVE_INFINITY * Half ( 2.0f ) ).isInfinite())
574
+ assertTrue((HALF_TWO * Half .POSITIVE_INFINITY ).isInfinite())
575
+ assertTrue((Half .POSITIVE_INFINITY * HALF_TWO ).isInfinite())
565
576
566
- assertTrue((Half ( 2.0f ) * Half .NEGATIVE_INFINITY ).isInfinite())
567
- assertTrue((Half .NEGATIVE_INFINITY * Half ( 2.0f ) ).isInfinite())
577
+ assertTrue((HALF_TWO * Half .NEGATIVE_INFINITY ).isInfinite())
578
+ assertTrue((Half .NEGATIVE_INFINITY * HALF_TWO ).isInfinite())
568
579
569
- assertTrue((Half ( 2.0f ) * Half .POSITIVE_ZERO ).isZero())
570
- assertTrue((Half .POSITIVE_ZERO * Half ( 2.0f ) ).isZero())
580
+ assertTrue((HALF_TWO * Half .POSITIVE_ZERO ).isZero())
581
+ assertTrue((Half .POSITIVE_ZERO * HALF_TWO ).isZero())
571
582
572
- assertTrue((Half ( 2.0f ) * Half .NEGATIVE_ZERO ).isZero())
573
- assertTrue((Half .NEGATIVE_ZERO * Half ( 2.0f ) ).isZero())
583
+ assertTrue((HALF_TWO * Half .NEGATIVE_ZERO ).isZero())
584
+ assertTrue((Half .NEGATIVE_ZERO * HALF_TWO ).isZero())
574
585
575
586
// Overflow
576
- assertEquals(Half .POSITIVE_INFINITY , Half ( 2.0f ) * Half .MAX_VALUE )
577
- assertEquals(Half .POSITIVE_INFINITY , Half .MAX_VALUE * Half ( 2.0f ) )
587
+ assertEquals(Half .POSITIVE_INFINITY , HALF_TWO * Half .MAX_VALUE )
588
+ assertEquals(Half .POSITIVE_INFINITY , Half .MAX_VALUE * HALF_TWO )
578
589
assertEquals(Half .NEGATIVE_INFINITY , Half (- 2.0f ) * Half .MAX_VALUE )
579
590
assertEquals(Half .NEGATIVE_INFINITY , Half .MAX_VALUE * Half (- 2.0f ))
580
591
581
592
// Underflow
582
593
assertEquals(Half .POSITIVE_ZERO , Half .MIN_VALUE * Half .MIN_NORMAL )
583
594
assertEquals(Half .NEGATIVE_ZERO , Half .MIN_VALUE * - Half .MIN_NORMAL )
584
595
585
- assertEquals(Half (8.0f ), Half ( 2.0f ) * Half (4.0f ))
596
+ assertEquals(Half (8.0f ), HALF_TWO * Half (4.0f ))
586
597
assertEquals(Half (2.88f ), Half (1.2f ) * Half (2.4f ))
587
598
assertEquals(Half (- 2.88f ), Half (1.2f ) * Half (- 2.4f ))
588
599
assertEquals(Half (- 2.88f ), Half (- 1.2f ) * Half (2.4f ))
@@ -597,8 +608,8 @@ class HalfTest {
597
608
598
609
@Test
599
610
fun division () {
600
- assertTrue((Half ( 2.0f ) / Half .NaN ).isNaN())
601
- assertTrue((Half .NaN / Half ( 2.0f ) ).isNaN())
611
+ assertTrue((HALF_TWO / Half .NaN ).isNaN())
612
+ assertTrue((Half .NaN / HALF_TWO ).isNaN())
602
613
assertTrue((Half .POSITIVE_INFINITY / Half .NaN ).isNaN())
603
614
assertTrue((Half .NaN / Half .POSITIVE_INFINITY ).isNaN())
604
615
assertTrue((Half .NEGATIVE_INFINITY / Half .NaN ).isNaN())
@@ -608,17 +619,17 @@ class HalfTest {
608
619
assertTrue((Half .NEGATIVE_ZERO / Half .NaN ).isNaN())
609
620
assertTrue((Half .NaN / Half .NEGATIVE_ZERO ).isNaN())
610
621
611
- assertTrue((Half ( 2.0f ) / Half .POSITIVE_INFINITY ).isZero())
612
- assertTrue((Half .POSITIVE_INFINITY / Half ( 2.0f ) ).isInfinite())
622
+ assertTrue((HALF_TWO / Half .POSITIVE_INFINITY ).isZero())
623
+ assertTrue((Half .POSITIVE_INFINITY / HALF_TWO ).isInfinite())
613
624
614
- assertTrue((Half ( 2.0f ) / Half .NEGATIVE_INFINITY ).isZero())
615
- assertTrue((Half .NEGATIVE_INFINITY / Half ( 2.0f ) ).isInfinite())
625
+ assertTrue((HALF_TWO / Half .NEGATIVE_INFINITY ).isZero())
626
+ assertTrue((Half .NEGATIVE_INFINITY / HALF_TWO ).isInfinite())
616
627
617
- assertTrue((Half ( 2.0f ) / Half .POSITIVE_ZERO ).isInfinite())
618
- assertTrue((Half .POSITIVE_ZERO / Half ( 2.0f ) ).isZero())
628
+ assertTrue((HALF_TWO / Half .POSITIVE_ZERO ).isInfinite())
629
+ assertTrue((Half .POSITIVE_ZERO / HALF_TWO ).isZero())
619
630
620
- assertTrue((Half ( 2.0f ) / Half .NEGATIVE_ZERO ).isInfinite())
621
- assertTrue((Half .NEGATIVE_ZERO / Half ( 2.0f ) ).isZero())
631
+ assertTrue((HALF_TWO / Half .NEGATIVE_ZERO ).isInfinite())
632
+ assertTrue((Half .NEGATIVE_ZERO / HALF_TWO ).isZero())
622
633
623
634
// Underflow
624
635
assertEquals(Half .POSITIVE_ZERO , Half .MIN_VALUE / Half .MAX_VALUE )
@@ -628,7 +639,7 @@ class HalfTest {
628
639
assertEquals(Half .POSITIVE_INFINITY , Half .MAX_VALUE / Half .MIN_VALUE )
629
640
assertEquals(Half .NEGATIVE_INFINITY , (- Half .MAX_VALUE ) / Half .MIN_VALUE )
630
641
631
- assertEquals(Half (0.5f ), Half ( 2.0f ) / Half (4.0f ))
642
+ assertEquals(Half (0.5f ), HALF_TWO / Half (4.0f ))
632
643
assertEquals(Half (0.5f ), Half (1.2f ) / Half (2.4f ))
633
644
assertEquals(Half (- 0.5f ), Half (1.2f ) / Half (- 2.4f ))
634
645
assertEquals(Half (- 0.5f ), Half (- 1.2f ) / Half (2.4f ))
@@ -637,10 +648,86 @@ class HalfTest {
637
648
assertEquals(Half (16_000.0f ), Half (48_000.0f ) / Half (3.0f ))
638
649
assertEquals(Half (- 16_000.0f ), Half (48_000.0f ) / Half (- 3.0f ))
639
650
640
- assertEquals(Half (2 .0861626e- 5f ), Half ( 1.0f ) / Half (48_000.0f ))
641
- assertEquals(Half (- 2 .0861626e- 5 ), Half ( 1.0f ) / Half (- 48_000.0f ))
651
+ assertEquals(Half (2 .0861626e- 5f ), HALF_ONE / Half (48_000.0f ))
652
+ assertEquals(Half (- 2 .0861626e- 5 ), HALF_ONE / Half (- 48_000.0f ))
642
653
643
654
assertEquals(Half (75.0f ), Half (0.03f ) / Half (0.0004f ))
644
655
assertEquals(Half (- 75.0f ), Half (0.03f ) / Half (- 0.0004f ))
645
656
}
657
+
658
+ @Test
659
+ fun addition () {
660
+ assertTrue((Half .NaN + Half .NaN ).isNaN())
661
+
662
+ assertTrue((Half .NaN + HALF_ONE ).isNaN())
663
+ assertTrue((Half .NaN - HALF_ONE ).isNaN())
664
+ assertTrue((HALF_ONE + Half .NaN ).isNaN())
665
+ assertTrue((Half (- 1.0f ) + Half .NaN ).isNaN())
666
+
667
+ assertTrue((Half .NaN + Half .POSITIVE_INFINITY ).isNaN())
668
+ assertTrue((Half .POSITIVE_INFINITY + Half .NaN ).isNaN())
669
+ assertTrue((Half .NaN + Half .NEGATIVE_INFINITY ).isNaN())
670
+ assertTrue((Half .NEGATIVE_INFINITY + Half .NaN ).isNaN())
671
+
672
+ assertTrue((Half .NaN + Half .POSITIVE_ZERO ).isNaN())
673
+ assertTrue((Half .POSITIVE_ZERO + Half .NaN ).isNaN())
674
+ assertTrue((Half .NaN + Half .NEGATIVE_ZERO ).isNaN())
675
+ assertTrue((Half .NEGATIVE_ZERO + Half .NaN ).isNaN())
676
+
677
+ assertTrue((Half .POSITIVE_INFINITY + HALF_ONE ).isInfinite())
678
+ assertTrue((Half .POSITIVE_INFINITY - HALF_ONE ).isInfinite())
679
+ assertTrue((HALF_ONE + Half .POSITIVE_INFINITY ).isInfinite())
680
+ assertTrue((HALF_ONE - Half .POSITIVE_INFINITY ).isInfinite())
681
+ assertTrue((Half .POSITIVE_INFINITY + Half .POSITIVE_INFINITY ).isInfinite())
682
+ assertTrue((Half .POSITIVE_INFINITY - Half .POSITIVE_INFINITY ).isNaN())
683
+
684
+ assertTrue((Half .NEGATIVE_INFINITY - HALF_ONE ).isInfinite())
685
+ assertTrue((Half .NEGATIVE_INFINITY + HALF_ONE ).isInfinite())
686
+ assertTrue((HALF_ONE + Half .NEGATIVE_INFINITY ).isInfinite())
687
+ assertTrue((HALF_ONE - Half .NEGATIVE_INFINITY ).isInfinite())
688
+ assertTrue((Half .NEGATIVE_INFINITY + Half .NEGATIVE_INFINITY ).isInfinite())
689
+ assertTrue((Half .NEGATIVE_INFINITY - Half .NEGATIVE_INFINITY ).isNaN())
690
+
691
+ assertEquals(Half (3.0f ), HALF_ONE + HALF_TWO )
692
+
693
+ // Overflow
694
+ assertEquals(Half .POSITIVE_INFINITY , Half (32768.0f ) + Half (32768.0f ))
695
+ // Underflow
696
+ assertEquals(Half .NEGATIVE_INFINITY , Half (- 32768.0f ) - Half (32768.0f ))
697
+
698
+ for (i in 0x0 .. 0xffff ) {
699
+ val v1 = Half (i.toUShort())
700
+ if (v1.isFinite()) {
701
+ assertTrue((v1 - v1).isZero())
702
+ assertEquals(v1 * HALF_TWO , v1 + v1)
703
+ }
704
+ }
705
+ }
706
+
707
+ @Test
708
+ fun ulp () {
709
+ assertTrue(Half .NaN .ulp.isNaN())
710
+
711
+ assertTrue(Half .POSITIVE_INFINITY .ulp.isInfinite())
712
+ assertTrue(Half .NEGATIVE_INFINITY .ulp.isInfinite())
713
+
714
+ assertTrue((Half .MAX_VALUE + Half .MAX_VALUE .ulp).isInfinite())
715
+
716
+ assertEquals(Half .MIN_VALUE , Half .POSITIVE_ZERO .ulp)
717
+ assertEquals(Half .MIN_VALUE , Half .NEGATIVE_ZERO .ulp)
718
+
719
+ assertEquals(HALF_ONE , Half (1024.0f ).ulp)
720
+ assertEquals(HALF_ONE , Half (- 1024.0f ).ulp)
721
+ }
722
+
723
+ @Test
724
+ fun sqrt () {
725
+ for (i in 0x0 .. 0xffff ) {
726
+ val v1 = Half (i.toUShort())
727
+ if (v1.isFinite()) {
728
+ val v2 = sqrt(v1)
729
+ assertTrue(v1 - (v2 * v2) <= HALF_TWO * v1.ulp)
730
+ }
731
+ }
732
+ }
646
733
}
0 commit comments