RESAZIP-PC\resaz

add daitomath

......@@ -49,4 +49,44 @@ Then projects using this package can update to the new stable version:
composer require daito/lib:^1.0
# or
composer update daito/lib
```
## Performance note (DaitoMath)
`DaitoMath` uses `brick/math` for precise decimal calculations (financial-safe).
- It can run without extra PHP extensions.
- For better performance on production, enable one of:
- `ext-gmp` (recommended)
- `ext-bcmath`
- `brick/math` will automatically use the fastest available calculator at runtime.
Quick check:
```bash
php -m | grep -Ei "gmp|bcmath"
```
Install extensions if missing:
```bash
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install -y php-gmp php-bcmath
sudo systemctl restart php8.2-fpm || sudo systemctl restart apache2
```
```bash
# CentOS / RHEL / Rocky / AlmaLinux
sudo yum install -y php-gmp php-bcmath
sudo systemctl restart php-fpm || sudo systemctl restart httpd
```
```powershell
# Windows (php.ini)
# 1) Open php.ini
# 2) Enable these lines:
extension=gmp
extension=bcmath
# 3) Restart web server / PHP-FPM service
```
\ No newline at end of file
......
......@@ -8,7 +8,12 @@
}
},
"require": {
"brick/math": "^0.9 || ^0.10 || ^0.11 || ^0.13",
"php": "^7.3 || ^8.0",
"laravel/framework": "^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0"
},
"suggest": {
"ext-gmp": "Faster big-number calculations",
"ext-bcmath": "Faster decimal calculations when GMP is unavailable"
}
}
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f0e944fbfbea5c3b7cd02a9cd33e3cea",
"content-hash": "0c23b9705a5fe293b678d1bbc5020207",
"packages": [
{
"name": "brick/math",
......
<?php
namespace Daito\Lib;
use Brick\Math\BigDecimal;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\RoundingMode;
use RuntimeException;
class DaitoMath
{
const DEFAULT_SCALE = 12;
public static function add($left, $right, $scale = self::DEFAULT_SCALE)
{
$bigDecimal = self::toBigDecimal($left)->plus(self::toBigDecimal($right));
return self::normalize((string) $bigDecimal->toScale((int) $scale));
}
public static function sub($left, $right, $scale = self::DEFAULT_SCALE)
{
$bigDecimal = self::toBigDecimal($left)->minus(self::toBigDecimal($right));
return self::normalize((string) $bigDecimal->toScale((int) $scale));
}
public static function mul($left, $right, $scale = self::DEFAULT_SCALE)
{
$bigDecimal = self::toBigDecimal($left)->multipliedBy(self::toBigDecimal($right));
return self::normalize((string) $bigDecimal->toScale((int) $scale));
}
public static function div($left, $right, $scale = self::DEFAULT_SCALE)
{
$leftValue = self::toBigDecimal($left);
$rightValue = self::toBigDecimal($right);
if ($rightValue->isZero()) {
throw new RuntimeException('Can not divide by zero.');
}
try {
$bigDecimal = $leftValue->dividedBy($rightValue, (int) $scale, RoundingMode::HALF_UP);
} catch (DivisionByZeroException $exception) {
throw new RuntimeException('Can not divide by zero.');
}
return self::normalize((string) $bigDecimal);
}
public static function floor($value)
{
return (string) self::toBigDecimal($value)->toScale(0, RoundingMode::FLOOR);
}
public static function ceil($value)
{
return (string) self::toBigDecimal($value)->toScale(0, RoundingMode::CEILING);
}
public static function round($value, $scale = 0)
{
return self::normalize((string) self::toBigDecimal($value)->toScale((int) $scale, RoundingMode::HALF_UP));
}
public static function mulFloor($left, $right, $scale = self::DEFAULT_SCALE)
{
return self::floor(self::mul($left, $right, $scale));
}
private static function toBigDecimal($value)
{
if (is_int($value) || is_string($value)) {
return BigDecimal::of((string) $value);
}
if (is_float($value)) {
return BigDecimal::of(self::normalize(sprintf('%.14F', $value)));
}
throw new RuntimeException('DaitoMath only supports int, float, or numeric string.');
}
private static function normalize($number)
{
$number = (string) $number;
if (strpos($number, '.') === false) {
return $number;
}
$number = rtrim($number, '0');
$number = rtrim($number, '.');
if ($number === '-0') {
return '0';
}
return $number === '' ? '0' : $number;
}
}