3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class BigDecimal { private $value; private $scale; function __construct(string $value){ if(preg_match('@^[0-9]*(?:.[0-9]*)?$@', $value) !== 1){ throw new Error("Invalid BigDecimal value $value"); } $this->value = $value; $this->scale = $this->getScaleOf($value); echo "scale of $value is " . $this->scale . "\n"; } private function getScale(BigDecimal $other){ return max($this->scale, $other->scale); } private function getScaleOf(string $value){ $pos = strrpos($value, '.'); if($pos === false){ return 0; } return strlen($value) - $pos - 1; } function valueEquals($other){ } function add(BigDecimal $other){ return new self(bcadd($this, $other, $this->getScale($other))); } function div(BigDecimal $other){ return new self(bcdiv($this, $other, $this->getScale($other))); } function mod(BigDecimal $other){ // not sure if mod or rem return new self(bcmod($this, $other, $this->getScale($other))); } function mul(BigDecimal $other){ return new self(bcmul($this, $other, $this->getScale($other))); } function pow(BigDecimal $other){ return new self(bcpow($this, $other, $this->getScale($other))); } function sub(BigDecimal $other){ return new self(bcsub($this, $other, $this->getScale($other))); } function __toString(){ return $this->value; } } function d(string $value){ return new BigDecimal($value); } echo d('10.123')->sub(d('5.02'));
based on mIH01
Output for 7.3.0alpha1 - 7.3.0beta1
scale of 10.123 is 3 scale of 5.02 is 2 Fatal error: Uncaught Error: Call to undefined function bcsub() in /in/4MecV:53 Stack trace: #0 /in/4MecV(65): BigDecimal->sub(Object(BigDecimal)) #1 {main} thrown in /in/4MecV on line 53
Process exited with code 255.
Output for 7.0.0 - 7.2.11
scale of 10.123 is 3 scale of 5.02 is 2 scale of 5.103 is 3 5.103
Output for 5.6.0 - 5.6.28
Catchable fatal error: Argument 1 passed to d() must be an instance of string, string given, called in /in/4MecV on line 65 and defined in /in/4MecV on line 61
Process exited with code 255.