@@ -525,6 +525,17 @@ class HalfTest {
525525
526526 assertEquals(Half .MIN_VALUE , Half .NEGATIVE_ZERO .nextUp())
527527 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 )))
528539 }
529540
530541 @Test
@@ -549,8 +560,8 @@ class HalfTest {
549560
550561 @Test
551562 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())
554565 assertTrue((Half .POSITIVE_INFINITY * Half .NaN ).isNaN())
555566 assertTrue((Half .NaN * Half .POSITIVE_INFINITY ).isNaN())
556567 assertTrue((Half .NEGATIVE_INFINITY * Half .NaN ).isNaN())
@@ -560,29 +571,29 @@ class HalfTest {
560571 assertTrue((Half .NEGATIVE_ZERO * Half .NaN ).isNaN())
561572 assertTrue((Half .NaN * Half .NEGATIVE_ZERO ).isNaN())
562573
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())
565576
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())
568579
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())
571582
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())
574585
575586 // 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 )
578589 assertEquals(Half .NEGATIVE_INFINITY , Half (- 2.0f ) * Half .MAX_VALUE )
579590 assertEquals(Half .NEGATIVE_INFINITY , Half .MAX_VALUE * Half (- 2.0f ))
580591
581592 // Underflow
582593 assertEquals(Half .POSITIVE_ZERO , Half .MIN_VALUE * Half .MIN_NORMAL )
583594 assertEquals(Half .NEGATIVE_ZERO , Half .MIN_VALUE * - Half .MIN_NORMAL )
584595
585- assertEquals(Half (8.0f ), Half ( 2.0f ) * Half (4.0f ))
596+ assertEquals(Half (8.0f ), HALF_TWO * Half (4.0f ))
586597 assertEquals(Half (2.88f ), Half (1.2f ) * Half (2.4f ))
587598 assertEquals(Half (- 2.88f ), Half (1.2f ) * Half (- 2.4f ))
588599 assertEquals(Half (- 2.88f ), Half (- 1.2f ) * Half (2.4f ))
@@ -597,8 +608,8 @@ class HalfTest {
597608
598609 @Test
599610 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())
602613 assertTrue((Half .POSITIVE_INFINITY / Half .NaN ).isNaN())
603614 assertTrue((Half .NaN / Half .POSITIVE_INFINITY ).isNaN())
604615 assertTrue((Half .NEGATIVE_INFINITY / Half .NaN ).isNaN())
@@ -608,17 +619,17 @@ class HalfTest {
608619 assertTrue((Half .NEGATIVE_ZERO / Half .NaN ).isNaN())
609620 assertTrue((Half .NaN / Half .NEGATIVE_ZERO ).isNaN())
610621
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())
613624
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())
616627
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())
619630
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())
622633
623634 // Underflow
624635 assertEquals(Half .POSITIVE_ZERO , Half .MIN_VALUE / Half .MAX_VALUE )
@@ -628,7 +639,7 @@ class HalfTest {
628639 assertEquals(Half .POSITIVE_INFINITY , Half .MAX_VALUE / Half .MIN_VALUE )
629640 assertEquals(Half .NEGATIVE_INFINITY , (- Half .MAX_VALUE ) / Half .MIN_VALUE )
630641
631- assertEquals(Half (0.5f ), Half ( 2.0f ) / Half (4.0f ))
642+ assertEquals(Half (0.5f ), HALF_TWO / Half (4.0f ))
632643 assertEquals(Half (0.5f ), Half (1.2f ) / Half (2.4f ))
633644 assertEquals(Half (- 0.5f ), Half (1.2f ) / Half (- 2.4f ))
634645 assertEquals(Half (- 0.5f ), Half (- 1.2f ) / Half (2.4f ))
@@ -637,10 +648,86 @@ class HalfTest {
637648 assertEquals(Half (16_000.0f ), Half (48_000.0f ) / Half (3.0f ))
638649 assertEquals(Half (- 16_000.0f ), Half (48_000.0f ) / Half (- 3.0f ))
639650
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 ))
642653
643654 assertEquals(Half (75.0f ), Half (0.03f ) / Half (0.0004f ))
644655 assertEquals(Half (- 75.0f ), Half (0.03f ) / Half (- 0.0004f ))
645656 }
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+ }
646733}
0 commit comments