Skip to content

Commit d7c7b14

Browse files
committed
Reworked addition/subtraction to fix logic flaw for negative zero becoming zero
-Starting rework on formula evaluations -All current addition/subtraction tests now succeed
1 parent e027e7c commit d7c7b14

5 files changed

Lines changed: 212 additions & 502 deletions

File tree

ExprFormulaTester/ExprFormulaTester.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,19 @@ int main()
231231
std::cout << targetVal.ToString() << " - " << rightVal.ToString() << " = " << altResult.ToString() << " FloatResult:" << floatingVal << std::endl;
232232

233233
std::cout << "---------------Log Tests------------------------------" << std::endl;
234-
//targetVal = "0.005";
235-
//floatingVal = 0.005;
236-
//double floatingRes;
237-
//for (targetVal = MediumDec::FiveThousandth; targetVal<MediumDec::One; targetVal += MediumDec::FiveThousandth)
238-
//{
239-
// floatingRes = log(floatingVal);
240-
// std::cout << "Builtin-Ln(" << floatingVal << ") = " << floatingRes;
241-
// floatingRes = BlazesFloatingCode::LnRef(floatingVal);
242-
// std::cout << " Ln(value) =" << floatingRes << std::endl;
243-
// floatingVal += 0.005;
244-
// altResult = BlazesRusDebug::LnTestRef(floatingVal,targetVal);
245-
// std::cout << "(MediumDec)Ln(" << targetVal.ToString()<<")= " << altResult.ToString() << std::endl;
246-
//}
234+
targetVal = "0.005";
235+
floatingVal = 0.005;
236+
double floatingRes;
237+
for (targetVal = MediumDec::FiveThousandth; targetVal < MediumDec::One; targetVal += MediumDec::FiveThousandth)
238+
{
239+
floatingRes = log(floatingVal);
240+
std::cout << "Builtin-Ln(" << floatingVal << ") = " << floatingRes;
241+
floatingRes = BlazesFloatingCode::LnRef(floatingVal);
242+
std::cout << " Ln(value) =" << floatingRes << std::endl;
243+
floatingVal += 0.005;
244+
altResult = BlazesRusDebug::LnTestRef(floatingVal, targetVal);
245+
std::cout << "(MediumDec)Ln(" << targetVal.ToString() << ")= " << altResult.ToString() << std::endl;
246+
}
247247

248248
std::cout << "---------------Testing Formula Code-------------------" << std::endl;
249249
//std::cout << "-------------------------Formula Code Tests---------------------------------" << std::endl;
@@ -254,20 +254,23 @@ int main()
254254
//IntFormTest = "5+10x";
255255
//std::cout << IntFormTest.ToString() << " = " << IntFormTest.EvalValues(IntValueDefinitions) << std::endl;
256256

257-
MediumDecFormula AltFormTest = "5.5^(1.5+x)+6x";
257+
MediumDecFormula AltFormTest = "5+5+x";
258+
tsl::ordered_map<std::string, MediumDec> ValueDefinitions;
259+
ValueDefinitions.insert_or_assign("x", MediumDec::One);
258260

259261
//tsl::ordered_map<std::string, MediumDec&> RefDefinitions;
260262
//MediumDec XReference = MediumDec::One;
261263
//RefDefinitions.insert_or_assign("x", XReference);
262264
//rootTest = AltFormTest.EvalValueRefs(RefDefinitions);
263265
//std::cout << AltFormTest.ToString() << " = " << rootTest.ToString()<< std::endl;
264266

265-
//tsl::ordered_map<std::string, MediumDec> ValueDefinitions;
266-
//ValueDefinitions.insert_or_assign("x", MediumDec::One);
267-
//rootTest = AltFormTest.EvalValues(ValueDefinitions);
267+
//
268+
268269
std::cout << "(MediumDecFormula) "<< AltFormTest.ToString() <<std::endl;
270+
AltFormTest = AltFormTest.SimplifyFormula(ValueDefinitions);
271+
std::cout << " = " << AltFormTest.ToString() << std::endl;
269272
//std::cout << " = " << rootTest.ToString() << std::endl;
270-
//AltFormTest = "5+5";
273+
//AltFormTest = "5.5^(1.5+x)+6x";
271274
//std::cout << "(MediumDecFormula) " << AltFormTest.ToString() << " = " << rootTest.ToString() << std::endl;
272275
//AltFormTest = "5+(5/4)";
273276
//std::cout << "(MediumDecFormula) " << AltFormTest.ToString() << " = " << rootTest.ToString() << std::endl;

GlobalCode/AltNum/AltNumDebug.hpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -276,52 +276,52 @@ namespace BlazesRusDebug
276276
//if (value <= 0) {}else//Error if equal or less than 0
277277
if (value==MediumDec::One)
278278
return MediumDec::Zero;
279-
if (value.IntValue==0)//Threshold between 0 and 2 based on Taylor code series from https://stackoverflow.com/questions/26820871/c-program-which-calculates-ln-for-a-given-variable-x-without-using-any-ready-f
280-
{
281-
double fthreshold = 0.000000005;
282-
double fbase = fvalue - 1; // Base of the numerator; exponent will be explicit
283-
int den = 2; // Denominator of the nth term
284-
bool posSign = true; // Used to swap the sign of each term
285-
double fterm = fbase; // First term
286-
double fprev; // Previous sum
287-
double fresult = fterm; // Kick it off
288-
double fAddRes;
289-
290-
MediumDec threshold = MediumDec::FiveMillionth;
291-
MediumDec base = value - 1; // Base of the numerator; exponent will be explicit
292-
MediumDec term = base; // First term
293-
MediumDec prev; // Previous sum
294-
MediumDec result = term; // Kick it off
295-
MediumDec AddRes;
296-
297-
do
298-
{
299-
posSign = !posSign;
300-
301-
fterm *= fbase;
302-
term *= base;
303-
304-
fprev = fresult;
305-
prev = result;
306-
307-
fAddRes = fterm / den;
308-
AddRes = term / den;
309-
310-
if (posSign)
311-
fresult += fAddRes;
312-
else
313-
fresult -= fAddRes;
314-
if (posSign)
315-
result += term / den;
316-
else
317-
result -= term / den;
318-
319-
den++;
320-
} while (abs(fprev - fresult) > fthreshold);
321-
322-
return result;
323-
}
324-
else if(value.IntValue==1)
279+
//if (value.IntValue==0)//Threshold between 0 and 2 based on Taylor code series from https://stackoverflow.com/questions/26820871/c-program-which-calculates-ln-for-a-given-variable-x-without-using-any-ready-f
280+
//{
281+
// double fthreshold = 0.000000005;
282+
// double fbase = fvalue - 1; // Base of the numerator; exponent will be explicit
283+
// int den = 2; // Denominator of the nth term
284+
// bool posSign = true; // Used to swap the sign of each term
285+
// double fterm = fbase; // First term
286+
// double fprev; // Previous sum
287+
// double fresult = fterm; // Kick it off
288+
// double fAddRes;
289+
290+
// MediumDec threshold = MediumDec::FiveMillionth;
291+
// MediumDec base = value - 1; // Base of the numerator; exponent will be explicit
292+
// MediumDec term = base; // First term
293+
// MediumDec prev; // Previous sum
294+
// MediumDec result = term; // Kick it off
295+
// MediumDec AddRes;
296+
297+
// do
298+
// {
299+
// posSign = !posSign;
300+
301+
// fterm *= fbase;
302+
// term *= base;
303+
304+
// fprev = fresult;
305+
// prev = result;
306+
307+
// fAddRes = fterm / den;
308+
// AddRes = term / den;
309+
310+
// if (posSign)
311+
// fresult += fAddRes;
312+
// else
313+
// fresult -= fAddRes;
314+
// if (posSign)
315+
// result += term / den;
316+
// else
317+
// result -= term / den;
318+
319+
// den++;
320+
// } while (abs(fprev - fresult) > fthreshold);
321+
322+
// return result;
323+
//} else
324+
if(value.IntValue<2)
325325
{
326326
MediumDec threshold = MediumDec::FiveMillionth;
327327
MediumDec base = value - 1; // Base of the numerator; exponent will be explicit

0 commit comments

Comments
 (0)