Post Reply 
multi criteria IF statement
06-11-2021, 11:06 AM
Post: #1
multi criteria IF statement
Hello,

I'm trying to write a program for directional angle calculation.

But my issue are the IF statements.

I have to test two variables in 4 different scenarios.

example Local y,x,n;

IF ( y>0 and x>0) then n:=ATAN( y/x); /// 1' scenario if the y or x is not greater next test
IF( y>0 and x<0) then n:=ATAN (y/x)+180; //// 2' scenario
IF( y<0 and x<0) then n:=ATAN(y/x)+180; //// 3' scenario
IF ( y<0 and x>0) then n:=ATAN(y/x)+360; ///// 4'

And i'm not sure how to write it.

Code:
IF ( y>0) THEN
    IF (x>0) THEN
    n:=ATAN(y/x);
    ELSE
IF ( y>0) THEN
    IF( x<0) THEN
    n:=(ATAN(y/x))+pi;
    ELSE
IF ( y<0) THEN
    IF(x<0) THEN
    n:=(ATAN(y/x))+pi;
    ELSE
    n:=(ATAN(y/x))+(2*pi);

I have tried like that, but i't doesnt give the correct awnser. And also at end of program i have to write END; like 6 times or so.
Find all posts by this user
Quote this message in a reply
06-11-2021, 11:37 AM
Post: #2
RE: multi criteria IF statement
I would probably use a case statement for that task:

Code:
EXPORT Amer7scode(x,y)
BEGIN
 LOCAL n, orig;
 orig:=HAngle; 
 HAngle:=1;
 CASE
  IF y>0 AND x>0 THEN n:=ATAN(y/x) END;
  IF y>0 AND x<0 THEN n:=ATAN(y/x)+180 END;
  IF y<0 AND x<0 THEN n:=ATAN(y/x)+180 END;
  IF y<0 AND x>0 THEN n:=ATAN(y/x)+360 END;
  DEFAULT n:="error"
 END;
 HAngle:=orig;
 RETURN n;
END;

-road
Find all posts by this user
Quote this message in a reply
06-11-2021, 11:59 AM
Post: #3
RE: multi criteria IF statement
Thanks,
that worked like charm. I wonder where, and how you learn these tricks and expressions.

Here is the complete code for Directional Angle - Geodesy - HP Prime
Code:
EXPORT Directional()
BEGIN
LOCAL Ay,Ax,By,Bx;
INPUT({Ay,Ax,By,Bx},"Direkcioni", {"Y tacke A","X tacke A","Y tacke B","X tacke b"});

LOCAL y,x,n,c;
y:=By-Ay;
x:=Bx-Ax;

CASE
  IF y>0 AND x>0 THEN n:=ATAN(y/x) END;
  IF y>0 AND x<0 THEN n:=ATAN(y/x)+180 END;
  IF y<0 AND x<0 THEN n:=ATAN(y/x)+180 END;
  IF y<0 AND x>0 THEN n:=ATAN(y/x)+360 END;
  DEFAULT n:="error"
 END;
 c:=→HMS(n);
 RETURN c;
END;
Find all posts by this user
Quote this message in a reply
06-11-2021, 02:04 PM
Post: #4
RE: multi criteria IF statement
(06-11-2021 11:06 AM)Amer7 Wrote:  Hello,

I'm trying to write a program for directional angle calculation.

Why write a function when one is already built in?

look up ARG(X+i*Y)

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
06-11-2021, 10:09 PM
Post: #5
RE: multi criteria IF statement
As a new prime user I appreciate seeing smaller programs even if they replicate built-in functions.

I have several questions.

1. How does the program know that the calculator is in degrees mode versus radians?

2. What does "c:=→HMS(n);" mean? It probably has to do with the HMS function
which I haven't used yet.

3. What is the english translation for the word "tacke"

Thanks.
Find all posts by this user
Quote this message in a reply
06-12-2021, 03:05 AM
Post: #6
RE: multi criteria IF statement
I can answer the 1st 2

1 there is a system varable HAngle you can set to 0 for radian and 1 for degrees, by default it is set in the setup screen or app

2 →HMS() changes a degree to DMS
HMS→() changes DMS to degree

3 I dont know
Find all posts by this user
Quote this message in a reply
06-12-2021, 11:53 AM
Post: #7
RE: multi criteria IF statement
(06-12-2021 03:05 AM)Dougggg Wrote:  there is a system varable HAngle you can set to 0 for radian and 1 for degrees, by default it is set in the setup screen or app

On HP Prime emulator, HAngle cannot be set to 0 in CAS (Why?)

CAS> HAngle := 0       → "HAngle(0) Error: Bad Argument Value"

It might be better to use AAngle instead, see http://edspi31415.blogspot.com/2018/02/h...mcalc.html

Code:
EXPORT bearing(x,y) // x=north, y=east
BEGIN
IF x = 0 THEN       // avoid divide-by-0
    y := sign(y)*90;
ELSE
    AAngle := 2;    // degree mode
    y := atan(y/x) + (x<0)*180;
    AAngle := 0;    // restore
END;
return →HMS(y MOD 360);
END;

(06-11-2021 02:04 PM)toml_12953 Wrote:  Why write a function when one is already built in?

look up ARG(X+i*Y)

Because there is a bug in ARG ?
see https://www.hpmuseum.org/forum/thread-16977.html

We can use atan2 instead.

Code:
EXPORT bearing(x,y) // x=north, y=east
BEGIN
AAngle := 2;        // degree mode
y := atan2(y,x);
AAngle := 0;        // restore
return →HMS(y MOD 360);
END;
Find all posts by this user
Quote this message in a reply
06-12-2021, 02:28 PM
Post: #8
RE: multi criteria IF statement
Don't forget to handle points on the vertical axis, including the origin. I'll arbitrarily assign 0 to atan(0,0). I haven't had nearly enough coffee yet and I'm not fluent in PPL, but in C++:

Code:
        if (x==0) {
            if (y == 0)
                n = 0;      // at the origin
            else
                n = 90 + 180*(y<0);  // on the y axis
        } else {
            // C++ atan() returns radians, so convert to degrees
            n = atan(y/x) * 180 / 3.141592653589792; // assume 1st quadrant
            if (x < 0) n += 180;    // adjust for quadrants 2 and 3
            else if (y < 0) n += 360; // adjust for quadrant 4
        }
Find all posts by this user
Quote this message in a reply
06-13-2021, 05:35 AM
Post: #9
RE: multi criteria IF statement
(06-12-2021 11:53 AM)Albert Chan Wrote:  
(06-12-2021 03:05 AM)Dougggg Wrote:  there is a system varable HAngle you can set to 0 for radian and 1 for degrees, by default it is set in the setup screen or app

On HP Prime emulator, HAngle cannot be set to 0 in CAS (Why?)

CAS> HAngle := 0       → "HAngle(0) Error: Bad Argument Value"

It might be better to use AAngle instead, see http://edspi31415.blogspot.com/2018/02/h...mcalc.html

Code:
EXPORT bearing(x,y) // x=north, y=east
BEGIN
IF x = 0 THEN       // avoid divide-by-0
    y := sign(y)*90;
ELSE
    AAngle := 2;    // degree mode
    y := atan(y/x) + (x<0)*180;
    AAngle := 0;    // restore
END;
return →HMS(y MOD 360);
END;

(06-11-2021 02:04 PM)toml_12953 Wrote:  Why write a function when one is already built in?

look up ARG(X+i*Y)

Because there is a bug in ARG ?
see https://www.hpmuseum.org/forum/thread-16977.html

The bug must have been fixed again. I get the proper 45 with all the examples in that thread.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
06-13-2021, 01:10 PM
Post: #10
RE: multi criteria IF statement
(06-13-2021 05:35 AM)toml_12953 Wrote:  
(06-12-2021 11:53 AM)Albert Chan Wrote:  Because there is a bug in ARG ?
see https://www.hpmuseum.org/forum/thread-16977.html

The bug must have been fixed again. I get the proper 45 with all the examples in that thread.

Strange... my G2 running firmware 14588 (2021 05 05) still has that bug (namely, arg(3+3.*i) in CAS view returns its answer in radians even when Prime is in Degrees mode). Make sure you're in CAS view, degree mode, and include the decimal point.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
06-13-2021, 03:19 PM
Post: #11
RE: multi criteria IF statement
(06-13-2021 01:10 PM)Joe Horn Wrote:  
(06-13-2021 05:35 AM)toml_12953 Wrote:  The bug must have been fixed again. I get the proper 45 with all the examples in that thread.

Strange... my G2 running firmware 14588 (2021 05 05) still has that bug (namely, arg(3+3.*i) in CAS view returns its answer in radians even when Prime is in Degrees mode). Make sure you're in CAS view, degree mode, and include the decimal point.

I just checked it again. Now it comes up with radians. I was sure I checked the angular mode before but I must not have. Of course in Home mode, it does work properly and that's the mode I always use so I never would have run into this bug.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
06-16-2021, 02:58 AM
Post: #12
RE: multi criteria IF statement
I found that while in CAS mode the prime allows HAngle to be set to one or two.

After an attempt to do

HAngle:=0

an error message appears, namely:

"HAngle(0) Error: Bad Argument Value"

I wonder if this a feature or a bug and if it is causing the problems
noted in this thread.

It seems that setting AAngle is not affected.

Thanks.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: