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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Translate the entire conversation x

Equal operation for comparison in Mathcad prime is not working

duo1663
2-Explorer

Equal operation for comparison in Mathcad prime is not working

Hi there, as shown in the following screenshot, could someone kindly tell what is wrong with this mathcad operation? There are are same values of 6 inches. But still it is showing "NOT OK". I would be much obliged if someone can resolve my this problem.

 

Saleh
ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:duo1663)

While you may think that both variables are of exact same value, that's not true.

You can see that there is a difference of slightly more than 10^-15 in and t.provided is actually smaller than t.required for that tiny amount. Hence you get "Not OK" as the answer.

Werner_E_0-1752403959155.png

These round-off errors or numerical inaccuracies are normal and to be expected. Tiny round-off errors when doing calculations or unit conversions (Prime stores internally in SI unit meter) are the cause.

Its a golden rule never to check float values for equality but rather for approximate equality.

So don't use "if a=b ..." but rather  "if |a-b| < 10^-10 ...". You decide for the level of accuracy and it does not matter if you use < or <=.

In your case you could cope with the problem by doing it that way:

Werner_E_1-1752404292005.png

10^-14 in might be too small for other values. You have to decide if an accuracy of 10^-6 in or 10^-8 in wouldn't be al you need.

 

To turn it into a function you could make the accuracy level a third function argument:

Werner_E_2-1752404422280.png

 

Or you may use

Werner_E_0-1752404759454.png

Here the third function argument represents the number of decimals needed as accuracy.

The division by the unit in does not affect the variables. Its just needed for the usage of "round".

You may also use the "Round" function (note the capital "R") to again provide the tolerance in inches as third argument:

Werner_E_1-1752405030536.png

A float value usually is said to be accurate up to 15 to 16 decimals. Given that 10^-15 meter is roughly 4*10^-14 inches, I would not expect a higher precision than 10^-14 inches. And round-off errors can accumulate if the value is derived by a series of calculations.

So demanding a precision of 10^-10 inches might be considered realistic but nonetheless ambitious. Normally I guess a much larger precision might suffice.

View solution in original post

14 REPLIES 14

Hi,

Very hard to actually diagnose problem without the worksheet.  Worksheets can be uploaded.

 

Check with many more decimal places for tprovided and trequired it could be round off errors.

 

Compare to a defined accuracy round(tprovided,3)>=round(trequired,3) works better.

 

Cheers

Terry

This is an incomplete mathcad file though. Only the simple mathcad operation is seeming to be wrong. It was supposed to show "OK" but showing "Not Ok" whereas the situation is like 6 inches = 6 inches. I have attached the whole file below

Saleh

It is an incomplete worksheet. Though I can upload it for you. The thing is that it was simple math operation of comparison using 'IF'. It was supposed to show "Ok" but it shows "Not Ok".

Saleh

There's nothing wrong with Mathcad, you're experiencing small differences in actual numerical results and mathematical formatting:

 

Fred_Kohlhepp_0-1752397523751.png

 

Is there any solution to this one, please?

duo1663_0-1752398401041.png

 

Saleh

Capture.JPG

 

Need to keep the unit intact because at the above section, there are variables in which i may enter the value in milimeters and use the advantage of auto conversion of units in mathcad.

Saleh

The comparison of the two values using inches does not alter the value input of trequired or tprovided so the unit you use to define them is still intact.

They are just compared in the test in inches to three decimal places.

Capture.JPG

SPaulis
14-Alexandrite
(To:terryhendicott)

What a weird bug for Mathcad to have : 

SPaulis_6-1752506036904.png

 

 

Dividing by the specific unit in question works as long as you select the correct unit and number of digits each time (tedious).  Some engineering values may have a tighter tolerance, but most construction materials are sold with 2 or 3 digits (...I know, the material is what's sold, but the accompanying data sheets usually only report 2 or 3 significant digits.)

 

But if you simply divide the two variable, the question of compatible units goes away and the same formula can be repeated for all the checks throughout the document.  Then, rounding the result to three digits is more than enough for most engineering applications (depending on your specific industry).

 

Here are some simplifications.  I prefer the in-line formula because normal reading style is to read across, then down.  So for the 'program style', the last line always says "Not OK" so it becomes distracting for the reviewer—keep is simple s-----!

 

SPaulis_2-1752503911785.png

 

Even better if you define a "generic" check function so that all the reviewer only sees is the result of the check, not the same distracting program every time:

SPaulis_4-1752505493360.png

 

 

 


   

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
Werner_E
25-Diamond I
(To:SPaulis)

I won't call it a bug as its the normal behaviour to be expected when working with IEEE float numbers.

But I agree that it can be an annoyance.

While displaying the converted 6 inches up to the max 15 decimals in mm does not show anything unusual, it can be seen by taking the difference that the converted 6 inches actually are slightly smaller than 152.5 mm.

Werner_E_0-1752509219144.png

You may get the expected zero by setting the zero to a lower value than the standard 15, but this does only affect the display of results and not the precision used in internal comparisons:

Werner_E_1-1752509315109.png

BTW, in real Mathcad when we demand to show 17 decimals we see

Werner_E_2-1752509492074.png

 

 

Werner_E
25-Diamond I
(To:SPaulis)

You may also define your own comparison operators.

Werner_E_3-1752510573012.png

Its doubtful if they would be helpful, though, because they are cumbersome to use (copy & paste) and because on contrary to real Mathcad we are not offered the usage of a function in two arguments in inline notation its not really good looking.

An option might be something like

Werner_E_4-1752510847292.png

Using the quotient and that way getting automatically rid of the units is a good idea.

 

 

SPaulis
14-Alexandrite
(To:Werner_E)

But in real life engineering problems it is 100% correct that 6" exactly equals 152.4 mm.  However, if the result is a "not good, provide a wider beam you silly engineer", and you listen to that result blindly, it doesn't hurt to upsize the beam by the next size.  We are in a "be exact, don't be so exact" style of applied science. 🤦🏻‍

 

Yeah... I just simplify my functions to leq(), geq(), lt(), gt(), etc., same as Mathcad uses.  But the are not readily recognizable by the other engineers, and i don't even use them that much.  Instead I prefer a simple "is()" function.  The name of the label and the result says it all.  And apparently its good for 99.999999999% of the calculations I do—I'll take my chances 😉

 

Using a quotient to get rid of units is only good until you are using relative numbers and get a divide by zero error.  Some people will need a more robust version that will do something like divide the two parameters by SIUnitsOf() function.  Or just leave it alone if your calculations are tolerant to this bug.  Practically all the time in engineering, I am never really close to a '1 = 1' scenario.  I usually have reserve capacity in my designs.

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
Werner_E
25-Diamond I
(To:duo1663)

While you may think that both variables are of exact same value, that's not true.

You can see that there is a difference of slightly more than 10^-15 in and t.provided is actually smaller than t.required for that tiny amount. Hence you get "Not OK" as the answer.

Werner_E_0-1752403959155.png

These round-off errors or numerical inaccuracies are normal and to be expected. Tiny round-off errors when doing calculations or unit conversions (Prime stores internally in SI unit meter) are the cause.

Its a golden rule never to check float values for equality but rather for approximate equality.

So don't use "if a=b ..." but rather  "if |a-b| < 10^-10 ...". You decide for the level of accuracy and it does not matter if you use < or <=.

In your case you could cope with the problem by doing it that way:

Werner_E_1-1752404292005.png

10^-14 in might be too small for other values. You have to decide if an accuracy of 10^-6 in or 10^-8 in wouldn't be al you need.

 

To turn it into a function you could make the accuracy level a third function argument:

Werner_E_2-1752404422280.png

 

Or you may use

Werner_E_0-1752404759454.png

Here the third function argument represents the number of decimals needed as accuracy.

The division by the unit in does not affect the variables. Its just needed for the usage of "round".

You may also use the "Round" function (note the capital "R") to again provide the tolerance in inches as third argument:

Werner_E_1-1752405030536.png

A float value usually is said to be accurate up to 15 to 16 decimals. Given that 10^-15 meter is roughly 4*10^-14 inches, I would not expect a higher precision than 10^-14 inches. And round-off errors can accumulate if the value is derived by a series of calculations.

So demanding a precision of 10^-10 inches might be considered realistic but nonetheless ambitious. Normally I guess a much larger precision might suffice.

duo1663
2-Explorer
(To:Werner_E)

This explains well. Thank you for your elaborate explanation and help. This worked in eliminating the issue.

Saleh
Announcements

Top Tags