Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #61 from precariouspanther/ISSUE-60-float-innerlog…
Browse files Browse the repository at this point in the history
…10-div-zero

ISSUE-60: Fix for division by zero caused by float precision
  • Loading branch information
Andrés Correa Casablanca committed Aug 31, 2017
2 parents edea342 + 86cc13c commit 9b90b32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Decimal.php
Expand Up @@ -1203,6 +1203,7 @@ private static function innerLog10(string $value, int $in_scale, int $out_scale)
switch ($cmp) {
case 1:
$value_log10_approx = $value_len - ($in_scale > 0 ? ($in_scale+2) : 1);
$value_log10_approx = max(0, $value_log10_approx);

return \bcadd(
(string)$value_log10_approx,
Expand Down
27 changes: 27 additions & 0 deletions tests/regression/issue60Test.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Litipk\BigNumbers\Decimal;
use PHPUnit\Framework\TestCase;

class issue60Test extends TestCase
{
public function test_that_fromFloat_division_does_not_calculate_invalid_log10_avoiding_div_zero()
{
$value = Decimal::fromFloat(1.001);
$divisor = Decimal::fromFloat(20);

$this->assertEquals(0.05005, $value->div($divisor)->asFloat());
$this->assertEquals(0.000434077479319, $value->log10()->asFloat());
}

public function test_that_fromFloat_less_than_1_still_correct()
{
$value = Decimal::fromFloat(0.175);
$divisor = Decimal::fromFloat(20);

$this->assertEquals(0.009, $value->div($divisor)->asFloat());
$this->assertEquals(-0.7569, $value->log10()->asFloat());
}
}

0 comments on commit 9b90b32

Please sign in to comment.