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.

25-Diamond I
February 25, 2023

You may also like this well known example which shows, that the commutative law for addition does not apply to "computer numbers".

Compare the last two digits of the result:

Werner_E_2-1677290141540.png

And if you have a couple of cups of coffee ready, you may try to symbolically evaluate both expressions. It will take a while and the result most likely is a fraction of two huge integers which you can turn into a floating point number using the "float" modifier.

 

 

25-Diamond I
February 25, 2023

You may consider reporting your suggestion to PTC support, hoping they will hand it over to R&D.

But I fear that, as you don't seem to be a paying customer, PTC is not much interested in your suggestions and gives you no channel to deposit them. But my impression is also that they are not particularly interested in the opinion of the paying customers either, but at least they pretend to be 😉