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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Mathcad odd result

dsignoretti
1-Newbie

Mathcad odd result

Please find attached the description of a bad result found by my colleague working with mathcad.

Does anyone have an explanation or solution to this issue?

Best Regards

Diego Signoretti

1 ACCEPTED SOLUTION

Accepted Solutions
athurin
4-Participant
(To:dsignoretti)

This is down to the way computers store and handle numbers.

Unlike humans do, computers usually don't handle exact number. For instance, a number is stored on a limited amount of disk space, which means that a number can only have a limite number of decimal places. Think of pi, or e, and you will instantly see how that can be an issue.

The consequence of that is that every time you describe a number that doesn't have an exact representation in binary (wikipedia will explain how "float" are implemented), there are rounding errors.

In your particular example, 0.220m doesn't have an "exact" representation in binary, so the number that is actually used is the closest binary representation of it, and I think you will agree that an error that is 13 orders of magnitude smaller than what you are looking at is in most cases (not all, but most) negligible.

So here, you are comparing 2 numbers that are actually different. so the result is right. In my opinion, this should never happen though. I would expect from a software such as mathcad to define a=b as |a-b|<2*rounting error, but hey, that's not how it's implemented, unfortunately...

I hope that makes sense ...

View solution in original post

8 REPLIES 8
athurin
4-Participant
(To:dsignoretti)

This is down to the way computers store and handle numbers.

Unlike humans do, computers usually don't handle exact number. For instance, a number is stored on a limited amount of disk space, which means that a number can only have a limite number of decimal places. Think of pi, or e, and you will instantly see how that can be an issue.

The consequence of that is that every time you describe a number that doesn't have an exact representation in binary (wikipedia will explain how "float" are implemented), there are rounding errors.

In your particular example, 0.220m doesn't have an "exact" representation in binary, so the number that is actually used is the closest binary representation of it, and I think you will agree that an error that is 13 orders of magnitude smaller than what you are looking at is in most cases (not all, but most) negligible.

So here, you are comparing 2 numbers that are actually different. so the result is right. In my opinion, this should never happen though. I would expect from a software such as mathcad to define a=b as |a-b|<2*rounting error, but hey, that's not how it's implemented, unfortunately...

I hope that makes sense ...

athurin
4-Participant
(To:athurin)

BTW : you will find the exact same problem in EXCEL, but, because OpenSource software programmers are usually smarter, it works just fine in LibreOffice.

thank you for your explanation. very useful.

diego signoretti

RichardJ
19-Tanzanite
(To:athurin)

So here, you are comparing 2 numbers that are actually different. so the result is right. In my opinion, this should never happen though. I would expect from a software such as mathcad to define a=b as |a-b|<2*rounting error, but hey, that's not how it's implemented, unfortunately...

That is how it is implemented. In Mathcad 15 (and earlier versions) if you go to "Tools", "Worksheet options", on the "Calculation" tab there is a checkbox "Use exact equality for comparisons and truncation". By default it's unchecked, for the very good reason of numeric roundoff issues. For whatever reason, Diego has it checked, and that's the problem. I have personally never figured out why I would ever want this checked, but presumably at some point in the past someone had a reason, and the option was added.

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

By default it's unchecked, for the very good reason of numeric roundoff issues.

That's interesting. It was checked by default in my Mathcad 14 Blank worksheet template ...

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

Oh. That's bad! I guess I haven't actually used the default template for at least 10 years, and forgot what the default setting is

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

Oh. That's bad! I guess I haven't actually used the default template for at least 10 years, and forgot what the default setting is

I tend to use my own Normal template which has Units turned off (which I must confess has the box checked), but I use the Blank template when I want to play with Units. I didn't think I'd messed with the Blank template (mainly because I don't think there is one!), but I checked the other templates that I know I wouldn't have touched and they all have exact comparison checked.

Stuart

StuartBruff
23-Emerald II
(To:athurin)

Adrien Thurin wrote:

This is down to the way computers store and handle numbers.

...

So here, you are comparing 2 numbers that are actually different. so the result is right. In my opinion, this should never happen though. I would expect from a software such as mathcad to define a=b as |a-b|<2*rounting error, but hey, that's not how it's implemented, unfortunately...

I hope that makes sense ...

I can't speak for Prime, but Mathcad <= 15 does have the capability to ignore a certain amount of rounding error when doing boolean comparisons or truncation. See Help for the Worksheets Options dialog box.

Stuart

Top Tags