aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/NextAfter.php
blob: e2b7706195a2c0fd025bd8e5591ab6c5f830f7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

declare(strict_types=1);

namespace Nsfisis\NextAfter;

use function assert;
use function is_infinite;
use function is_nan;
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
{
    private function __construct()
    {
    }

    /**
     * @phpstan-pure
     */
    public static function nextAfter(float $x, float $y): float
    {
        if (is_nan($x) || is_nan($y)) {
            return NAN;
        }
        if ($x === $y) {
            return $y;
        }
        return $x < $y ? self::nextUp($x) : self::nextDown($x);
    }

    /**
     * @phpstan-pure
     */
    public static function nextUp(float $x): float
    {
        if (is_nan($x)) {
            return NAN;
        }
        if (is_infinite($x) && $x > 0) {
            return INF;
        }
        if ($x === 0.0) {
            return PHP_FLOAT_MIN;
        }
        $u = self::floatToInt($x);
        return $x > 0.0 ? self::intToFloat($u + 1) :
        self::intToFloat($u - 1);

    }

    /**
     * @phpstan-pure
     */
    public static function nextDown(float $x): float
    {
        if (is_nan($x)) {
            return NAN;
        }
        if (is_infinite($x) && $x < 0) {
            return -INF;
        }
        if ($x === 0.0) {
            return -PHP_FLOAT_MIN;
        }
        $u = self::floatToInt($x);
        return $x > 0.0 ? self::intToFloat($u - 1) :
        self::intToFloat($u + 1);

    }

    /**
     * @phpstan-pure
     */
    private static function intToFloat(int $x): float
    {
        return self::unpackFloat64(self::packInt64($x));
    }

    /**
     * @phpstan-pure
     */
    private static function floatToInt(float $x): int
    {
        return self::unpackInt64(self::packFloat64($x));
    }

    /**
     * @phpstan-pure
     */
    private static function unpackFloat64(string $s): float
    {
        assert(PHP_FLOAT_DIG === 15); // @phpstan-ignore-line
        return unpack('d', $s)[1]; // @phpstan-ignore-line
    }

    /**
     * @phpstan-pure
     */
    private static function packInt64(int $x): string
    {
        assert(PHP_INT_SIZE === 8); // @phpstan-ignore-line
        return pack('q', $x);
    }

    /**
     * @phpstan-pure
     */
    private static function packFloat64(float $x): string
    {
        assert(PHP_FLOAT_DIG === 15); // @phpstan-ignore-line
        return pack('d', $x);
    }

    /**
     * @phpstan-pure
     */
    private static function unpackInt64(string $s): int
    {
        assert(PHP_INT_SIZE === 8); // @phpstan-ignore-line
        return unpack('q', $s)[1]; // @phpstan-ignore-line
    }
}