mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 22:03:11 -07:00
Fix wrong results after sqrt
This commit is contained in:
parent
248b762b80
commit
af50df2ec0
2 changed files with 40 additions and 0 deletions
|
@ -342,11 +342,32 @@ void powratNumeratorDenominator(PRAT *px, PRAT y, uint32_t radix, int32_t precis
|
|||
DUPRAT(originalResult, pxPow);
|
||||
powratcomp(&originalResult, oneoveryDenom, radix, precision);
|
||||
|
||||
// ##################################
|
||||
// Find number of digits after decimal point
|
||||
// and prepare multiplier for rounding
|
||||
// ##################################
|
||||
PRAT resultMultiplier = nullptr;
|
||||
PRAT resultDigits = nullptr;
|
||||
PNUMBER numResult = RatToNumber(pxPow, radix, precision);
|
||||
if (-numResult->exp < precision)
|
||||
{
|
||||
DUPRAT(resultDigits, longtorat(-numResult->exp / 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
DUPRAT(resultDigits, rat_zero);
|
||||
}
|
||||
DUPRAT(resultMultiplier, rat_ten);
|
||||
powratcomp(&resultMultiplier, resultDigits, radix, precision);
|
||||
destroyrat(resultDigits);
|
||||
destroynum(numResult);
|
||||
|
||||
// ##################################
|
||||
// Round the originalResult to roundedResult
|
||||
// ##################################
|
||||
PRAT roundedResult = nullptr;
|
||||
DUPRAT(roundedResult, originalResult);
|
||||
mulrat(&roundedResult, resultMultiplier, precision);
|
||||
if (roundedResult->pp->sign == -1)
|
||||
{
|
||||
subrat(&roundedResult, rat_half, precision);
|
||||
|
@ -356,6 +377,8 @@ void powratNumeratorDenominator(PRAT *px, PRAT y, uint32_t radix, int32_t precis
|
|||
addrat(&roundedResult, rat_half, precision);
|
||||
}
|
||||
intrat(&roundedResult, radix, precision);
|
||||
divrat(&roundedResult, resultMultiplier, precision);
|
||||
destroyrat(resultMultiplier);
|
||||
|
||||
// ##################################
|
||||
// Take the yDenom power of the roundedResult.
|
||||
|
|
|
@ -428,6 +428,23 @@ namespace CalculatorManagerTest
|
|||
Command::CommandSQRT, Command::CommandSUB, Command::Command3, Command::Command2,
|
||||
Command::CommandADD, Command::CommandNULL };
|
||||
TestDriver::Test(L"0", L"\x221A(1024) - 32 + ", commands22);
|
||||
|
||||
Command commands23[] = { Command::Command2, Command::CommandPNT, Command::Command2, Command::Command5,
|
||||
Command::CommandSQRT, Command::CommandSUB, Command::Command1, Command::CommandPNT, Command::Command5,
|
||||
Command::CommandADD, Command::CommandNULL };
|
||||
TestDriver::Test(L"0", L"\x221A(2.25) - 1.5 + ", commands23);
|
||||
|
||||
Command commands24[] = { Command::Command8, Command::Command7, Command::Command6, Command::Command5,
|
||||
Command::Command4, Command::Command3, Command::Command2, Command::Command1,
|
||||
Command::CommandPNT, Command::Command1, Command::Command2, Command::Command3, Command::Command4,
|
||||
Command::Command5, Command::Command6, Command::Command7, Command::Command8,
|
||||
Command::CommandSQR, Command::CommandSQRT, Command::CommandSUB,
|
||||
Command::Command8, Command::Command7, Command::Command6, Command::Command5,
|
||||
Command::Command4, Command::Command3, Command::Command2, Command::Command1,
|
||||
Command::CommandPNT, Command::Command1, Command::Command2, Command::Command3, Command::Command4,
|
||||
Command::Command5, Command::Command6, Command::Command7, Command::Command8,
|
||||
Command::CommandADD, Command::CommandNULL };
|
||||
TestDriver::Test(L"0", L"\x221A(sqr(87654321.12345678)) - 87654321.12345678 + ", commands24);
|
||||
}
|
||||
|
||||
void CalculatorManagerTest::CalculatorManagerTestScientific()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue