diff options
| -rw-r--r-- | composer.json | 2 | ||||
| -rw-r--r-- | src/NextAfter.php | 13 | ||||
| -rw-r--r-- | tests/NextAfterTest.php | 26 |
3 files changed, 28 insertions, 13 deletions
diff --git a/composer.json b/composer.json index d22502a..bde9b11 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "autoload": { "psr-4": { "Nsfisis\\NextAfter\\": "src/", - "Nsfisis\\NextAfter\\Tests\\": "tests/src/" + "Nsfisis\\NextAfter\\Tests\\": "tests/" } }, "config": { diff --git a/src/NextAfter.php b/src/NextAfter.php index e2b7706..678de25 100644 --- a/src/NextAfter.php +++ b/src/NextAfter.php @@ -11,7 +11,6 @@ use function pack; use function unpack; use const INF; use const NAN; -use const PHP_FLOAT_MIN; use const PHP_INT_SIZE; final class NextAfter @@ -46,7 +45,7 @@ final class NextAfter return INF; } if ($x === 0.0) { - return PHP_FLOAT_MIN; + return self::minValue(); } $u = self::floatToInt($x); return $x > 0.0 ? self::intToFloat($u + 1) : @@ -66,7 +65,7 @@ final class NextAfter return -INF; } if ($x === 0.0) { - return -PHP_FLOAT_MIN; + return -self::minValue(); } $u = self::floatToInt($x); return $x > 0.0 ? self::intToFloat($u - 1) : @@ -77,6 +76,14 @@ final class NextAfter /** * @phpstan-pure */ + public static function minValue(): float + { + return self::intToFloat(1); + } + + /** + * @phpstan-pure + */ private static function intToFloat(int $x): float { return self::unpackFloat64(self::packInt64($x)); diff --git a/tests/NextAfterTest.php b/tests/NextAfterTest.php index 8699203..ee61a37 100644 --- a/tests/NextAfterTest.php +++ b/tests/NextAfterTest.php @@ -10,7 +10,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use const INF; use const NAN; -use const PHP_FLOAT_MIN; #[CoversClass(NextAfter::class)] final class NextAfterTest extends TestCase @@ -33,6 +32,11 @@ final class NextAfterTest extends TestCase self::assertSameFloat($expected, NextAfter::nextDown($x)); } + public function testMinValue(): void + { + self::assertSameFloat(5.0e-324, NextAfter::minValue()); + } + /** * @return iterable<array{float, float, float}> */ @@ -56,12 +60,14 @@ final class NextAfterTest extends TestCase yield 'infinity to maximum finite value' => [PHP_FLOAT_MAX, INF, PHP_FLOAT_MAX]; yield 'negative infinity to zero' => [-PHP_FLOAT_MAX, -INF, 0.0]; yield 'negative infinity to negative maximum finite value' => [-PHP_FLOAT_MAX, -INF, -PHP_FLOAT_MAX]; - yield 'zero to infinity' => [PHP_FLOAT_MIN, 0.0, INF]; - yield 'zero to negative infinity' => [-PHP_FLOAT_MIN, 0.0, -INF]; - yield 'negative zero to infinity' => [PHP_FLOAT_MIN, -0.0, INF]; - yield 'negative zero to negative infinity' => [-PHP_FLOAT_MIN, -0.0, -INF]; + yield 'zero to infinity' => [NextAfter::minValue(), 0.0, INF]; + yield 'zero to negative infinity' => [-NextAfter::minValue(), 0.0, -INF]; + yield 'negative zero to infinity' => [NextAfter::minValue(), -0.0, INF]; + yield 'negative zero to negative infinity' => [-NextAfter::minValue(), -0.0, -INF]; yield 'maximum finite value to inifinity' => [INF, PHP_FLOAT_MAX, INF]; yield 'negative maximum finite value to negative inifinity' => [-INF, -PHP_FLOAT_MAX, -INF]; + yield 'minimum positive value to zero' => [0.0, NextAfter::minValue(), 0.0]; + yield 'minimum negative value to negative zero' => [-0.0, -NextAfter::minValue(), -0.0]; } /** @@ -72,10 +78,11 @@ final class NextAfterTest extends TestCase yield 'NaN' => [NAN, NAN]; yield 'positive infinity' => [INF, INF]; yield 'negative infinity' => [-PHP_FLOAT_MAX, -INF]; - yield 'positive zero' => [PHP_FLOAT_MIN, 0.0]; - yield 'negative zero' => [PHP_FLOAT_MIN, -0.0]; + yield 'positive zero' => [NextAfter::minValue(), 0.0]; + yield 'negative zero' => [NextAfter::minValue(), -0.0]; yield 'positive value' => [1.0000000000000002, 1.0]; yield 'negative value' => [-0.9999999999999999, -1.0]; + yield 'minimum negative value' => [-0.0, -NextAfter::minValue()]; yield 'maximum finite value' => [INF, PHP_FLOAT_MAX]; } @@ -87,9 +94,10 @@ final class NextAfterTest extends TestCase yield 'NaN' => [NAN, NAN]; yield 'negative infinity' => [-INF, -INF]; yield 'positive infinity' => [PHP_FLOAT_MAX, INF]; - yield 'positive zero' => [-PHP_FLOAT_MIN, 0.0]; - yield 'negative zero' => [-PHP_FLOAT_MIN, -0.0]; + yield 'positive zero' => [-NextAfter::minValue(), 0.0]; + yield 'negative zero' => [-NextAfter::minValue(), -0.0]; yield 'positive value' => [0.9999999999999999, 1.0]; + yield 'minimum positive value' => [0.0, NextAfter::minValue()]; yield 'negative value' => [-1.0000000000000002, -1.0]; yield 'negative maximum finite value' => [-INF, -PHP_FLOAT_MAX]; } |
