CalcEngine: Remove the need to specify base/radix when working with Rational values (#31)

- Separates values from the representation (base/radix) of those values.
- Uses a single base for all values represented as Rationals.
- Rationals are converted to/from a specific base when they are converted to/from strings.
This commit is contained in:
Josh Koon 2019-02-22 10:00:19 -08:00 committed by GitHub
parent 47f9996fa9
commit 73372283a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 146 additions and 145 deletions

View File

@ -307,7 +307,7 @@ Rational CalcInput::ToRational(uint32_t radix, int32_t precision)
PRAT rat = StringToRat(m_base.IsNegative(), m_base.value, m_exponent.IsNegative(), m_exponent.value, radix, precision); PRAT rat = StringToRat(m_base.IsNegative(), m_base.value, m_exponent.IsNegative(), m_exponent.value, radix, precision);
if (rat == nullptr) if (rat == nullptr)
{ {
return Rational{}; return 0;
} }
Rational result{ rat }; Rational result{ rat };

View File

@ -50,12 +50,12 @@ namespace CalcEngine
destroyrat(pr); destroyrat(pr);
} }
Rational::Rational(uint64_t ui, uint32_t radix, int32_t precision) Rational::Rational(uint64_t ui, int32_t precision)
{ {
uint32_t hi = HIDWORD(ui); uint32_t hi = HIDWORD(ui);
uint32_t lo = LODWORD(ui); uint32_t lo = LODWORD(ui);
Rational temp = Rational{ hi }.Lsh(32, radix, precision).Or(lo, radix, precision); Rational temp = Rational{ hi }.Lsh(32, precision).Or(lo, precision);
m_p = Number{ temp.P() }; m_p = Number{ temp.P() };
m_q = Number{ temp.Q() }; m_q = Number{ temp.Q() };
@ -206,14 +206,14 @@ namespace CalcEngine
return result; return result;
} }
Rational Rational::Lsh(Rational const& rhs, uint32_t radix, int32_t precision) const Rational Rational::Lsh(Rational const& rhs, int32_t precision) const
{ {
PRAT lhsRat = this->ToPRAT(); PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT(); PRAT rhsRat = rhs.ToPRAT();
try try
{ {
lshrat(&lhsRat, rhsRat, radix, precision); lshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
destroyrat(rhsRat); destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
@ -229,14 +229,14 @@ namespace CalcEngine
return result; return result;
} }
Rational Rational::Rsh(Rational const& rhs, uint32_t radix, int32_t precision) const Rational Rational::Rsh(Rational const& rhs, int32_t precision) const
{ {
PRAT lhsRat = this->ToPRAT(); PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT(); PRAT rhsRat = rhs.ToPRAT();
try try
{ {
rshrat(&lhsRat, rhsRat, radix, precision); rshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
destroyrat(rhsRat); destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
@ -252,32 +252,19 @@ namespace CalcEngine
return result; return result;
} }
Rational Rational::Not(bool isIntegerMode, Rational const& chopNum, uint32_t radix, int32_t precision) const Rational Rational::Not(Rational const& chopNum, int32_t precision) const
{ {
Rational result{}; return this->Xor(chopNum, precision);
if (radix == 10 && !isIntegerMode)
{
result = RationalMath::Integer(*this, radix, precision);
result = result.Add(1, precision);
result = result.Negate();
}
else
{
result = this->Xor(chopNum, radix, precision);
}
return result;
} }
Rational Rational::And(Rational const& rhs, uint32_t radix, int32_t precision) const Rational Rational::And(Rational const& rhs, int32_t precision) const
{ {
PRAT lhsRat = this->ToPRAT(); PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT(); PRAT rhsRat = rhs.ToPRAT();
try try
{ {
andrat(&lhsRat, rhsRat, radix, precision); andrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
destroyrat(rhsRat); destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
@ -293,13 +280,13 @@ namespace CalcEngine
return result; return result;
} }
Rational Rational::Or(Rational const& rhs, uint32_t radix, int32_t precision) const Rational Rational::Or(Rational const& rhs, int32_t precision) const
{ {
PRAT lhsRat = this->ToPRAT(); PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT(); PRAT rhsRat = rhs.ToPRAT();
try try
{ {
orrat(&lhsRat, rhsRat, radix, precision); orrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
destroyrat(rhsRat); destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
@ -315,13 +302,13 @@ namespace CalcEngine
return result; return result;
} }
Rational Rational::Xor(Rational const& rhs, uint32_t radix, int32_t precision) const Rational Rational::Xor(Rational const& rhs, int32_t precision) const
{ {
PRAT lhsRat = this->ToPRAT(); PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT(); PRAT rhsRat = rhs.ToPRAT();
try try
{ {
xorrat(&lhsRat, rhsRat, radix, precision); xorrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
destroyrat(rhsRat); destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
@ -454,13 +441,13 @@ namespace CalcEngine
return result; return result;
} }
uint64_t Rational::ToUInt64_t(uint32_t radix, int32_t precision) const uint64_t Rational::ToUInt64_t(int32_t precision) const
{ {
PRAT rat = this->ToPRAT(); PRAT rat = this->ToPRAT();
uint64_t result; uint64_t result;
try try
{ {
result = rattoUlonglong(rat, radix, precision); result = rattoUlonglong(rat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {

View File

@ -95,7 +95,7 @@ CCalcEngine::CCalcEngine(bool fPrecedence, bool fIntegerMode, CalculationManager
m_dwWordBitWidth = DwWordBitWidthFromeNumWidth(m_numwidth); m_dwWordBitWidth = DwWordBitWidthFromeNumWidth(m_numwidth);
m_maxTrigonometricNum = RationalMath::Pow(10, 100, m_radix, m_precision); m_maxTrigonometricNum = RationalMath::Pow(10, 100, m_precision);
SetRadixTypeAndNumWidth(DEC_RADIX, m_numwidth); SetRadixTypeAndNumWidth(DEC_RADIX, m_numwidth);
SettingsChanged(); SettingsChanged();
@ -118,7 +118,7 @@ void CCalcEngine::InitChopNumbers()
for (size_t i = 0; i < m_chopNumbers.size(); i++) for (size_t i = 0; i < m_chopNumbers.size(); i++)
{ {
auto maxVal = m_chopNumbers[i].Div(2, m_precision); auto maxVal = m_chopNumbers[i].Div(2, m_precision);
maxVal = RationalMath::Integer(maxVal, m_radix, m_precision); maxVal = RationalMath::Integer(maxVal, m_precision);
m_maxDecimalValueStrings[i] = maxVal.ToString(10, FMT_FLOAT, m_precision); m_maxDecimalValueStrings[i] = maxVal.ToString(10, FMT_FLOAT, m_precision);
} }

View File

@ -1048,12 +1048,12 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
try try
{ {
uint64_t w64Bits = tempRat.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = tempRat.ToUInt64_t(m_precision);
bool fMsb = ((w64Bits >> (m_dwWordBitWidth - 1)) & 1); bool fMsb = ((w64Bits >> (m_dwWordBitWidth - 1)) & 1);
if ((radix == 10) && fMsb) if ((radix == 10) && fMsb)
{ {
// If high bit is set, then get the decimal number in negative 2's compl form. // If high bit is set, then get the decimal number in negative 2's compl form.
tempRat = tempRat.Not(true, m_chopNumbers[m_numwidth], m_radix, m_precision); tempRat = tempRat.Not(m_chopNumbers[m_numwidth], m_precision);
tempRat = tempRat.Add(1, m_precision); tempRat = tempRat.Add(1, m_precision);
tempRat = tempRat.Negate(); tempRat = tempRat.Negate();
} }

View File

@ -57,7 +57,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
} }
// Truncate to an integer. Do not round here. // Truncate to an integer. Do not round here.
auto result = RationalMath::Integer(rat, m_radix, m_precision); auto result = RationalMath::Integer(rat, m_precision);
// Can be converting a dec negative number to Hex/Oct/Bin rep. Use 2's complement form // Can be converting a dec negative number to Hex/Oct/Bin rep. Use 2's complement form
// Check the range. // Check the range.
@ -66,10 +66,10 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
// if negative make positive by doing a twos complement // if negative make positive by doing a twos complement
result = result.Negate(); result = result.Negate();
result = result.Sub(1, m_precision); result = result.Sub(1, m_precision);
result = result.Not(true /* IntegerMode */, m_chopNumbers[m_numwidth], m_radix, m_precision); result = result.Not(m_chopNumbers[m_numwidth], m_precision);
} }
result = result.And(m_chopNumbers[m_numwidth], m_radix, m_precision); result = result.And(m_chopNumbers[m_numwidth], m_precision);
return result; return result;
} }

View File

@ -32,26 +32,35 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
switch (op) switch (op)
{ {
case IDC_CHOP: case IDC_CHOP:
result = m_bInv ? Frac(rat, m_radix, m_precision) : Integer(rat, m_radix, m_precision); result = m_bInv ? Frac(rat, m_precision) : Integer(rat, m_precision);
break; break;
/* Return complement. */ /* Return complement. */
case IDC_COM: case IDC_COM:
result = rat.Not(m_fIntegerMode, m_chopNumbers[m_numwidth], m_radix, m_precision); if (m_radix == 10 && !m_fIntegerMode)
{
result = RationalMath::Integer(rat, m_precision);
result = result.Add(1, m_precision);
result = result.Negate();
}
else
{
result = rat.Xor(m_chopNumbers[m_numwidth], m_precision);
}
break; break;
// Rotate Left with hi bit wrapped over to lo bit // Rotate Left with hi bit wrapped over to lo bit
case IDC_ROL: case IDC_ROL:
if (m_fIntegerMode) if (m_fIntegerMode)
{ {
result = Integer(rat, m_radix, m_precision); result = Integer(rat, m_precision);
uint64_t w64Bits = result.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = result.ToUInt64_t(m_precision);
uint64_t msb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; uint64_t msb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
w64Bits <<= 1; // LShift by 1 w64Bits <<= 1; // LShift by 1
w64Bits |= msb; // Set the prev Msb as the current Lsb w64Bits |= msb; // Set the prev Msb as the current Lsb
result = Rational{ w64Bits, m_radix, m_precision }; result = Rational{ w64Bits, m_precision };
} }
break; break;
@ -59,14 +68,14 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
case IDC_ROR: case IDC_ROR:
if (m_fIntegerMode) if (m_fIntegerMode)
{ {
result = Integer(rat, m_radix, m_precision); result = Integer(rat, m_precision);
uint64_t w64Bits = result.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = result.ToUInt64_t(m_precision);
uint64_t lsb = ((w64Bits & 0x01) == 1) ? 1 : 0; uint64_t lsb = ((w64Bits & 0x01) == 1) ? 1 : 0;
w64Bits >>= 1; //RShift by 1 w64Bits >>= 1; //RShift by 1
w64Bits |= (lsb << (m_dwWordBitWidth - 1)); w64Bits |= (lsb << (m_dwWordBitWidth - 1));
result = Rational{ w64Bits, m_radix, m_precision }; result = Rational{ w64Bits, m_precision };
} }
break; break;
@ -89,42 +98,42 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
case IDC_SIN: /* Sine; normal and arc */ case IDC_SIN: /* Sine; normal and arc */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ASin(rat, m_angletype, m_radix, m_precision) : Sin(rat, m_angletype, m_radix, m_precision); result = m_bInv ? ASin(rat, m_angletype, m_precision) : Sin(rat, m_angletype, m_precision);
} }
break; break;
case IDC_SINH: /* Sine- hyperbolic and archyperbolic */ case IDC_SINH: /* Sine- hyperbolic and archyperbolic */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ASinh(rat, m_radix, m_precision) : Sinh(rat, m_radix, m_precision); result = m_bInv ? ASinh(rat, m_precision) : Sinh(rat, m_precision);
} }
break; break;
case IDC_COS: /* Cosine, follows convention of sine function. */ case IDC_COS: /* Cosine, follows convention of sine function. */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ACos(rat, m_angletype, m_radix, m_precision) : Cos(rat, m_angletype, m_radix, m_precision); result = m_bInv ? ACos(rat, m_angletype, m_precision) : Cos(rat, m_angletype, m_precision);
} }
break; break;
case IDC_COSH: /* Cosine hyperbolic, follows convention of sine h function. */ case IDC_COSH: /* Cosine hyperbolic, follows convention of sine h function. */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ACosh(rat, m_radix, m_precision) : Cosh(rat, m_radix, m_precision); result = m_bInv ? ACosh(rat, m_precision) : Cosh(rat, m_precision);
} }
break; break;
case IDC_TAN: /* Same as sine and cosine. */ case IDC_TAN: /* Same as sine and cosine. */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ATan(rat, m_angletype, m_radix, m_precision) : Tan(rat, m_angletype, m_radix, m_precision); result = m_bInv ? ATan(rat, m_angletype, m_precision) : Tan(rat, m_angletype, m_precision);
} }
break; break;
case IDC_TANH: /* Same as sine h and cosine h. */ case IDC_TANH: /* Same as sine h and cosine h. */
if (!m_fIntegerMode) if (!m_fIntegerMode)
{ {
result = m_bInv ? ATanh(rat, m_precision) : Tanh(rat, m_radix, m_precision); result = m_bInv ? ATanh(rat, m_precision) : Tanh(rat, m_precision);
} }
break; break;
@ -133,16 +142,16 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
break; break;
case IDC_SQR: /* Square */ case IDC_SQR: /* Square */
result = Pow(rat, 2, m_radix, m_precision); result = Pow(rat, 2, m_precision);
break; break;
case IDC_SQRT: /* Square Root */ case IDC_SQRT: /* Square Root */
result = Root(rat, 2, m_radix, m_precision); result = Root(rat, 2, m_precision);
break; break;
case IDC_CUBEROOT: case IDC_CUBEROOT:
case IDC_CUB: /* Cubing and cube root functions. */ case IDC_CUB: /* Cubing and cube root functions. */
result = IDC_CUBEROOT == op ? Root(rat, 3, m_radix, m_precision) : Pow(rat, 3, m_radix, m_precision); result = IDC_CUBEROOT == op ? Root(rat, 3, m_precision) : Pow(rat, 3, m_precision);
break; break;
case IDC_LOG: /* Functions for common log. */ case IDC_LOG: /* Functions for common log. */
@ -150,15 +159,15 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
break; break;
case IDC_POW10: case IDC_POW10:
result = Pow(10, rat, m_radix, m_precision); result = Pow(10, rat, m_precision);
break; break;
case IDC_LN: /* Functions for natural log. */ case IDC_LN: /* Functions for natural log. */
result = m_bInv ? Exp(rat, m_radix, m_precision) : Log(rat, m_precision); result = m_bInv ? Exp(rat, m_precision) : Log(rat, m_precision);
break; break;
case IDC_FAC: /* Calculate factorial. Inverse is ineffective. */ case IDC_FAC: /* Calculate factorial. Inverse is ineffective. */
result = Fact(rat, m_radix, m_precision); result = Fact(rat, m_precision);
break; break;
case IDC_DEGREES: case IDC_DEGREES:
@ -173,14 +182,14 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
{ {
Rational shftRat{ m_bInv ? 100 : 60 }; Rational shftRat{ m_bInv ? 100 : 60 };
Rational degreeRat = Integer(rat, m_radix, m_precision); Rational degreeRat = Integer(rat, m_precision);
Rational minuteRat = rat.Sub(degreeRat, m_precision); Rational minuteRat = rat.Sub(degreeRat, m_precision);
minuteRat = minuteRat.Mul(shftRat, m_precision); minuteRat = minuteRat.Mul(shftRat, m_precision);
Rational secondRat = minuteRat; Rational secondRat = minuteRat;
minuteRat = Integer(minuteRat, m_radix, m_precision); minuteRat = Integer(minuteRat, m_precision);
secondRat = secondRat.Sub(minuteRat, m_precision); secondRat = secondRat.Sub(minuteRat, m_precision);
secondRat = secondRat.Mul(shftRat, m_precision); secondRat = secondRat.Mul(shftRat, m_precision);

View File

@ -8,12 +8,12 @@
using namespace std; using namespace std;
using namespace CalcEngine; using namespace CalcEngine;
Rational RationalMath::Frac(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Frac(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
fracrat(&prat, radix, precision); fracrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -27,12 +27,12 @@ Rational RationalMath::Frac(Rational const& rat, uint32_t radix, int32_t precisi
return result; return result;
} }
Rational RationalMath::Integer(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Integer(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
intrat(&prat, radix, precision); intrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -46,14 +46,14 @@ Rational RationalMath::Integer(Rational const& rat, uint32_t radix, int32_t prec
return result; return result;
} }
Rational RationalMath::Pow(Rational const& base, Rational const& pow, uint32_t radix, int32_t precision) Rational RationalMath::Pow(Rational const& base, Rational const& pow, int32_t precision)
{ {
PRAT baseRat = base.ToPRAT(); PRAT baseRat = base.ToPRAT();
PRAT powRat = pow.ToPRAT(); PRAT powRat = pow.ToPRAT();
try try
{ {
powrat(&baseRat, powRat, radix, precision); powrat(&baseRat, powRat, RATIONAL_BASE, precision);
destroyrat(powRat); destroyrat(powRat);
} }
catch (DWORD error) catch (DWORD error)
@ -69,18 +69,18 @@ Rational RationalMath::Pow(Rational const& base, Rational const& pow, uint32_t r
return result; return result;
} }
Rational RationalMath::Root(Rational const& base, Rational const& root, uint32_t radix, int32_t precision) Rational RationalMath::Root(Rational const& base, Rational const& root, int32_t precision)
{ {
return Pow(base, Invert(root, precision), radix, precision); return Pow(base, Invert(root, precision), precision);
} }
Rational RationalMath::Fact(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Fact(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
factrat(&prat, radix, precision); factrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -94,13 +94,13 @@ Rational RationalMath::Fact(Rational const& rat, uint32_t radix, int32_t precisi
return result; return result;
} }
Rational RationalMath::Exp(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Exp(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
exprat(&prat, radix, precision); exprat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -149,13 +149,13 @@ Rational RationalMath::Abs(Rational const& rat)
return Rational{ Number{ 1, rat.P().Exp(), rat.P().Mantissa() }, Number{ 1, rat.Q().Exp(), rat.Q().Mantissa() } }; return Rational{ Number{ 1, rat.P().Exp(), rat.P().Mantissa() }, Number{ 1, rat.Q().Exp(), rat.Q().Mantissa() } };
} }
Rational RationalMath::Sin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::Sin(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
sinanglerat(&prat, angletype, radix, precision); sinanglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -169,13 +169,13 @@ Rational RationalMath::Sin(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
return result; return result;
} }
Rational RationalMath::Cos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::Cos(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
cosanglerat(&prat, angletype, radix, precision); cosanglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -189,13 +189,13 @@ Rational RationalMath::Cos(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
return result; return result;
} }
Rational RationalMath::Tan(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::Tan(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
tananglerat(&prat, angletype, radix, precision); tananglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -209,13 +209,13 @@ Rational RationalMath::Tan(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
return result; return result;
} }
Rational RationalMath::ASin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::ASin(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
asinanglerat(&prat, angletype, radix, precision); asinanglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -229,13 +229,13 @@ Rational RationalMath::ASin(Rational const& rat, ANGLE_TYPE angletype, uint32_t
return result; return result;
} }
Rational RationalMath::ACos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::ACos(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
acosanglerat(&prat, angletype, radix, precision); acosanglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -249,13 +249,13 @@ Rational RationalMath::ACos(Rational const& rat, ANGLE_TYPE angletype, uint32_t
return result; return result;
} }
Rational RationalMath::ATan(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision) Rational RationalMath::ATan(Rational const& rat, ANGLE_TYPE angletype, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
atananglerat(&prat, angletype, radix, precision); atananglerat(&prat, angletype, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -269,13 +269,13 @@ Rational RationalMath::ATan(Rational const& rat, ANGLE_TYPE angletype, uint32_t
return result; return result;
} }
Rational RationalMath::Sinh(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Sinh(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
sinhrat(&prat, radix, precision); sinhrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -289,13 +289,13 @@ Rational RationalMath::Sinh(Rational const& rat, uint32_t radix, int32_t precisi
return result; return result;
} }
Rational RationalMath::Cosh(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Cosh(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
coshrat(&prat, radix, precision); coshrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -309,13 +309,13 @@ Rational RationalMath::Cosh(Rational const& rat, uint32_t radix, int32_t precisi
return result; return result;
} }
Rational RationalMath::Tanh(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::Tanh(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
tanhrat(&prat, radix, precision); tanhrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -329,13 +329,13 @@ Rational RationalMath::Tanh(Rational const& rat, uint32_t radix, int32_t precisi
return result; return result;
} }
Rational RationalMath::ASinh(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::ASinh(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
asinhrat(&prat, radix, precision); asinhrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
@ -349,13 +349,13 @@ Rational RationalMath::ASinh(Rational const& rat, uint32_t radix, int32_t precis
return result; return result;
} }
Rational RationalMath::ACosh(Rational const& rat, uint32_t radix, int32_t precision) Rational RationalMath::ACosh(Rational const& rat, int32_t precision)
{ {
PRAT prat = rat.ToPRAT(); PRAT prat = rat.ToPRAT();
try try
{ {
acoshrat(&prat, radix, precision); acoshrat(&prat, RATIONAL_BASE, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {

View File

@ -18,15 +18,15 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
switch (operation) switch (operation)
{ {
case IDC_AND: case IDC_AND:
result = result.And(rhs, m_radix, m_precision); result = result.And(rhs, m_precision);
break; break;
case IDC_OR: case IDC_OR:
result = result.Or(rhs, m_radix, m_precision); result = result.Or(rhs, m_precision);
break; break;
case IDC_XOR: case IDC_XOR:
result = result.Xor(rhs, m_radix, m_precision); result = result.Xor(rhs, m_precision);
break; break;
case IDC_RSHF: case IDC_RSHF:
@ -36,21 +36,21 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
throw CALC_E_NORESULT; throw CALC_E_NORESULT;
} }
uint64_t w64Bits = rhs.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = rhs.ToUInt64_t(m_precision);
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
Rational holdVal = result; Rational holdVal = result;
result = rhs.Rsh(holdVal, m_radix, m_precision); result = rhs.Rsh(holdVal, m_precision);
if (fMsb) if (fMsb)
{ {
result = Integer(result, m_radix, m_precision); result = Integer(result, m_precision);
auto tempRat = m_chopNumbers[m_numwidth].Rsh(holdVal, m_radix, m_precision); auto tempRat = m_chopNumbers[m_numwidth].Rsh(holdVal, m_precision);
tempRat = Integer(tempRat, m_radix, m_precision); tempRat = Integer(tempRat, m_precision);
tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_radix, m_precision); tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_precision);
result = result.Or(tempRat, m_radix, m_precision); result = result.Or(tempRat, m_precision);
} }
break; break;
} }
@ -61,7 +61,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
throw CALC_E_NORESULT; throw CALC_E_NORESULT;
} }
result = rhs.Lsh(result, m_radix, m_precision); result = rhs.Lsh(result, m_precision);
break; break;
case IDC_ADD: case IDC_ADD:
@ -85,23 +85,23 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (m_fIntegerMode) if (m_fIntegerMode)
{ {
uint64_t w64Bits = rhs.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = rhs.ToUInt64_t(m_precision);
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
if (fMsb) if (fMsb)
{ {
result = rhs.Not(true /* IntegerMode */, m_chopNumbers[m_numwidth], m_radix, m_precision); result = rhs.Not(m_chopNumbers[m_numwidth], m_precision);
result = result.Add(1, m_precision); result = result.Add(1, m_precision);
iNumeratorSign = -1; iNumeratorSign = -1;
} }
w64Bits = temp.ToUInt64_t(m_radix, m_precision); w64Bits = temp.ToUInt64_t(m_precision);
fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
if (fMsb) if (fMsb)
{ {
temp = temp.Not(true /* IntegerMode */, m_chopNumbers[m_numwidth], m_radix, m_precision); temp = temp.Not(m_chopNumbers[m_numwidth], m_precision);
temp = temp.Add(1, m_precision); temp = temp.Add(1, m_precision);
iDenominatorSign = -1; iDenominatorSign = -1;
@ -121,18 +121,18 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (m_fIntegerMode && iFinalSign == -1) if (m_fIntegerMode && iFinalSign == -1)
{ {
result = Integer(result, m_radix, m_precision).Negate(); result = Integer(result, m_precision).Negate();
} }
break; break;
} }
case IDC_PWR: // Calculates rhs to the result(th) power. case IDC_PWR: // Calculates rhs to the result(th) power.
result = Pow(rhs, result, m_radix, m_precision); result = Pow(rhs, result, m_precision);
break; break;
case IDC_ROOT: // Calculates rhs to the result(th) root. case IDC_ROOT: // Calculates rhs to the result(th) root.
result = Root(rhs, result, m_radix, m_precision); result = Root(rhs, result, m_precision);
break; break;
} }
} }

View File

@ -18,13 +18,13 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RADIX_TYPE radixtype, NUM_WIDTH numwid
// back to 1111,1111,1000,0001 when in Word mode. // back to 1111,1111,1000,0001 when in Word mode.
if (m_fIntegerMode) if (m_fIntegerMode)
{ {
uint64_t w64Bits = m_currentVal.ToUInt64_t(m_radix, m_precision); uint64_t w64Bits = m_currentVal.ToUInt64_t(m_precision);
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; // make sure you use the old width bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; // make sure you use the old width
if (fMsb) if (fMsb)
{ {
// If high bit is set, then get the decimal number in -ve 2'scompl form. // If high bit is set, then get the decimal number in -ve 2'scompl form.
auto tempResult = m_currentVal.Not(true /* IntegerMode */, m_chopNumbers[m_numwidth], m_radix, m_precision); auto tempResult = m_currentVal.Not(m_chopNumbers[m_numwidth], m_precision);
tempResult = tempResult.Add(1, m_precision); tempResult = tempResult.Add(1, m_precision);
m_currentVal = tempResult.Negate(); m_currentVal = tempResult.Negate();
@ -85,7 +85,7 @@ bool CCalcEngine::TryToggleBit(CalcEngine::Rational& rat, DWORD wbitno)
return false; // ignore error cant happen return false; // ignore error cant happen
} }
Rational result = Integer(rat, m_radix, m_precision); Rational result = Integer(rat, m_precision);
if (result.IsZero()) if (result.IsZero())
{ {
// This is the same work around happenning in SciCalcFunctions. Ought to move to intrat function itself. // This is the same work around happenning in SciCalcFunctions. Ought to move to intrat function itself.
@ -93,8 +93,8 @@ bool CCalcEngine::TryToggleBit(CalcEngine::Rational& rat, DWORD wbitno)
result = Rational{}; result = Rational{};
} }
auto pow = Pow(2, static_cast<int32_t>(wbitno), m_radix, m_precision); auto pow = Pow(2, static_cast<int32_t>(wbitno), m_precision);
rat = result.Xor(pow, m_radix, m_precision); rat = result.Xor(pow, m_precision);
return true; return true;
} }

View File

@ -6,6 +6,10 @@
namespace CalcEngine namespace CalcEngine
{ {
// Default Base/Radix to use for Rational calculations
// RatPack calculations currently support up to Base64.
inline constexpr uint32_t RATIONAL_BASE = 10;
class Rational class Rational
{ {
public: public:
@ -14,7 +18,7 @@ namespace CalcEngine
Rational(Number const& p, Number const& q) noexcept; Rational(Number const& p, Number const& q) noexcept;
Rational(int32_t i); Rational(int32_t i);
Rational(uint32_t ui); Rational(uint32_t ui);
Rational(uint64_t ui, uint32_t radix, int32_t precision); Rational(uint64_t ui, int32_t precision);
explicit Rational(PRAT prat) noexcept; explicit Rational(PRAT prat) noexcept;
PRAT ToPRAT() const; PRAT ToPRAT() const;
@ -29,13 +33,13 @@ namespace CalcEngine
Rational Div(Rational const& rhs, int32_t precision) const; Rational Div(Rational const& rhs, int32_t precision) const;
Rational Mod(Rational const& rhs) const; Rational Mod(Rational const& rhs) const;
Rational Lsh(Rational const& r, uint32_t radix, int32_t precision) const; Rational Lsh(Rational const& r, int32_t precision) const;
Rational Rsh(Rational const& r, uint32_t radix, int32_t precision) const; Rational Rsh(Rational const& r, int32_t precision) const;
Rational Not(bool isIntegerMode, Rational const& chopNum, uint32_t radix, int32_t precision) const; Rational Not(Rational const& chopNum, int32_t precision) const;
Rational And(Rational const& r, uint32_t radix, int32_t precision) const; Rational And(Rational const& r, int32_t precision) const;
Rational Or(Rational const& r, uint32_t radix, int32_t precision) const; Rational Or(Rational const& r, int32_t precision) const;
Rational Xor(Rational const& r, uint32_t radix, int32_t precision) const; Rational Xor(Rational const& r, int32_t precision) const;
bool IsZero() const; bool IsZero() const;
bool IsLess(Rational const& r, int32_t precision) const; bool IsLess(Rational const& r, int32_t precision) const;
@ -44,7 +48,7 @@ namespace CalcEngine
bool IsEq(Rational const& r, int32_t precision) const; bool IsEq(Rational const& r, int32_t precision) const;
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const; std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
uint64_t ToUInt64_t(uint32_t radix, int32_t precision) const; uint64_t ToUInt64_t(int32_t precision) const;
private: private:
Number m_p; Number m_p;

View File

@ -5,31 +5,31 @@
namespace CalcEngine::RationalMath namespace CalcEngine::RationalMath
{ {
Rational Frac(Rational const& rat, uint32_t radix, int32_t precision); Rational Frac(Rational const& rat, int32_t precision);
Rational Integer(Rational const& rat, uint32_t radix, int32_t precision); Rational Integer(Rational const& rat, int32_t precision);
Rational Pow(Rational const& base, Rational const& pow, uint32_t radix, int32_t precision); Rational Pow(Rational const& base, Rational const& pow, int32_t precision);
Rational Root(Rational const& base, Rational const& root, uint32_t radix, int32_t precision); Rational Root(Rational const& base, Rational const& root, int32_t precision);
Rational Fact(Rational const& rat, uint32_t radix, int32_t precision); Rational Fact(Rational const& rat, int32_t precision);
Rational Exp(Rational const& rat, uint32_t radix, int32_t precision); Rational Exp(Rational const& rat, int32_t precision);
Rational Log(Rational const& rat, int32_t precision); Rational Log(Rational const& rat, int32_t precision);
Rational Log10(Rational const& rat, int32_t precision); Rational Log10(Rational const& rat, int32_t precision);
Rational Invert(Rational const& rat, int32_t precision); Rational Invert(Rational const& rat, int32_t precision);
Rational Abs(Rational const& rat); Rational Abs(Rational const& rat);
Rational Sin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational Sin(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational Cos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational Cos(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational Tan(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational Tan(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational ASin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational ASin(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational ACos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational ACos(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational ATan(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision); Rational ATan(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
Rational Sinh(Rational const& rat, uint32_t radix, int32_t precision); Rational Sinh(Rational const& rat, int32_t precision);
Rational Cosh(Rational const& rat, uint32_t radix, int32_t precision); Rational Cosh(Rational const& rat, int32_t precision);
Rational Tanh(Rational const& rat, uint32_t radix, int32_t precision); Rational Tanh(Rational const& rat, int32_t precision);
Rational ASinh(Rational const& rat, uint32_t radix, int32_t precision); Rational ASinh(Rational const& rat, int32_t precision);
Rational ACosh(Rational const& rat, uint32_t radix, int32_t precision); Rational ACosh(Rational const& rat, int32_t precision);
Rational ATanh(Rational const& rat, int32_t precision); Rational ATanh(Rational const& rat, int32_t precision);
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -347,6 +347,7 @@ PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix)
// mantissa a string representation of a number // mantissa a string representation of a number
// exponentIsNegative true if exponent is less than zero // exponentIsNegative true if exponent is less than zero
// exponent a string representation of a number // exponent a string representation of a number
// radix is the number base used in the source string
// //
// RETURN: PRAT representation of string input. // RETURN: PRAT representation of string input.
// Or nullptr if no number scanned. // Or nullptr if no number scanned.

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------