Convert decimal > fraction in program
12-09-2015, 08:23 PM (This post was last modified: 12-09-2015 08:36 PM by jrozsas.)
Post: #1
 jrozsas Member Posts: 158 Joined: Nov 2014
Convert decimal > fraction in program
In a program, what is the way to convert decimal to fraction and display it on the screen? Example: 5.25 = 21/4.
The key has a command for programming?

Leo

12-09-2015, 09:01 PM (This post was last modified: 12-09-2015 09:09 PM by Marcus von Cube.)
Post: #2
 Marcus von Cube Senior Member Posts: 760 Joined: Dec 2013
RE: Convert decimal > fraction in program
exact(5.25) in CAS returns 21/4.

Edit: Works in Home as well.

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
12-09-2015, 10:56 PM
Post: #3
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Convert decimal > fraction in program
(12-09-2015 09:01 PM)Marcus von Cube Wrote:  exact(5.25) in CAS returns 21/4.

Edit: Works in Home as well.

Thanks Marcus!.
it works perfectly (cas and Home).

Leo

12-09-2015, 11:55 PM
Post: #4
 StephenG1CMZ Senior Member Posts: 812 Joined: May 2015
RE: Convert decimal > fraction in program
You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

Stephen Lewkowicz (G1CMZ)
The word "set" has the most definitions in the OED
12-10-2015, 10:18 AM (This post was last modified: 12-10-2015 10:19 AM by Dirk..)
Post: #5
 Dirk. Junior Member Posts: 41 Joined: Mar 2015
RE: Convert decimal > fraction in program
A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.
12-10-2015, 07:21 PM
Post: #6
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Convert decimal > fraction in program
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.
Thanks,
I need more help. See this program:
// Program to edit and show Matrix
EXPORT MeuSimp()
BEGIN
RECT();
EDITMAT(M1);
PRINT;
PRINT (exact(M1));
FREEZE;
END;
It works, but I want to see the matrix as in Figure 2

How to?

Leo

12-11-2015, 02:40 PM (This post was last modified: 12-11-2015 03:57 PM by StephenG1CMZ.)
Post: #7
 StephenG1CMZ Senior Member Posts: 812 Joined: May 2015
RE: Convert decimal > fraction in program
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

In a separate thread, Didier has pointed out that it is better to use STRING(Number,1) with numbers, as otherwise the string may lose precision. The modes can be combined by adding them, so I now suggest something like
MSGBOX(STRING(3.14,1+7)) or 1+14

As for printing a Matrix neatly, I do not know any simple solutions.

Stephen Lewkowicz (G1CMZ)
The word "set" has the most definitions in the OED
12-11-2015, 04:00 PM
Post: #8
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Convert decimal > fraction in program
(12-11-2015 02:40 PM)StephenG1CMZ Wrote:
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

In a separate thread, Didier has pointed out that it is better to use STRING(Number,1) with numbers, as otherwise the string may lose precision. The modes can be combined by adding them, so I now suggest something like
MSGBOX(STRING(3.14,1+7)) or 1+14
It is an interesting alternative StephenG1CMZ, but see that the goal was to show an matrix as in Figure 2, in a program that posted (MeuSimp). However, in both alternatives, does not appear to need.

Leo

12-11-2015, 04:10 PM
Post: #9
 StephenG1CMZ Senior Member Posts: 812 Joined: May 2015
RE: Convert decimal > fraction in program
(12-10-2015 10:18 AM)Dirk. Wrote:  A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.

Displaying 5 for 0.5 certainly looks wrong - the Android emulator does this too.

Stephen Lewkowicz (G1CMZ)
The word "set" has the most definitions in the OED
12-11-2015, 04:30 PM (This post was last modified: 12-11-2015 04:39 PM by jrozsas.)
Post: #10
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Convert decimal > fraction in program
The solution probably this in this command?

I did it and got this result, but the program does not.

The program with the command MSGBOX:

and the program with PRINT

Leo

12-11-2015, 04:30 PM
Post: #11
 Tim Wessman Senior Member Posts: 2,175 Joined: Dec 2013
RE: Convert decimal > fraction in program
(12-11-2015 04:10 PM)StephenG1CMZ Wrote:
(12-10-2015 10:18 AM)Dirk. Wrote:  A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.

Displaying 5 for 0.5 certainly looks wrong - the Android emulator does this too.

Yup. Already was on the list and I think corrected quite a while back. I'll check again though.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
12-11-2015, 04:58 PM (This post was last modified: 12-11-2015 05:01 PM by luisphysics.)
Post: #12
 luisphysics Member Posts: 54 Joined: Nov 2015
RE: Convert decimal > fraction in program
It was not corrected. Just checked this on my physical calculator.When you type .5 it turns into 5E-1 and ab/c gives 5 rather than 1/2.
This does not work in Scientific or Engineering.

I want to make a note though. It works in CAS mode but not in the non-CAS.

-Luis-
12-11-2015, 06:11 PM (This post was last modified: 12-11-2015 06:18 PM by jrozsas.)
Post: #13
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Convert decimal > fraction in program
I am close to the solution. See the result using "string" combined with "row".
// Program to edit and show Matrix
EXPORT MeuSimp()
BEGIN
LOCAL B,G;
RECT();
EDITMAT(M2);
B:=rowDim(M2);
PRINT;
FOR G FROM 1 TO B DO
PRINT(STRING(row(M2,G),7));
END;
FREEZE;
END;

Suggestions?Must present the results in form of matrix and and fractions. See M2 (in decimal):

Leo

12-11-2015, 06:57 PM (This post was last modified: 12-11-2015 07:00 PM by StephenG1CMZ.)
Post: #14
 StephenG1CMZ Senior Member Posts: 812 Joined: May 2015
RE: Convert decimal > fraction in program
I was wondering whether what is needed is not exact(matrix), but matrix[1 2]:=exact(1.5) - making each individual element exact one by one. Unfortunately, I don't think that can be done - when I tried it I got a syntax error.

However in a spreadsheet entering exact(1.5) into a cell does work.
Although only the current cell is displayed formatted, the other cells being displayed decimal.
But once you have the exact elements in a rectangular set of spreadsheet cells (or maybe just a list or array), you could then print them out; but it will need formatting - I don't think that will be better than your Row solution.

Unless someone else has a solution...

Stephen Lewkowicz (G1CMZ)
The word "set" has the most definitions in the OED
 « Next Oldest | Next Newest »

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