help with exact command. [Solved]
04-25-2016, 07:54 PM (This post was last modified: 04-25-2016 11:18 PM by Spybot.)
Post: #1
 Spybot Member Posts: 173 Joined: Feb 2015
help with exact command. [Solved]
Hello!

Yeah I know this may seem ridiculously easy, but I'm trying to convert 0.333 to 1/3 ... but it resulted to be not as simple as I thought, perhaps someone has another approach!?

EXPORT z()
BEGIN
LOCAL M;
5▶HFormat;
3▶HDigits;
M:=0.333;
M:=exact(M);
PRINT(M);
END;

Thank You.

Spybot.
04-25-2016, 08:13 PM
Post: #2
 Didier Lachieze Senior Member Posts: 1,169 Joined: Dec 2013
RE: help with exact command.
The issue is that 0.333 is quite different from 1/3, how can the Prime guess that the result you're expecting is the fraction 1/3 while the fraction 333/1000 is exactly equal to 0.333?

You need to provide at less 10 significant digits to get 1/3: exact(0.3333333333)=1/3
04-25-2016, 08:33 PM (This post was last modified: 04-25-2016 08:56 PM by Spybot.)
Post: #3
 Spybot Member Posts: 173 Joined: Feb 2015
RE: help with exact command.
(04-25-2016 08:13 PM)Didier Lachieze Wrote:  The issue is that 0.333 is quite different from 1/3, how can the Prime guess that the result you're expecting is the fraction 1/3 while the fraction 333/1000 is exactly equal to 0.333?
You need to provide at less 10 significant digits to get 1/3: exact(0.3333333333)=1/3

I can make 0.333 to become 1/3 by changing some HOMEsettings [number format:rounded & HDigits to 3]... it'll do the trick though, the thing is that... when I try to replicate those settings from within a program and attempt to do the same operation... it's not working as expected!

EXPORT z()
BEGIN
LOCAL M;
5▶HFormat;
3▶HDigits;
M:=0.333;
M:=exact(M);
PRINT(M);
END;

Spybot.
04-25-2016, 09:27 PM (This post was last modified: 04-25-2016 09:27 PM by DrD.)
Post: #4
 DrD Senior Member Posts: 1,124 Joined: Feb 2014
RE: help with exact command.
Didier has given you the answer. Another way to do it would be to change:

Then you would be able to see that approx(M) = 0.333 (to what ever your settings are), while exact(M) = 1/3.

-Dale
04-25-2016, 10:20 PM
Post: #5
 Didier Lachieze Senior Member Posts: 1,169 Joined: Dec 2013
RE: help with exact command.
(04-25-2016 08:33 PM)Spybot Wrote:  I can make 0.333 to become 1/3 by changing some HOMEsettings [number format:rounded & HDigits to 3]... it'll do the trick though, the thing is that... when I try to replicate those settings from within a program and attempt to do the same operation... it's not working as expected!

Changing the HOME settings as you do has an impact on the [a b/c] key in Home but not on the CAS exact() function which depends on the value of the CAS epsilon setting.

The following program does what you're looking for:
Code:
EXPORT z() BEGIN LOCAL M, saved_ep; saved_ep:=CAS("epsilon"); CAS("epsilon:=0.001"); M:=0.333; M:=exact(M); PRINT(M); CAS("epsilon:=saved_ep"); RETURN M; END;

Note that the line:
Code:
CAS("epsilon:=saved_ep");
works only with the latest firmware version (10077) which allows the CAS to access local variables. With the previous version (8151) epsilon is not restored and remains at 0.001 at the end of the program.
04-25-2016, 11:18 PM (This post was last modified: 04-26-2016 12:30 AM by Spybot.)
Post: #6
 Spybot Member Posts: 173 Joined: Feb 2015
RE: help with exact command.
Yes, that's what I'm talking about, I didn't expect epsilon had something to do with the thing I was trying to do here, but I see it definitely has. in fact this is exactly what I was looking for, Thanks Didier.

A couple of hours ago I did notice that, no mater what changes I made to the HOME settings, they did't affect the way CAS was treating objects in my program. but since epsilon is a CAS setting, I see it does have an impact. Now I found a use for this setting(epsilon), which I used to consider as an untouched setting.

Spybot.
04-27-2016, 06:02 AM (This post was last modified: 04-27-2016 03:26 PM by Spybot.)
Post: #7
 Spybot Member Posts: 173 Joined: Feb 2015
RE: help with exact command. [Solved]
Didier Lachieze : Hello....

We can actually restore epsilon on FW 8151...

EXPORT z()
BEGIN
LOCAL M;
PRINT;
CAS("0.001▶epsilon");
PRINT("temporary epsilon ="+CAS("epsilon"));
M:=0.333;
M:=exact(M);
PRINT("number...= "+M);
CAS("0.00000000001▶epsilon");
END;

I know the User might have a different value assigned to epsilon, that's the part I'm still figuring out.

Spybot.
04-27-2016, 08:03 AM
Post: #8
 Didier Lachieze Senior Member Posts: 1,169 Joined: Dec 2013
RE: help with exact command. [Solved]
(04-27-2016 06:02 AM)Spybot Wrote:  We can actually restore epsilon on FW 8151...
[…]
I know the User might have a different value assigned to epsilon, that the part I'm still figuring out.

Yes it's possible with 8151 but it's much simpler with the latest firmware thanks to the CAS access to local variables …

On 8151, instead of restoring a fixed value as in your example:
Code:
CAS("0.00000000001▶epsilon");
you have to build a string with the saved value of epsilon like that:
Code:
STRING(saved_epsilon)+"▶epsilon"▶restore_epsilon; CAS(EVAL(restore_epsilon));

So this program should work on both 8151 and 10077 (not tested on 8151 as I don’t have a 8151 firmware on hand):
Code:
EXPORT z() BEGIN LOCAL M, saved_epsilon, restore_epsilon; PRINT; CAS("epsilon")▶saved_epsilon; CAS("0.001▶epsilon"); PRINT("temporary epsilon ="+CAS("epsilon")); M:=0.333; M:=exact(M); PRINT("number...= "+M); STRING(saved_epsilon)+"▶epsilon"▶restore_epsilon; CAS(EVAL(restore_epsilon)); PRINT("epsilon already restored="+CAS("epsilon")); END;
04-28-2016, 07:58 AM (This post was last modified: 04-28-2016 07:59 AM by retoa.)
Post: #9
 retoa Member Posts: 168 Joined: Jan 2015
RE: help with exact command. [Solved]
I wrote a little program to transform periodic numbers into fractions:

Code:
 EXPORT periodo(n,la) BEGIN LOCAL a,b,e:=0,num,den;  a:=n*10^la;  b:=IP(a);  WHILE FP(a)≠0 DO   a:=10*a;   e:=e+1;  END;  num:=a-b;  den:=10^(e+la)-10^la;  RETURN({numer(num/den),denom(num/den)}); END;
n is the number you want to transform with the periodical part written only once
la is the length of the antiperiod, 0 if no antiperiod
it returns a list with numerator and denominator of the fracion.

examples:

0.33333333
periodo(0.3,0) -> {1, 3}

2.353535353535
periodo(2.35,0) -> {233, 99}

3.456767676767 (the periodical part is 67, the antiperiod is 45, 2 digit length)
periodo(3.4567,2) -> {17111, 4950}

It should work independently of the number format settings.
04-28-2016, 11:13 AM
Post: #10
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: help with exact command. [Solved]
thank you, retoa, nice program :-)

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
04-28-2016, 04:30 PM (This post was last modified: 04-28-2016 04:31 PM by Spybot.)
Post: #11
 Spybot Member Posts: 173 Joined: Feb 2015
RE: help with exact command. [Solved]
Yes definitely a nice program, it simulates very much what the [a b/c] key does, I'm sure it can be used in many programs.

Thank you for sharing.

Spybot.
 « Next Oldest | Next Newest »

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