[Thread Prev][Thread Next][Index]

Re: [ferret_users] if condition error



​Hi all, Thanks to Bruno and Ryo for providing solutions. Thanks to Ryo for his funny proposal :-) 

regards
Steeven Paul​

On Tue, May 31, 2016 at 7:26 PM, Ryo Furue <furue@xxxxxxxxxx> wrote:
Hi

First of all, I was wondering if branching with IFs is really necessary.  Steeven Paul seems to try to compute some sort of angle of vector (v5,v6) and I wonder if a regular function can be constructed without IFs.

Anyway,

I would use this instead:
let theta1 = if(v5 gt 0 and v6 gt 0) then Q1 ELSE 0
let theta2 = if(v5 lt 0 and v6 gt 0) then Q2 ELSE 0
let theta3 = if(v5 lt 0 and v6 lt 0) then Q3 ELSE 0
let theta4 = if(v5 gt 0 and v6 lt 0) then Q4 ELSE 0

let theta = theta1 + theta2 + theta3 + theta4

Another trick is

  let s5 = v5 / abs(v5)
  let s6 = v6 / abs(v6)

Notice that s5 =  if v5 > 0 then 1 else -1 .  Forget about v5 == 0.  Likewise for v6.  Now,

  let q = 180*(1 - s5)*(s6 + 1)/2 + 180*(1 - s6)/2

This is what you want.  Really?  See:

yes? cancel mode verify
yes? let v5 = 3; let v6 = 5; say `q`
0
yes? let v5 = -3; let v6 = 5; say `q`
360
yes? let v5 = -3; let v6 = -5; say `q`
180
yes? let v5 = 3; let v6 = -5; say `q`
180

What to do when v5 = 0 or v6 = 0 is left for the reader's exercise.

Okay, I'm not seriously proposing this.  It's just for fun :-)  Having said that, using the SIGN function to emulate switches is a common (sometimes necessary) trick in programming.

Cheers,

Ryo



[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement