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:
parent
47f9996fa9
commit
73372283a0
@ -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);
|
||||
if (rat == nullptr)
|
||||
{
|
||||
return Rational{};
|
||||
return 0;
|
||||
}
|
||||
|
||||
Rational result{ rat };
|
||||
|
@ -50,12 +50,12 @@ namespace CalcEngine
|
||||
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 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_q = Number{ temp.Q() };
|
||||
@ -206,14 +206,14 @@ namespace CalcEngine
|
||||
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 rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
lshrat(&lhsRat, rhsRat, radix, precision);
|
||||
lshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -229,14 +229,14 @@ namespace CalcEngine
|
||||
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 rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
rshrat(&lhsRat, rhsRat, radix, precision);
|
||||
rshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -252,32 +252,19 @@ namespace CalcEngine
|
||||
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{};
|
||||
|
||||
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 this->Xor(chopNum, 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 rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
andrat(&lhsRat, rhsRat, radix, precision);
|
||||
andrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -293,13 +280,13 @@ namespace CalcEngine
|
||||
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 rhsRat = rhs.ToPRAT();
|
||||
try
|
||||
{
|
||||
orrat(&lhsRat, rhsRat, radix, precision);
|
||||
orrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -315,13 +302,13 @@ namespace CalcEngine
|
||||
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 rhsRat = rhs.ToPRAT();
|
||||
try
|
||||
{
|
||||
xorrat(&lhsRat, rhsRat, radix, precision);
|
||||
xorrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -454,13 +441,13 @@ namespace CalcEngine
|
||||
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();
|
||||
uint64_t result;
|
||||
try
|
||||
{
|
||||
result = rattoUlonglong(rat, radix, precision);
|
||||
result = rattoUlonglong(rat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
|
@ -95,7 +95,7 @@ CCalcEngine::CCalcEngine(bool fPrecedence, bool fIntegerMode, CalculationManager
|
||||
|
||||
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);
|
||||
SettingsChanged();
|
||||
@ -118,7 +118,7 @@ void CCalcEngine::InitChopNumbers()
|
||||
for (size_t i = 0; i < m_chopNumbers.size(); i++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -1048,12 +1048,12 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
|
||||
|
||||
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);
|
||||
if ((radix == 10) && fMsb)
|
||||
{
|
||||
// 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.Negate();
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
|
||||
}
|
||||
|
||||
// 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
|
||||
// Check the range.
|
||||
@ -66,10 +66,10 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
|
||||
// if negative make positive by doing a twos complement
|
||||
result = result.Negate();
|
||||
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;
|
||||
}
|
||||
|
@ -32,26 +32,35 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
switch (op)
|
||||
{
|
||||
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;
|
||||
|
||||
/* Return complement. */
|
||||
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;
|
||||
|
||||
// Rotate Left with hi bit wrapped over to lo bit
|
||||
case IDC_ROL:
|
||||
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;
|
||||
w64Bits <<= 1; // LShift by 1
|
||||
w64Bits |= msb; // Set the prev Msb as the current Lsb
|
||||
|
||||
result = Rational{ w64Bits, m_radix, m_precision };
|
||||
result = Rational{ w64Bits, m_precision };
|
||||
}
|
||||
break;
|
||||
|
||||
@ -59,14 +68,14 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
case IDC_ROR:
|
||||
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;
|
||||
w64Bits >>= 1; //RShift by 1
|
||||
w64Bits |= (lsb << (m_dwWordBitWidth - 1));
|
||||
|
||||
result = Rational{ w64Bits, m_radix, m_precision };
|
||||
result = Rational{ w64Bits, m_precision };
|
||||
}
|
||||
break;
|
||||
|
||||
@ -89,42 +98,42 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
case IDC_SIN: /* Sine; normal and arc */
|
||||
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;
|
||||
|
||||
case IDC_SINH: /* Sine- hyperbolic and archyperbolic */
|
||||
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;
|
||||
|
||||
case IDC_COS: /* Cosine, follows convention of sine function. */
|
||||
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;
|
||||
|
||||
case IDC_COSH: /* Cosine hyperbolic, follows convention of sine h function. */
|
||||
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;
|
||||
|
||||
case IDC_TAN: /* Same as sine and cosine. */
|
||||
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;
|
||||
|
||||
case IDC_TANH: /* Same as sine h and cosine h. */
|
||||
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;
|
||||
|
||||
@ -133,16 +142,16 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
break;
|
||||
|
||||
case IDC_SQR: /* Square */
|
||||
result = Pow(rat, 2, m_radix, m_precision);
|
||||
result = Pow(rat, 2, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_SQRT: /* Square Root */
|
||||
result = Root(rat, 2, m_radix, m_precision);
|
||||
result = Root(rat, 2, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_CUBEROOT:
|
||||
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;
|
||||
|
||||
case IDC_LOG: /* Functions for common log. */
|
||||
@ -150,15 +159,15 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
break;
|
||||
|
||||
case IDC_POW10:
|
||||
result = Pow(10, rat, m_radix, m_precision);
|
||||
result = Pow(10, rat, m_precision);
|
||||
break;
|
||||
|
||||
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;
|
||||
|
||||
case IDC_FAC: /* Calculate factorial. Inverse is ineffective. */
|
||||
result = Fact(rat, m_radix, m_precision);
|
||||
result = Fact(rat, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_DEGREES:
|
||||
@ -173,14 +182,14 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||
{
|
||||
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);
|
||||
minuteRat = minuteRat.Mul(shftRat, m_precision);
|
||||
|
||||
Rational secondRat = minuteRat;
|
||||
|
||||
minuteRat = Integer(minuteRat, m_radix, m_precision);
|
||||
minuteRat = Integer(minuteRat, m_precision);
|
||||
|
||||
secondRat = secondRat.Sub(minuteRat, m_precision);
|
||||
secondRat = secondRat.Mul(shftRat, m_precision);
|
||||
|
@ -8,12 +8,12 @@
|
||||
using namespace std;
|
||||
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();
|
||||
try
|
||||
{
|
||||
fracrat(&prat, radix, precision);
|
||||
fracrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -27,12 +27,12 @@ Rational RationalMath::Frac(Rational const& rat, uint32_t radix, int32_t precisi
|
||||
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();
|
||||
try
|
||||
{
|
||||
intrat(&prat, radix, precision);
|
||||
intrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -46,14 +46,14 @@ Rational RationalMath::Integer(Rational const& rat, uint32_t radix, int32_t prec
|
||||
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 powRat = pow.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
powrat(&baseRat, powRat, radix, precision);
|
||||
powrat(&baseRat, powRat, RATIONAL_BASE, precision);
|
||||
destroyrat(powRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
@ -69,18 +69,18 @@ Rational RationalMath::Pow(Rational const& base, Rational const& pow, uint32_t r
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
factrat(&prat, radix, precision);
|
||||
factrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -94,13 +94,13 @@ Rational RationalMath::Fact(Rational const& rat, uint32_t radix, int32_t precisi
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
exprat(&prat, radix, precision);
|
||||
exprat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
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() } };
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
sinanglerat(&prat, angletype, radix, precision);
|
||||
sinanglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -169,13 +169,13 @@ Rational RationalMath::Sin(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
cosanglerat(&prat, angletype, radix, precision);
|
||||
cosanglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -189,13 +189,13 @@ Rational RationalMath::Cos(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
tananglerat(&prat, angletype, radix, precision);
|
||||
tananglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -209,13 +209,13 @@ Rational RationalMath::Tan(Rational const& rat, ANGLE_TYPE angletype, uint32_t r
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
asinanglerat(&prat, angletype, radix, precision);
|
||||
asinanglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -229,13 +229,13 @@ Rational RationalMath::ASin(Rational const& rat, ANGLE_TYPE angletype, uint32_t
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
acosanglerat(&prat, angletype, radix, precision);
|
||||
acosanglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -249,13 +249,13 @@ Rational RationalMath::ACos(Rational const& rat, ANGLE_TYPE angletype, uint32_t
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
atananglerat(&prat, angletype, radix, precision);
|
||||
atananglerat(&prat, angletype, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -269,13 +269,13 @@ Rational RationalMath::ATan(Rational const& rat, ANGLE_TYPE angletype, uint32_t
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
sinhrat(&prat, radix, precision);
|
||||
sinhrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -289,13 +289,13 @@ Rational RationalMath::Sinh(Rational const& rat, uint32_t radix, int32_t precisi
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
coshrat(&prat, radix, precision);
|
||||
coshrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -309,13 +309,13 @@ Rational RationalMath::Cosh(Rational const& rat, uint32_t radix, int32_t precisi
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
tanhrat(&prat, radix, precision);
|
||||
tanhrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -329,13 +329,13 @@ Rational RationalMath::Tanh(Rational const& rat, uint32_t radix, int32_t precisi
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
asinhrat(&prat, radix, precision);
|
||||
asinhrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
@ -349,13 +349,13 @@ Rational RationalMath::ASinh(Rational const& rat, uint32_t radix, int32_t precis
|
||||
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();
|
||||
|
||||
try
|
||||
{
|
||||
acoshrat(&prat, radix, precision);
|
||||
acoshrat(&prat, RATIONAL_BASE, precision);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
|
@ -18,15 +18,15 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||
switch (operation)
|
||||
{
|
||||
case IDC_AND:
|
||||
result = result.And(rhs, m_radix, m_precision);
|
||||
result = result.And(rhs, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_OR:
|
||||
result = result.Or(rhs, m_radix, m_precision);
|
||||
result = result.Or(rhs, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_XOR:
|
||||
result = result.Xor(rhs, m_radix, m_precision);
|
||||
result = result.Xor(rhs, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_RSHF:
|
||||
@ -36,21 +36,21 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||
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;
|
||||
|
||||
Rational holdVal = result;
|
||||
result = rhs.Rsh(holdVal, m_radix, m_precision);
|
||||
result = rhs.Rsh(holdVal, m_precision);
|
||||
|
||||
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);
|
||||
tempRat = Integer(tempRat, m_radix, m_precision);
|
||||
auto tempRat = m_chopNumbers[m_numwidth].Rsh(holdVal, m_precision);
|
||||
tempRat = Integer(tempRat, m_precision);
|
||||
|
||||
tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_radix, m_precision);
|
||||
result = result.Or(tempRat, m_radix, m_precision);
|
||||
tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_precision);
|
||||
result = result.Or(tempRat, m_precision);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -61,7 +61,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||
throw CALC_E_NORESULT;
|
||||
}
|
||||
|
||||
result = rhs.Lsh(result, m_radix, m_precision);
|
||||
result = rhs.Lsh(result, m_precision);
|
||||
break;
|
||||
|
||||
case IDC_ADD:
|
||||
@ -85,23 +85,23 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
iNumeratorSign = -1;
|
||||
}
|
||||
|
||||
w64Bits = temp.ToUInt64_t(m_radix, m_precision);
|
||||
w64Bits = temp.ToUInt64_t(m_precision);
|
||||
fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||
|
||||
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);
|
||||
|
||||
iDenominatorSign = -1;
|
||||
@ -121,18 +121,18 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||
|
||||
if (m_fIntegerMode && iFinalSign == -1)
|
||||
{
|
||||
result = Integer(result, m_radix, m_precision).Negate();
|
||||
result = Integer(result, m_precision).Negate();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -18,13 +18,13 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RADIX_TYPE radixtype, NUM_WIDTH numwid
|
||||
// back to 1111,1111,1000,0001 when in Word mode.
|
||||
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
|
||||
|
||||
if (fMsb)
|
||||
{
|
||||
// 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);
|
||||
|
||||
m_currentVal = tempResult.Negate();
|
||||
@ -85,7 +85,7 @@ bool CCalcEngine::TryToggleBit(CalcEngine::Rational& rat, DWORD wbitno)
|
||||
return false; // ignore error cant happen
|
||||
}
|
||||
|
||||
Rational result = Integer(rat, m_radix, m_precision);
|
||||
Rational result = Integer(rat, m_precision);
|
||||
if (result.IsZero())
|
||||
{
|
||||
// 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{};
|
||||
}
|
||||
|
||||
auto pow = Pow(2, static_cast<int32_t>(wbitno), m_radix, m_precision);
|
||||
rat = result.Xor(pow, m_radix, m_precision);
|
||||
auto pow = Pow(2, static_cast<int32_t>(wbitno), m_precision);
|
||||
rat = result.Xor(pow, m_precision);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -6,6 +6,10 @@
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
@ -14,7 +18,7 @@ namespace CalcEngine
|
||||
Rational(Number const& p, Number const& q) noexcept;
|
||||
Rational(int32_t i);
|
||||
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;
|
||||
PRAT ToPRAT() const;
|
||||
@ -29,13 +33,13 @@ namespace CalcEngine
|
||||
Rational Div(Rational const& rhs, int32_t precision) const;
|
||||
Rational Mod(Rational const& rhs) const;
|
||||
|
||||
Rational Lsh(Rational const& r, uint32_t radix, int32_t precision) const;
|
||||
Rational Rsh(Rational const& r, uint32_t radix, int32_t precision) const;
|
||||
Rational Lsh(Rational const& r, 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 And(Rational const& r, uint32_t radix, int32_t precision) const;
|
||||
Rational Or(Rational const& r, uint32_t radix, int32_t precision) const;
|
||||
Rational Xor(Rational const& r, uint32_t radix, int32_t precision) const;
|
||||
Rational Not(Rational const& chopNum, int32_t precision) const;
|
||||
Rational And(Rational const& r, int32_t precision) const;
|
||||
Rational Or(Rational const& r, int32_t precision) const;
|
||||
Rational Xor(Rational const& r, int32_t precision) const;
|
||||
|
||||
bool IsZero() 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;
|
||||
|
||||
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:
|
||||
Number m_p;
|
||||
|
@ -5,31 +5,31 @@
|
||||
|
||||
namespace CalcEngine::RationalMath
|
||||
{
|
||||
Rational Frac(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational Integer(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational Frac(Rational const& rat, 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 Root(Rational const& base, Rational const& root, uint32_t radix, int32_t precision);
|
||||
Rational Fact(Rational const& rat, 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, 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 Log10(Rational const& rat, int32_t precision);
|
||||
|
||||
Rational Invert(Rational const& rat, int32_t precision);
|
||||
Rational Abs(Rational const& rat);
|
||||
|
||||
Rational Sin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision);
|
||||
Rational Cos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision);
|
||||
Rational Tan(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision);
|
||||
Rational ASin(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision);
|
||||
Rational ACos(Rational const& rat, ANGLE_TYPE angletype, uint32_t radix, int32_t precision);
|
||||
Rational ATan(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, int32_t precision);
|
||||
Rational Tan(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
|
||||
Rational ASin(Rational const& rat, ANGLE_TYPE angletype, int32_t precision);
|
||||
Rational ACos(Rational const& rat, ANGLE_TYPE angletype, 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 Cosh(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational Tanh(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational ASinh(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational ACosh(Rational const& rat, uint32_t radix, int32_t precision);
|
||||
Rational Sinh(Rational const& rat, int32_t precision);
|
||||
Rational Cosh(Rational const& rat, int32_t precision);
|
||||
Rational Tanh(Rational const& rat, int32_t precision);
|
||||
Rational ASinh(Rational const& rat, int32_t precision);
|
||||
Rational ACosh(Rational const& rat, int32_t precision);
|
||||
Rational ATanh(Rational const& rat, int32_t precision);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
@ -347,6 +347,7 @@ PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix)
|
||||
// mantissa a string representation of a number
|
||||
// exponentIsNegative true if exponent is less than zero
|
||||
// exponent a string representation of a number
|
||||
// radix is the number base used in the source string
|
||||
//
|
||||
// RETURN: PRAT representation of string input.
|
||||
// Or nullptr if no number scanned.
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user