Skip to content

Commit a25775a

Browse files
committed
Tweak for AMORDEGRC
Changes to Php8.4 floating point calculations caused failures in unit tests for ROUNDDOWN, ROUNDUP, and AMORDEGRC. These were addressed in a kludgey manner by PR #3897. Then someone reported a problem (not specifically related to Php8.4) with TRUNC. That was fixed by PR #4115, in which I applied the method used by TRUNC to ROUNDDOWN and ROUNDUP as well. The method used to fix these was to cast a floating point value to string and then cast it back to float again. It's a bit surprising that this works, but it seems effective for all our test cases, and is less kludgey than what had been done earlier. Missing from that PR was a similar change for AMORDEGRC. This PR applies that change to AMORDEGRC, which again passes the unit test suite for all releases of Php.
1 parent e1dae99 commit a25775a

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

src/PhpSpreadsheet/Calculation/Financial/Amortization.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
class Amortization
1111
{
12-
private const ROUNDING_ADJUSTMENT = (PHP_VERSION_ID < 80400) ? 0 : 1e-14;
13-
1412
/**
1513
* AMORDEGRC.
1614
*
@@ -82,7 +80,7 @@ public static function AMORDEGRC(
8280
$amortiseCoeff = self::getAmortizationCoefficient($rate);
8381

8482
$rate *= $amortiseCoeff;
85-
$rate += self::ROUNDING_ADJUSTMENT;
83+
$rate = (float) (string) $rate; // ugly way to avoid rounding problem
8684
$fNRate = round($yearFrac * $rate * $cost, 0);
8785
$cost -= $fNRate;
8886
$fRest = $cost - $salvage;

0 commit comments

Comments
 (0)