Skip to content

Commit 4d60656

Browse files
committed
Fix exception message for BigDecimal::nthRoot() with scale 1
1 parent a8507be commit 4d60656

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/BigDecimal.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,13 @@ public function nthRoot(int $n, int $scale, RoundingMode $roundingMode = Roundin
736736
}
737737

738738
if ($n === 1) {
739-
return $this->toScale($scale, $roundingMode);
739+
$scaled = DecimalHelper::scale($this->value, $this->scale, $scale, $roundingMode);
740+
741+
if ($scaled === null) {
742+
throw RoundingNecessaryException::decimalNthRootScaleTooSmall();
743+
}
744+
745+
return new BigDecimal($scaled, $scale);
740746
}
741747

742748
if ($this->isZero()) {

tests/BigDecimalTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,15 +2524,19 @@ public static function providerNthRoot(): Generator
25242524
['0', 1, 4, RoundingMode::Unnecessary, '0.0000'],
25252525
['0.0', 1, 0, RoundingMode::Unnecessary, '0'],
25262526
['0.00', 1, 1, RoundingMode::Unnecessary, '0.0'],
2527+
['1.23', 1, 1, RoundingMode::Unnecessary, 'SCALE_TOO_SMALL'],
25272528
['1.23', 1, 1, RoundingMode::Down, '1.2'],
25282529
['1.23', 1, 1, RoundingMode::Up, '1.3'],
25292530
['1.23', 1, 1, RoundingMode::HalfUp, '1.2'],
2531+
['1.27', 1, 1, RoundingMode::Unnecessary, 'SCALE_TOO_SMALL'],
25302532
['1.27', 1, 1, RoundingMode::Down, '1.2'],
25312533
['1.27', 1, 1, RoundingMode::Up, '1.3'],
25322534
['1.27', 1, 1, RoundingMode::HalfUp, '1.3'],
2535+
['-1.23', 1, 1, RoundingMode::Unnecessary, 'SCALE_TOO_SMALL'],
25332536
['-1.23', 1, 1, RoundingMode::Down, '-1.2'],
25342537
['-1.23', 1, 1, RoundingMode::Up, '-1.3'],
25352538
['-1.23', 1, 1, RoundingMode::HalfUp, '-1.2'],
2539+
['-1.27', 1, 1, RoundingMode::Unnecessary, 'SCALE_TOO_SMALL'],
25362540
['-1.27', 1, 1, RoundingMode::Down, '-1.2'],
25372541
['-1.27', 1, 1, RoundingMode::Up, '-1.3'],
25382542
['-1.27', 1, 1, RoundingMode::HalfUp, '-1.3'],
@@ -2799,6 +2803,7 @@ public static function providerNthRootMidpointTies(): Generator
27992803
{
28002804
$tests = [
28012805
// n = 1 identity at a tie: 1.25 doesn't fit at scale 1; midpoint between 1.2 and 1.3.
2806+
['1.25', 1, 1, RoundingMode::Unnecessary, 'SCALE_TOO_SMALL'],
28022807
['1.25', 1, 1, RoundingMode::Down, '1.2'],
28032808
['1.25', 1, 1, RoundingMode::Up, '1.3'],
28042809
['1.25', 1, 1, RoundingMode::HalfUp, '1.3'],

0 commit comments

Comments
 (0)