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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

## Loop program  7-Bedrock

## Loop program

The problem is the R loop solve 2 instead of 1 and the Vx loop solve 2 instead of 3. What’s the problem?

9 REPLIES 9  17-Peridot
(To:AS_9781967)

Don't know why the units cause problems.   24-Ruby V
(To:AS_9781967)

When you learn programming than one of the first things you learn is that you should never compare float values for equality but rather ask if the absolut value of their difference is smaller than a certain small value like 10^-10.

The use of the units results in very small floating point rounding/conversion errors and so (l-b) is not exactly equal to x. You may do it that way: Or define a function BTW, your programs don't contain any loops (no "if" and no "while").  7-Bedrock
(To:Werner_E)

Thank you for your input. Is it possible to use different calculation formulae depending on x value

If x = (l-b); Matcad use formulae 1 to solve R

If 0 ≤ x < (l-b); Matcad use formulae 2 to solve R

If (l-b) < x ≤ l; Matcad use formulae 3 to solve R

For any other values of x, Matcad use 0 to solve R

If 0 ≤ x ≤ a; Matcad use formulae 1 to solve Vx

If a < x < (l-b); Matcad use formulae 2 to solve Vx

If (l-b) ≤ x ≤ l; Matcad use formulae 3 to solve Vx

For any other values of x, Matcad use 0 to solve Vx  24-Ruby V
(To:AS_9781967)

@AS_9781967 wrote:

Thank you for your input. Is it possible to use different calculation formulae depending on x value

Sure. Simply replace "Result <-- 1" by "Result <-- <formulae 1>" etc.  7-Bedrock
(To:Werner_E)

I didnt quite get why Matcad solve 2 if x = (l-b) = 60?  Why should we have to add e = 10^10?  24-Ruby V
(To:AS_9781967)

Because the value stored in x and the result of l-b are not exactly equal. Because of internal round off or conversion errors they are off by about 10^-18 as I had shown. Thats the reason why if x=l-b ... does not trigger - Prime realizes/thinks the the expressions are NOT equal. If you have two real floating point values  (and using unit mm seems to turn the integers into floats) a and b you should not ask if a=b but rather if "a" is approximately equal to "b" within a certain accuracy. So you don't ask "if a=b ..." but rather "if |a-b|< eps ..." with eps being a small value like 10^-12 or the like.

But there is an even simpler solution to your problem: Chose menu ribbon "Calculation", then open "Calculation Options" and turn on "Approximate Equality". This should make your if statement trigger the way it should be.  7-Bedrock
(To:Werner_E)

Can you please share the worksheet?  24-Ruby V
(To:AS_9781967)

@AS_9781967 wrote:

Can you please share the worksheet?

Here you are.

Its just your worksheet unchanged - only with "Approximate Equality" enabled.  17-Peridot
(To:AS_9781967)

See here:  