HP Forums
help with exact command. [Solved] - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: help with exact command. [Solved] (/thread-6145.html)



help with exact command. [Solved] - Spybot - 04-25-2016 07:54 PM

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.


RE: help with exact command. - Didier Lachieze - 04-25-2016 08:13 PM

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


RE: help with exact command. - Spybot - 04-25-2016 08:33 PM

(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;


RE: help with exact command. - DrD - 04-25-2016 09:27 PM

Didier has given you the answer. Another way to do it would be to change:

M:=1/3; // Instead of M:=0.333

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

-Dale


RE: help with exact command. - Didier Lachieze - 04-25-2016 10:20 PM

(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.


RE: help with exact command. - Spybot - 04-25-2016 11:18 PM

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.

Thanks again for your help!


RE: help with exact command. [Solved] - Spybot - 04-27-2016 06:02 AM

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");
PRINT("epsilon already restored="+CAS("epsilon"));
END;

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


RE: help with exact command. [Solved] - Didier Lachieze - 04-27-2016 08:03 AM

(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;



RE: help with exact command. [Solved] - retoa - 04-28-2016 07:58 AM

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.


RE: help with exact command. [Solved] - salvomic - 04-28-2016 11:13 AM

thank you, retoa, nice program :-)

Salvo


RE: help with exact command. [Solved] - Spybot - 04-28-2016 04:30 PM

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.