Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-zlib": "*",
"composer-runtime-api": "^2.2",
"async-aws/s3": "^2.6 || ^3.0",
"brick/math": "^0.12 || ^0.13 || ^0.14",
"brick/math": "^0.12 || ^0.13 || ^0.14 || ^0.15 || ^0.16 || ^0.17",
"coduo/php-humanizer": "^5.0",
"cuyz/valinor": "^2.4",
"doctrine/dbal": "^3.6 || ^4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"php": "~8.3.0 || ~8.4.0 || ~8.5.0",
"ext-json": "*",
"psr/clock": "^1.0",
"brick/math": "^0.12 || ^0.13 || ^0.14",
"brick/math": "^0.12 || ^0.13 || ^0.14 || ^0.15 || ^0.16 || ^0.17",
"composer-runtime-api": "^2.2",
"flow-php/telemetry": "self.version",
"flow-php/types": "self.version",
Expand Down
24 changes: 17 additions & 7 deletions src/core/etl/src/Flow/Calculator/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function add(int|float|string $a, int|float|string $b) : int|float
{
$result = BigDecimal::of((string) $a)->plus(BigDecimal::of((string) $b));

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}

Expand All @@ -39,9 +39,10 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n
{
try {
if ($scale === null && $rounding === null) {
$result = BigDecimal::of($a)->dividedBy(BigDecimal::of($b));
$aDecimal = BigDecimal::of((string) $a);
$result = $aDecimal->dividedBy(BigDecimal::of((string) $b), $aDecimal->getScale());

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}
}
Expand All @@ -63,7 +64,7 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n

$result = BigDecimal::of((string) $a)->dividedBy(BigDecimal::of((string) $b), $scale, $brickMode);

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}

Expand Down Expand Up @@ -92,7 +93,7 @@ public function multiply(int|float|string $a, int|float|string $b) : int|float
{
$result = BigDecimal::of((string) $a)->multipliedBy(BigDecimal::of((string) $b));

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}

Expand All @@ -107,7 +108,7 @@ public function power(int|float|string $a, int|string $b) : int|float
{
$result = BigDecimal::of((string) $a)->power(BigInteger::of((string) $b)->toInt());

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}

Expand All @@ -122,10 +123,19 @@ public function subtract(int|float|string $a, int|float|string $b) : int|float
{
$result = BigDecimal::of((string) $a)->minus(BigDecimal::of((string) $b));

if (!$result->hasNonZeroFractionalPart()) {
if (!self::hasNonZeroFractionalPart($result)) {
return $result->toInt();
}

return $result->toFloat();
}

private static function hasNonZeroFractionalPart(BigDecimal $result) : bool
{
if (method_exists($result, 'hasNonZeroFractionalPart')) { // @phpstan-ignore function.alreadyNarrowedType
return $result->hasNonZeroFractionalPart();
}

return !$result->getFractionalPart()->isZero(); // @phpstan-ignore method.nonObject
}
}
Loading