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

1-Newbie

## zero not zero

In MathCAD, I used an expressions like:

if (cos(angle), expression 1, expression 2)

Meaning: if the cosine of the angle is not zero (TRUE), then evaluate expression 1, else expression 2.

or: if (1,2,3) yields 2

if (0,2,3) yields 3

I assumed that cos(PI/2) = 0.

BUT: it appears that 0 is not exactly 0. Even though I fill in n*(PI/2), and cos(angle) yields 0, the IF statement does not yield proper results.

Only if I round of cos(angle) at approx. 12-13 decimals, do I get proper results with the IF statement.

also, sign(cos(angle)) never yields 0, only 1 or -1. It should yield 0 at angle=PI/2.....

Did I miss something? Is there a better way then to "force" the results to 0 by rounding them off?

Any wizards here to help me out?

tuduvu
12 REPLIES 12
Eek! That's a nasty bug.

Richard
Yes, a nasty one.

It gets even worse (for me).

I have set up a complex calculation with a lot of gonimetric functions, and e.g. for n*PI/2 it creates a chaos-formula (extremely narrow and high spikes). So I need to filter out these arguments, and I round off the cos(angle), to force it to 0. E.g. at 14 decimals.

When n gets large, I need to set the accuracy to fewer decimals (n can get up to 10^6), e.g. to 12.

Also, I need to figure out the quadrant where, for a certain argument, the function is.

I use sign(cos(angle)), that yields only 1 and -1, never 0, as it should for the nagles mentioned.

I think I need to develop a kind of magnet function, meaning that all angle values are being left as they are, unless they are close to the problem-causing angles. Then they should snap to a better-evaluated constant, like (example 312/214).

Anyone knows an approximate for PI?

Funny: in Excel everything worked fine. It appears that one can be too accurate......

tuduvu
1-Newbie
1-Newbie
It still seems to be chopped. Maybe this will work:

As a rule of thumb, instead of writing if(x=0,1,2) one should write if(abs(x) is less than epsilon,1,2).

Robert

Would this function help?

Richard
Has anyone gotten extended-precision arithmetic to work with the symbolic processor? I'm trying to make a function that takes an extended-precision number and returns an extended-precision answer, but it doesn't seem to work. I'm attaching a worksheet that shows it working under some very special circustances for cos.

Also, I mentioned earlier that atan2 returns an answer from 0 to 2pi, actually it's -pi to pi and angle returns a value from 0 to 2pi.

Robert
1-Newbie
OK, it helps if I attach the file.

Robert

1-Newbie
This is what I get (apparently the trick does not work in all cases):

Xavier
1-Newbie
arg(z) also returns an angle within [-pi, pi].

By the way, I've found in the past that the acos() function is also rather unreliable near zero.

Xavier
OK. After a night's sleep, here is what I came up with:

zero:= 0.0001 (or as small as needed)

cos.r(angle):= if(|cos(angle)| < zero, 0, cos(angle))

cos.r to be used instead of cos, when applicable (like sign / if statements)

Thanks to all of you. It was my first post in this forum, and I am happy to see the enthousiasm displayed here.

tuduvu
Here's a function that forces cos(x) to be zero when x is odd-integer multiple of pi/2. It is very ugly looking, but I think it works. No guarantees though!

Richard
Announcements
Check out the latest