Skip to content

Commit

Permalink
Tweak for AMORDEGRC
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman committed Sep 7, 2024
1 parent e1dae99 commit a25775a
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions src/PhpSpreadsheet/Calculation/Financial/Amortization.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class Amortization
{
private const ROUNDING_ADJUSTMENT = (PHP_VERSION_ID < 80400) ? 0 : 1e-14;

/**
* AMORDEGRC.
*
Expand Down Expand Up @@ -82,7 +80,7 @@ public static function AMORDEGRC(
$amortiseCoeff = self::getAmortizationCoefficient($rate);

$rate *= $amortiseCoeff;
$rate += self::ROUNDING_ADJUSTMENT;
$rate = (float) (string) $rate; // ugly way to avoid rounding problem
$fNRate = round($yearFrac * $rate * $cost, 0);
$cost -= $fNRate;
$fRest = $cost - $salvage;
Expand Down

0 comments on commit a25775a

Please sign in to comment.