Skip to main content
3-Newcomer
February 25, 2023
Solved

Wrong calculation results

  • February 25, 2023
  • 1 reply
  • 3769 views
 

Hi!

 

I surprisingly found that Mathcad calculates this simple expression wrong:

 

NG_10567195_3-1677285427579.png

 

Both results must be the same. The second result is the correct one.

It gets even worse in this case:

 

NG_10567195_2-1677285392379.png

 

It seems that Mathcad accumulates floating point errors in sums, which can be easily avoided (or greatly reduced) by applying a proper summation algorithm. It costs nothing to implement, so why not do it?

Best answer by Werner_E

So the option you have are

a) Talk PTC into changing the built-in algorithms (don't hold your breath)

b) Use the symbolic engine for evaluation

c) Implement Kahan as a user defined function. As you see below, it does its job

Werner_E_0-1677461916621.png

 

I was also curious what it would mean to calculation time and used a crude timer function.
It looks like Kahan increases execution time by about 25%. But I guess it could be significantly speeded up by programming in Visual Studio and turning it into a Prime Custom function DLL.

Werner_E_1-1677462428015.png

 

 

 

1 reply

25-Diamond I
February 25, 2023

I guess that what you experience is the typical limitation of numbers stored in IEEE format and its typical 14-15 valid significant digits. Additionally the subsequent conversion of all units to standard meter makes it even worse. You probably know that a number like 0.1 is a periodic number in the binary system and so has to be cut off somewhere.

For more precise results you may use the symbolic evaluation. You may have to turn on the calculation option to use constants and units in symbolics.

Even if you turn on this option, you still will have to tell the symbolics that 1 cm equals 0.01 m, etc. Either by redefining the units in the worksheet or by using the "substitute" modifier. Unfortunately the symbolics still does not know much about units.

Werner_E_1-1677289623336.png

BTW, if you define a new unit like dm, you should make it a "real" unit by labelling it as a unit and not, as you did, as a variable.

3-Newcomer
February 25, 2023

Thank you Werner_E. I know how to obtain the correct results.

This was simply a test. What I expect is that the numerical summation does not build up the floating point errors so much. It depends on how Mathcad calculates the sums internally. It can be done in better ways. 🙂