<?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;
}
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'));