cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community email notifications are disrupted. While we are working to resolve, please check on your favorite boards regularly to keep up with your conversations and new topics.

Wrong calculation results

NG_10567195
3-Visitor

Wrong calculation results

 

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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

 

 

 

View solution in original post

10 REPLIES 10

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.

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. 🙂

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.

 

 

Yes, of course I know that. That is why I put this issue to discussion. Summation can be performed in a way that reduces the floating point errors, an I am wondering why Mathcad has not implemented such algorithm.

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 😉

 

OK, Thank you,  Werner!

I am still evaluating Mathcad, but I can try as you suggest. Probably, they have too many clients and will not want another one. 😉 But for the money they require, I would expect a software to the highest standards.

 

What I mean are methods like pairwise (cascading) summation, or the Kahan summation algorithm.

For the example with the units, I get zero error when I implement my own routine. So, both results are the same with double precision floating point and no symbolics. And it also performs much faster.

 

NG_10567195_0-1677295229360.png

 

But it seems that Mathcad simply adds consequently each member to the sum  (sum += x_i, i = 1..n) and in this way, accumulates the rounding error. Consequent summation is not good because, as the sum grows, you start adding many small numbers to a much larger one and lose significant digits each time. 😞

What exactly are you trying to measure to the accuracy of 5,555,000,055,577,470 m or 5,555,000,055,500,000 m

384,400,000 m Average Earth to Moon
148,070,000,000 m One source Earth to Sun
149,600,000,000 m Another Source
149,597,870,000 m average Another Source as an astronomical unit

Astronomical unit is the average distance between the sun and the earth which is about 149.6149.6 million km. Neptune is 30.0730.07 Astronomical units away from the sun. Another Source.
314,955,948,445,000 m give or take?  Sun to Neptune  or the radius of the solar system, sorry Pluto  What about elliptical orbits?

5,555,000,055,500,000 m please forgive me if I chuckle.  17.691 times the solar system give or take.  Mathcad is for engineers.  Hoorah.

This was just an example that I used to figure out how Mathcad calculates sums. Are you trying to bully me that the numbers are not practical for this particular example? This is ridiculous. It was not my purpose anyway.

 

There are many engineering fields that require precise calculations. FP Rounding errors are harmless in most cases, but sometimes they can have significant impact on the final result. One example for this is "cancellation". It may leave you with a completely wrong answer after you lose all of the correct significant digits. So, it is good to minimize rounding errors in general, by using more stable algorithms where possible.

 

You can have a look on this paper:

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Pay attention on the "Cancellation" and "Errors In Summation". chapters. You probably also have not checked my link about the Kahan algorithm above to see what is actually the problem:

https://en.wikipedia.org/wiki/Kahan_summation_algorithm

 

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

 

 

 

Hi Werner!

 

Thank you very much!

 

Great job and great idea for implementing the Kahan algorithm manually on Mathcad! I will mark it as the solution.

It is correct and definitely does the job better. Of course, we have some performance trade off, because it requires additional operations. But the overhead will be much less if the algorithm is implemented internally.

 

Making Custom function DLL is a good option too. I have the algorithm implemented in C#. I will try to make a DLL and will post it here, when ready. 🙂

 

Top Tags