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:
Spybot. |
|||
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 |
|||
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: The exact result is 2*sqrt(2), not that ungainly fraction. V. All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
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; 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() 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 ... |
|||
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. |
|||
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 |
|||
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, 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 |
|||
01-23-2021, 07:56 PM
Post: #8
|
|||
|
|||
RE: What is wrong with my code?
(01-23-2021 02:30 PM)Albert Chan Wrote: [...] 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 ? |
|||
01-24-2021, 03:31 AM
Post: #9
|
|||
|
|||
RE: What is wrong with my code?
(01-23-2021 01:25 AM)Spybot Wrote: Hello! 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 |
|||
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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)