3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('serialize_precision', 13); $encoder = new FloatEncoder(); var_dump($encoder->encode(1.1234567890123456, 1, [ 'float.integers' => false, 'float.precision' => false, ], function () { })); class FloatEncoder { /** The maximum value that can be accurately represented by a float */ const FLOAT_MAX = 9007199254740992.0; /** @var array Default values for options in the encoder */ private static $defaultOptions = [ 'float.integers' => false, 'float.precision' => 17, ]; public function getDefaultOptions() { return self::$defaultOptions; } public function supports($value) { return is_float($value); } public function encode($value, $depth, array $options, callable $encode) { if (is_nan($value)) { return 'NAN'; } elseif (is_infinite($value)) { return $value < 0 ? '-INF' : 'INF'; } return $this->encodeNumber($value, $options); } /** * Encodes the number as a PHP number representation. * @param float $float The number to encode * @param array $options The float encoding options * @return string The PHP code representation for the number */ private function encodeNumber($float, array $options) { if ($this->isInteger($float, $options['float.integers'])) { return number_format($float, 0, '.', ''); } elseif ($float === 0.0) { return '0.0'; } $precision = $options['float.precision']; if ($precision === false) { $precision = ini_get('serialize_precision'); } return $this->encodeFloat($float, $precision); } /** * Tells if the number can be encoded as an integer. * @param float $float The number to test * @param bool|string $allowIntegers Whether integers should be allowed * @return bool True if the number can be encoded as an integer, false if not */ private function isInteger($float, $allowIntegers) { if (!$allowIntegers || round($float) !== $float) { return false; } elseif (abs($float) < self::FLOAT_MAX) { return true; } return $allowIntegers === 'all'; } /** * Encodes the number using a floating point representation. * @param float $float The number to encode * @param int $precision The maximum precision of encoded floats * @return string The PHP code representation for the number */ private function encodeFloat($float, $precision) { $precision = max(1, (int) $precision); $log = (int) floor(log(abs($float), 10)); if (abs($float) < self::FLOAT_MAX && $log > -5 && abs($log) < $precision) { return $this->formatFloat($float, $precision - $log - 1); } // Deal with overflow that results from rounding $log += (int) (round(abs($float) / pow(10, $log), $precision - 1) / 10); $string = $this->formatFloat($float / pow(10, $log), $precision - 1); return sprintf('%sE%+d', $string, $log); } /** * Formats the number as a decimal number. * @param float $float The number to format * @param int $digits The maximum number of decimal digits * @return string The number formatted as a decimal number */ private function formatFloat($float, $digits) { $digits = max((int) $digits, 1); $string = rtrim(number_format($float, $digits, '.', ''), '0'); return substr($string, -1) === '.' ? $string . '0' : $string; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0180.00318.68
8.3.50.0160.00721.41
8.3.40.0150.00019.09
8.3.30.0070.01018.96
8.3.20.0080.00020.40
8.3.10.0030.00621.92
8.3.00.0040.00420.41
8.2.180.0150.00418.41
8.2.170.0170.00322.96
8.2.160.0110.00420.52
8.2.150.0050.00324.18
8.2.140.0070.00024.66
8.2.130.0050.00326.16
8.2.120.0050.00517.88
8.2.110.0050.00522.51
8.2.100.0090.00318.00
8.2.90.0000.00819.44
8.2.80.0030.00617.97
8.2.70.0030.00517.88
8.2.60.0050.00318.05
8.2.50.0040.00418.07
8.2.40.0000.00919.64
8.2.30.0040.00420.87
8.2.20.0040.00417.88
8.2.10.0070.00018.48
8.2.00.0030.00518.23
8.1.280.0100.00725.92
8.1.270.0030.00524.09
8.1.260.0000.00826.35
8.1.250.0000.00728.09
8.1.240.0050.00522.81
8.1.230.0110.00017.73
8.1.220.0000.00818.04
8.1.210.0040.00418.77
8.1.200.0090.00017.60
8.1.190.0050.00317.50
8.1.180.0000.00818.10
8.1.170.0030.00518.83
8.1.160.0030.00522.30
8.1.150.0050.00219.13
8.1.140.0060.00317.62
8.1.130.0000.00818.02
8.1.120.0070.00017.66
8.1.110.0050.00317.58
8.1.100.0040.00417.64
8.1.90.0070.00017.70
8.1.80.0000.00717.71
8.1.70.0040.00417.74
8.1.60.0040.00417.87
8.1.50.0040.00417.75
8.1.40.0000.00917.76
8.1.30.0040.00417.94
8.1.20.0000.00817.84
8.1.10.0050.00317.74
8.1.00.0000.00817.76
8.0.300.0040.00418.77
8.0.290.0000.00717.13
8.0.280.0030.00318.63
8.0.270.0000.00717.43
8.0.260.0070.00017.41
8.0.250.0020.00517.27
8.0.240.0070.00017.25
8.0.230.0040.00417.25
8.0.220.0040.00417.17
8.0.210.0040.00417.15
8.0.200.0000.00617.20
8.0.190.0050.00317.30
8.0.180.0000.00817.11
8.0.170.0050.00317.13
8.0.160.0070.00017.11
8.0.150.0030.00517.20
8.0.140.0040.00417.05
8.0.130.0030.00313.66
8.0.120.0000.00817.15
8.0.110.0070.00017.11
8.0.100.0000.00717.10
8.0.90.0000.00717.26
8.0.80.0070.01117.13
8.0.70.0000.00717.07
8.0.60.0000.00717.15
8.0.50.0050.00216.98
8.0.30.0110.01217.31
8.0.20.0140.00817.40
8.0.10.0070.00017.31
8.0.00.0050.01317.04
7.4.330.0030.00315.28
7.4.320.0000.00616.78
7.4.300.0000.00716.75
7.4.290.0070.00016.68
7.4.280.0030.00516.70
7.4.270.0000.00716.77
7.4.260.0030.00316.80
7.4.250.0040.00416.85
7.4.240.0030.00416.79
7.4.230.0050.00216.75
7.4.220.0160.00316.78
7.4.210.0060.00916.82
7.4.200.0030.00416.83
7.4.160.0030.01316.73
7.4.150.0100.00717.40
7.4.140.0100.01217.86
7.4.130.0070.01016.62
7.4.120.0060.01216.71
7.4.110.0100.00716.73
7.4.100.0200.00316.68
7.4.90.0030.01316.84
7.4.80.0080.01319.39
7.4.70.0060.00916.70
7.4.60.0130.00316.60
7.4.50.0000.00416.69
7.4.40.0110.01316.77
7.4.30.0120.00616.60
7.4.10.0130.00715.65
7.4.00.0080.00615.19
7.3.330.0050.00013.54
7.3.320.0000.00613.59
7.3.310.0000.00716.70
7.3.300.0060.00016.53
7.3.290.0070.00716.58
7.3.280.0090.01016.61
7.3.270.0070.01117.40
7.3.260.0110.01116.77
7.3.250.0100.00716.79
7.3.240.0170.00716.47
7.3.230.0060.01416.66
7.3.210.0080.00816.73
7.3.200.0150.00616.75
7.3.190.0030.01616.47
7.3.180.0140.00316.66
7.3.170.0100.00616.71
7.3.160.0080.00816.64
7.3.130.0080.01015.39
7.3.120.0040.01414.89
7.3.110.0100.01015.22
7.3.100.0070.00715.32
7.3.90.0070.00715.02
7.3.80.0000.01215.48
7.3.70.0060.00915.01
7.3.60.0040.01215.19
7.3.50.0110.00315.19
7.3.40.0060.01315.03
7.3.30.0070.00415.06
7.3.20.0050.00517.06
7.3.10.0030.00617.38
7.3.00.0030.01316.85
7.2.330.0100.01416.77
7.2.320.0070.01117.05
7.2.310.0000.01817.00
7.2.300.0030.02017.06
7.2.290.0110.00717.05
7.2.260.0110.00715.29
7.2.250.0030.01215.57
7.2.240.0090.00615.45
7.2.230.0040.01215.72
7.2.220.0070.01015.48
7.2.210.0100.00315.29
7.2.200.0000.01615.46
7.2.190.0030.01315.71
7.2.180.0070.01315.55
7.2.170.0030.00915.33
7.2.160.0090.00315.53
7.2.150.0000.01517.35
7.2.140.0040.01117.32
7.2.130.0090.00017.14
7.2.120.0110.00317.31
7.2.110.0040.01117.37
7.2.100.0030.01017.20
7.2.90.0080.00817.30
7.2.80.0030.01017.15
7.2.70.0060.01017.39
7.2.60.0090.00617.24
7.2.50.0030.00917.26
7.2.40.0000.01517.05
7.2.30.0090.00317.45
7.2.20.0030.00917.50
7.2.10.0030.00917.55
7.2.00.0030.01418.57
7.1.330.0030.01316.32
7.1.320.0030.00916.24
7.1.310.0060.00616.01
7.1.300.0090.00616.14
7.1.290.0140.00015.98
7.1.280.0050.00615.89
7.1.270.0120.00316.25
7.1.260.0000.01516.05
7.1.250.0000.01116.14
7.1.240.0080.00816.19
7.1.230.0030.00616.12
7.1.220.0060.00316.16
7.1.210.0030.00916.18
7.1.200.0020.01316.13
7.1.190.0070.00716.09
7.1.180.0110.00316.13
7.1.170.0000.01216.23
7.1.160.0040.01216.15
7.1.150.0030.01015.94
7.1.140.0060.00616.06
7.1.130.0120.00616.30
7.1.120.0090.00916.24
7.1.110.0090.00916.11
7.1.100.0030.00917.36
7.1.90.0040.01116.05
7.1.80.0070.00715.95
7.1.70.0060.00616.93
7.1.60.0110.00717.64
7.1.50.0070.00916.77
7.1.40.0100.00315.86
7.1.30.0040.00416.07
7.1.20.0040.01116.39
7.1.10.0030.01215.84
7.1.00.0030.04219.37
7.0.330.0030.00515.91
7.0.320.0070.00315.89
7.0.310.0030.01015.82
7.0.300.0070.00415.71
7.0.290.0070.00715.73
7.0.280.0080.00415.87
7.0.270.0060.00615.77
7.0.260.0040.01115.91
7.0.250.0090.00915.87
7.0.240.0100.00316.03
7.0.230.0090.00915.95
7.0.220.0040.00815.81
7.0.210.0040.01115.87
7.0.200.0060.00416.49
7.0.190.0060.00615.82
7.0.180.0060.00815.76
7.0.170.0060.00315.86
7.0.160.0070.00715.81
7.0.150.0040.00415.78
7.0.140.0000.00915.61
7.0.130.0060.00915.68
7.0.120.0070.01015.98
7.0.110.0100.00615.63
7.0.100.0110.00415.63
7.0.90.0030.01015.86
7.0.80.0000.01615.93
7.0.70.0060.00615.95
7.0.60.0150.04218.05
7.0.50.0050.02916.90
7.0.40.0070.04717.21
7.0.30.0090.02917.17
7.0.20.0130.04817.31
7.0.10.0120.03917.03
7.0.00.0030.02517.24
5.6.400.0070.00714.73
5.6.390.0000.01414.71
5.6.380.0030.01014.73
5.6.370.0090.00614.87
5.6.360.0030.01014.47
5.6.350.0090.00614.73
5.6.340.0040.00814.99
5.6.330.0000.01914.57
5.6.320.0060.00315.02
5.6.310.0000.00915.14
5.6.300.0080.00514.98
5.6.290.0000.01014.81
5.6.280.0050.03718.04
5.6.270.0030.01214.72
5.6.260.0000.01114.68
5.6.250.0080.00414.83
5.6.240.0100.00714.81
5.6.230.0150.00314.59
5.6.220.0040.00814.63
5.6.210.0080.04017.76
5.6.200.0060.04316.57
5.6.190.0110.03817.92
5.6.180.0280.03517.83
5.6.170.0120.02817.73
5.6.160.0030.02717.68
5.6.150.0050.04716.56
5.6.140.0070.03816.55
5.6.130.0070.02316.67
5.6.120.0030.03018.11
5.6.110.0100.03718.04
5.6.100.0070.04118.01
5.6.90.0130.04118.00
5.6.80.0130.03517.64
5.6.70.0000.01414.23
5.6.60.0040.00814.72
5.6.50.0080.00314.68
5.6.40.0100.00014.53
5.6.30.0030.00814.49
5.6.20.0080.00614.61
5.6.10.0070.00714.56
5.6.00.0050.00514.47
5.5.380.0000.01815.05
5.5.370.0090.00914.64
5.5.360.0080.00014.80
5.5.350.0120.04617.70
5.5.340.0080.03316.44
5.5.330.0050.04517.61
5.5.320.0220.02117.47
5.5.310.0180.02717.58
5.5.300.0070.04416.67
5.5.290.0030.02416.64
5.5.280.0070.04517.76
5.5.270.0080.02017.83
5.5.260.0130.03717.96
5.5.250.0080.04317.66
5.5.240.0130.04117.61
5.5.230.0060.00914.62
5.5.220.0130.00014.41
5.5.210.0040.00814.61
5.5.200.0030.01414.59
5.5.190.0040.00714.65
5.5.180.0000.01214.80
5.5.170.0060.00614.80
5.5.160.0100.00314.51
5.5.150.0070.00714.79
5.5.140.0000.01214.43
5.5.130.0060.00614.61
5.5.120.0000.01314.43
5.5.110.0030.01014.45
5.5.100.0030.01014.63
5.5.90.0100.00314.99
5.5.80.0070.00714.74
5.5.70.0060.00914.41
5.5.60.0000.00914.74
5.5.50.0030.01014.66
5.5.40.0030.01214.64
5.5.30.0060.00914.57
5.5.20.0090.00314.78
5.5.10.0030.01014.42
5.5.00.0000.01214.34
5.4.450.0390.03516.43
5.4.440.0420.03516.38
5.4.430.0550.03516.59
5.4.420.0380.03016.50
5.4.410.0400.02716.30
5.4.400.0360.03416.26
5.4.390.0400.03016.37
5.4.380.0380.03216.35
5.4.370.0370.02716.33
5.4.360.0320.03216.44
5.4.350.0660.02816.34
5.4.340.0460.02816.44
5.4.330.0080.00313.52
5.4.320.0350.03016.43
5.4.310.0350.03616.42
5.4.300.0350.02816.22
5.4.290.0530.01816.29
5.4.280.0380.03316.44
5.4.270.0470.04316.41
5.4.260.0460.02516.37
5.4.250.0470.02816.37
5.4.240.0210.02816.44
5.4.230.0120.03516.20
5.4.220.0080.02816.35
5.4.210.0130.03016.44
5.4.200.0360.02916.26
5.4.190.0330.02916.47
5.4.180.0300.03216.22
5.4.170.0400.03216.25
5.4.160.0570.03016.45
5.4.150.0500.02816.21
5.4.140.0470.02615.05
5.4.130.0310.02815.11
5.4.120.0430.03315.18
5.4.110.0350.02815.05
5.4.100.0380.02315.15
5.4.90.0350.02515.16
5.4.80.0340.02715.09
5.4.70.0130.02215.11
5.4.60.0120.03815.07
5.4.50.0020.02815.20
5.4.40.0170.03515.14
5.4.30.0180.03115.00
5.4.20.0140.03615.22
5.4.10.0030.00313.52
5.4.00.0060.00613.52

preferences:
45.47 ms | 401 KiB | 5 Q