3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; public function __construct() { return $this; } /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function get($string, $precision = 4) { $number = static::_get($string, $precision); $number->number = bcadd($string, '0', $precision); return $number; } private static function _get($string, $precision) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = $string; return $number; } /** * Add a formal entry point for decimal numbers * @param DecimalNumber $number Decimal number to base off of * @param integer $precision new precision * @return DecimalNumber DecimalNumber with new precision */ public static function decimalNumber(DecimalNumber $number, $precision = 4) { if ($number->precision === $precision) return clone $number; return static::get($number, $precision); } /** * Perform an addition operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function sum(DecimalNumber $number) { return DecimalNumber::_get(bcadd($this->number, $number, $this->precision), $this->precision); } /** * Perform a subtraction operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function difference(DecimalNumber $number) { return DecimalNumber::_get(bcsub($this->number, $number, $this->precision), $this->precision); } /** * Perform a multiplication operation * @param DecimalNumber $number Multiplier * @return DecimalNumber Product */ public function product(DecimalNumber $number) { return DecimalNumber::_get(bcmul($this->number, $number, $this->precision), $this->precision); } /** * Perform a division operation * @param DecimalNumber $number Divisor * @return DecimalNumber Quotient */ public function quotient(DecimalNumber $number) { return DecimalNumber::_get(bcdiv($this->number, $number, $this->precision), $this->precision); } public function floored() { return DecimalNumber::_get(bcadd(bcadd($this->number, '0'), '0', $this->precision), $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $a = DecimalNumber::get('.7'); $b = DecimalNumber::get('.1'); $c = $a->sum($b); $d = $c->product(DecimalNumber::get('10')); echo $d;
Output for 5.3.3 - 5.3.29, 5.5.17, 5.6.0 - 5.6.38, 7.0.0 - 7.0.31, 7.1.0 - 7.1.23, 7.2.0 - 7.2.33, 7.3.12 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
8.0000
Output for 7.3.32 - 7.3.33, 7.4.26, 7.4.33, 8.0.13
Fatal error: Uncaught Error: Call to undefined function bcadd() in /in/XCZAT:24 Stack trace: #0 /in/XCZAT(100): DecimalNumber::get('.7') #1 {main} thrown in /in/XCZAT on line 24
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.16, 5.5.18 - 5.5.38
Fatal error: Call to undefined function bcadd() in /in/XCZAT on line 24
Process exited with code 255.
Output for 5.3.0 - 5.3.2
Strict Standards: Redefining already defined constructor for class DecimalNumber in /in/XCZAT on line 45 8.0000
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STATIC in /in/XCZAT on line 23
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STATIC in /in/XCZAT on line 23
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/XCZAT on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/XCZAT on line 9
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/XCZAT on line 9
Process exited with code 255.

preferences:
300.19 ms | 401 KiB | 417 Q