Post Reply 
What is wrong with my code?
01-20-2021, 09:05 PM (This post was last modified: 11-30-2022 08:19 PM by Spybot.)
Post: #1
What is wrong with my code?
Hi!
I'm just trying to get the distance between 2 points and show the result in exact format, nothing fancy.... all I get is an "r" as a result. I'm using the latest version of the HP Primie Emulator.

My Code:

Code:

EXPORT DBTP()
BEGIN
LOCAL a:=0,b:=0,c:=0,d:=0,r:=0;
PRINT();
INPUT({{a,[0],{10,20,1}},{b,[0],{40,20,1}},{c,[0],{10,20,2}},{d,[0],{40,20,2}}},"Distance between 2 Points",{"X₁:","Y₁:","X₂:","Y₂:"},{"Input a value for X₁ (Real number).","Input a value for Y₁ (Real number).","Input a value for X₂ (Real number).","Input a value for Y₂ (Real number)."});
IF 0 THEN BREAK;
END;
r:=√((c-a)^2+(d-b)^2);
r:=exact(r);
PRINT("
  Distance between points:
  "+r);
END;
[attachment=9037]

Spybot.
Find all posts by this user
Quote this message in a reply
01-21-2021, 05:13 AM (This post was last modified: 01-21-2021 05:14 AM by rawi.)
Post: #2
RE: What is wrong with my code?
I think the problem is your print command before the final end command.
Replace it by
PRINT("
Distance between points:
"+r);
and it works.
If I try with 1,2,3,4 being a,b,c,d respectively I get:

Distance between points:
665857/235416

Best
Find all posts by this user
Quote this message in a reply
01-21-2021, 03:17 PM
Post: #3
RE: What is wrong with my code?
(01-21-2021 05:13 AM)rawi Wrote:  If I try with 1,2,3,4 being a,b,c,d respectively I get:

Distance between points:
665857/235416

The exact result is 2*sqrt(2), not that ungainly fraction.

V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
01-21-2021, 08:27 PM (This post was last modified: 01-21-2021 08:39 PM by C.Ret.)
Post: #4
RE: What is wrong with my code?
Hello,

Please excuse my dummy question : What is the purpose of this section ?
Code:
IF 0 THEN BREAK;
END;


P.S.: Running the numeric example {1, 2 , 3 4 for respectively a,b,c and d) directly from 'HOME' command line I got the expected 2*√2 exact response.

By running the original code, I get the same "ungainly" 665857/235416 ratio than rawi.

Apparently, the small roundoff error due to storing the value in the r variable makes the EXACT function not to 'detect' the square root.

Here is how I modified the initial code to get the expected exact result:

Code:
EXPORT DBTP()
BEGIN
LOCAL a:=0,b:=0,c:=0,d:=0;
PRINT();
IF INPUT({{a,[0],{10,20,1}},{b,[0],{40,20,1}},{c,[0],{10,20,2}},{d,[0],{40,20,2}}},
            "Distance between 2 Points",
            {"X₁:","Y₁:","X₂:","Y₂:"},
            {"Input a value for X₁ (Real number).","Input a value for Y₁ (Real number).","Input a value for X₂ (Real number).","Input a value for Y₂ (Real number)."})
   ==0 THEN BREAK; END;

LOCAL r:=exact(√((c-a)^2+(d-b)^2));

PRINT("
  Distance between points:
  "+r+"");
END;

The test catch any zero value produce when INPUT is cancel by the user.

Since the variable r is most symbolic than a number, I don't initalize it to a real value, nor I store any real value in it making the EXACT function "ungainly".


Hope this help a few ...
Find all posts by this user
Quote this message in a reply
01-23-2021, 01:25 AM
Post: #5
RE: What is wrong with my code?
Hello!
Thank you for your answers.

Using C.Ret's code and I'm getting this as a result: √((c-a)^2+(d-b)^2)
Something happened along the way something broke the code. The little code I posted is just a tiny fraction of a 7k line program I wrote a couple of years ago, everything use to work just fine and then something happened and all my program is broken.

Spybot.
Find all posts by this user
Quote this message in a reply
01-23-2021, 09:55 AM
Post: #6
RE: What is wrong with my code?
Hello,
It happened to me as well. For this reason I break my code in small pieces inside the same program. While it seems longer to manage the variables, there are huge advantages debugging the code.

Programming style makes the difference sometimes. I am not saying that this is a possible reason for this problems but this certainly simplified my programming sessions.

Imho the Prime handle very well splitting the code in small pieces. If we could just have the line numbers in the editor like the one in the connectivity kit...

I am one of those who likes programming on the phisical calculator since I use my laptop all the day...

My 0,000001 cent,

Giancarlo
Find all posts by this user
Quote this message in a reply
01-23-2021, 02:30 PM
Post: #7
RE: What is wrong with my code?
(01-23-2021 01:25 AM)Spybot Wrote:  Using C.Ret's code and I'm getting this as a result: √((c-a)^2+(d-b)^2)

Hi, Spybot

It seems whatever entered into a,b,c,d does not stick.

I did a calculator reset before copy/paste your original code.
Except for the fact that it gives the "ungainly" convergent, everything else seems OK.

(01-21-2021 08:27 PM)C.Ret Wrote:  Since the variable r is most symbolic than a number, I don't initalize it to a real value,
nor I store any real value in it making the EXACT function "ungainly".

This may not be the reason for getting the "ungainly" convergent.

r := exact(√((c-a)^2+(d-b)^2));

In HOME mode, above is *not* the same as

r := √((c-a)^2+(d-b)^2);
r := exact(r);

HOME> r := sqrt(8)       // 2.82842712475
HOME> exact(r)            // 665857/235416

Storing √8 into r also turned it non-exact.
exact(r) does not turn float to exact, only a good convergent.

Combining 2 assignments into 1, we *mostly* avoided the non-exact path.
But, this assumed all inputs are integers.

Example, (a,b) = (1.,2.) , (c,d) = (3.,4.4):

r := exact(√((c-a)^2+(d-b)^2));                    // r = 2971372/951113

The more correct way is to rationalize the inputs.

r := simplify(√(exact(c-a)^2+exact(d-b)^2)); // r = 2*√61/5
Find all posts by this user
Quote this message in a reply
01-23-2021, 07:56 PM
Post: #8
RE: What is wrong with my code?
(01-23-2021 02:30 PM)Albert Chan Wrote:  [...]
Combining 2 assignments into 1, we *mostly* avoided the non-exact path.
But, this assumed all inputs are integers.

Thanks, that the point I missed. Your strategy is by far more efficient:

r := simplify(√(exact(c-a)^2+exact(d-b)^2)); // \( r = \frac{2\times \sqrt{61}}{5} \)


I still in trouble reproducing the 'non ticking' behavior of SpyBot's results.

Is there a way to avoid the evaluation of an expressions ?
Find all posts by this user
Quote this message in a reply
01-24-2021, 03:31 AM
Post: #9
RE: What is wrong with my code?
(01-23-2021 01:25 AM)Spybot Wrote:  Hello!
Thank you for your answers.

Using C.Ret's code and I'm getting this as a result: √((c-a)^2+(d-b)^2)
Something happened along the way something broke the code. The little code I posted is just a tiny fraction of a 7k line program I wrote a couple of years ago, everything use to work just fine and then something happened and all my program is broken.

It was probably the result of changes to the way CAS commands are parsed. My memory is hazy, but I vaguely recall EVAL() being required to pass a variable by value. Otherwise, variables are just symbolic names if passed to a CAS command without the EVAL() command.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
01-24-2021, 03:40 PM
Post: #10
RE: What is wrong with my code?
Hi Han!

Yes that was problem, adding the EVAL command to the original code like this:
r:=EVAL(exact(r));
Makes it work again.

Thank you.

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




User(s) browsing this thread: 1 Guest(s)