Programming the INT and FRAC functions
01-03-2016, 01:35 PM (This post was last modified: 01-03-2016 01:35 PM by Dieter.)
Post: #1
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
Programming the INT and FRAC functions
Reading this thread I came across TI's SR-52/SR-56 brochure. There is a feature list, showing that the SR-56 included some functions that were not yet available on the (otherwise more capable) SR-52. For instance the respective table says that the SR-52 did not feature functions for the integer and fractional part of a number – commands that today are considered common on a decent programmable calculator. However, TI says that while these functions are missing, they can be programmed.

So here is my question for the community: how would you program an INT or FRAC function on a "classic" calculator of the Seventies. For instance, imagine the HP67/97 or the 19C/29C would not offer such functions. How would you code them on these calculators – or on a SR-52? Of course as short, fast and elegant as possible. ;-)

Dieter
01-03-2016, 02:39 PM (This post was last modified: 01-03-2016 02:42 PM by J-F Garnier.)
Post: #2
 J-F Garnier Member Posts: 298 Joined: Dec 2013
RE: Programming the INT and FRAC functions
The INT and FRAC functions were very useful on machines of the time, to do things such as rounding a value to a certain number of decimal places (e.g. 100 * 0.5 + INT 100 / to round a positive value to two decimal places), or to extract the mantissa and/or exponent of a number (e.g. ENTER LOG INT 10^x / to get the mantissa - multiplied by 10).

I don't know any way to simulate the INT and FRAC functions, except if there is a way to round a number to a certain number of decimal places, which is the case of the SR-52, isn't it? ;-)

J-F
01-03-2016, 02:45 PM
Post: #3
 Thomas Klemm Senior Member Posts: 1,448 Joined: Dec 2013
RE: Programming the INT and FRAC functions
Assuming the calculator is in DEG mode this could be used for the FRAC function:

Code:
.5 - 360 * 1 P-R R-P RDN 360 / .5 +

As this is rather MOD 1 it doesn't work for negative numbers. But that could probably be fixed easily.

Thanks for the challenge.
Thomas
01-03-2016, 04:06 PM
Post: #4
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
Some links to the SR-52 Notes from 1976 on this topic:
01-03-2016, 07:23 PM (This post was last modified: 01-04-2016 07:14 AM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-03-2016 04:06 PM)Didier Lachieze Wrote:  Some links to the SR-52 Notes from 1976 on this topic:

Thank you very much.
Here's a quote from the very first issue of "52 notes":
Quote:I expect that so far more users have spent more time trying to perfect integer/fraction truncation routines than any other.

Looks like the int/frac topic at that time had opened a can of worms. ;-)
And TI seems to have noticed it. Only a few months later the SR-56 offered these functions.

All solutions for the SR-52 I've seen so far seem to use TI's display rounding procedure EE INV EE, so they will work only for some TI models. I still wonder if there is a short and effective way of implementing an INT or FRAC function on other calculators...?

Dieter
01-04-2016, 12:07 AM
Post: #6
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-03-2016 07:23 PM)Dieter Wrote:  All solutions for the SR-52 I've seen so far seem to use TI's display rounding procedure EE INV EE, so they will work only for some TI models. I still wonder if there is a short and effective way of implementing an INT or FRAC function on other calculators...?

On the HP-55, for INT you can do :
Code:
g H.MS g H.MS EEX 9 + f LastX -
And for FRAC :
Code:
ENTER g H.MS g H.MS EEX 9 + f LastX - -
This works for positive numbers but not for negative ones and I'm not sure it fits your definition of "short and effective"...
01-04-2016, 06:58 AM (This post was last modified: 01-04-2016 07:11 AM by Dieter.)
Post: #7
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 12:07 AM)Didier Lachieze Wrote:  On the HP-55, for INT you can do :
Code:
g H.MS g H.MS EEX 9 + f LastX -
And for FRAC :
Code:
ENTER g H.MS g H.MS EEX 9 + f LastX - -

Fine. Omit the two H.MS commands and you get a RND 0 function. ;-)

(01-04-2016 12:07 AM)Didier Lachieze Wrote:  This works for positive numbers but not for negative ones

Change the E9 into 2E9 and it works for negative numbers as well.

(01-04-2016 12:07 AM)Didier Lachieze Wrote:  and I'm not sure it fits your definition of "short and effective"...

I think it won't get much shorter, so yes, it fits.

BTW, I sometimes use a similar technique for rounding numbers to a certain number of digits without having to change the display settings. Just take a lower constant like 2E+8, 2E+7, ... etc.

Dieter
01-04-2016, 04:41 PM
Post: #8
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 06:58 AM)Dieter Wrote:  Change the E9 into 2E9 and it works for negative numbers as well.
Thanks, I don't know why I missed this yesterday... maybe it was too late for me.
Anyway, here are the updated sequences for the HP-55:

INT:
Code:
g H.MS g H.MS 2 EEX 9 + f LastX -

FRAC:
Code:
ENTER g H.MS g H.MS 2 EEX 9 + f LastX - -
01-04-2016, 05:21 PM
Post: #9
 Marcus von Cube Senior Member Posts: 754 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 04:41 PM)Didier Lachieze Wrote:
Code:
g H.MS g H.MS ...

What is the reasoning behind this sequence?

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
01-04-2016, 05:52 PM
Post: #10
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
To reduce the fractional part below 0.5 to avoid rounding issue when adding 2E9.
01-04-2016, 07:34 PM (This post was last modified: 01-04-2016 07:56 PM by Dieter.)
Post: #11
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 05:21 PM)Marcus von Cube Wrote:
(01-04-2016 04:41 PM)Didier Lachieze Wrote:
Code:
g H.MS g H.MS ...

What is the reasoning behind this sequence?

The first →H.MS reduces the fractional part to less than 0,6 and after the second →H.MS it's below 0,36. This way the rounding procedure always rounds down to the next lower integer. You could just as well use [.] [5] [–], but [→H.MS] [→H.MS] is one step shorter. ;-)

BTW, this method still is not perfect. It works up to x = 8E+9.

Dieter
01-04-2016, 07:45 PM (This post was last modified: 01-04-2016 07:45 PM by Didier Lachieze.)
Post: #12
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 07:34 PM)Dieter Wrote:  You could just as well use [.] [5] [–], but [→H.MS] [→H.MS] is one step shorter. ;-)

On the HP-55 [→H.MS] [→H.MS] uses 4 steps (keystrokes are not merged) so it's one step longer, but it's better because [.] [5] [–] doesn't work with negative numbers.
01-05-2016, 06:52 AM (This post was last modified: 01-05-2016 07:04 AM by Dieter.)
Post: #13
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-04-2016 07:45 PM)Didier Lachieze Wrote:  On the HP-55 [→H.MS] [→H.MS] uses 4 steps (keystrokes are not merged) so it's one step longer, but it's better because [.] [5] [–] doesn't work with negative numbers.

It depends – you might just as well say that it works even better. ;-)
Using [.] [5] [–] yields a FLOOR function. This is the way "INT" is implemented in many computer languages: it returns the largest integer ≤ x.
This way also a CEIL function could be added, since ceil(x) = –floor(–x).

Dieter
01-05-2016, 07:19 AM
Post: #14
 Didier Lachieze Senior Member Posts: 1,100 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-05-2016 06:52 AM)Dieter Wrote:
(01-04-2016 07:45 PM)Didier Lachieze Wrote:  On the HP-55 [→H.MS] [→H.MS] uses 4 steps (keystrokes are not merged) so it's one step longer, but it's better because [.] [5] [–] doesn't work with negative numbers.

It depends – you might just as well say that it works even better. ;-)
Using [.] [5] [–] yields a FLOOR function. This is the way "INT" is implemented in many computer languages: it returns the largest integer ≤ x.
This way also a CEIL function could be added, since ceil(x) = –floor(–x).

Dieter

Yes, FLOOR and CEIL are useful functions but in my mind they are different from INT and FRAC whose usage is to split numbers and get either the integer or the fractional part.
01-05-2016, 12:42 PM
Post: #15
 Pekis Member Posts: 95 Joined: Aug 2014
RE: Programming the INT and FRAC functions
Hello,

Beware of the internal precision before applying INT ...

e.g. with a 12-digit mantissa and 10 digits displayed:

While the number 15.9999999999 (kept in internal memory) is displayed as 16, should the INT function have to display 15 (applying INT directly) or 16 (Rounding to 10 digits then applying INT) ?
01-05-2016, 07:37 PM
Post: #16
 Dieter Senior Member Posts: 2,400 Joined: Dec 2013
RE: Programming the INT and FRAC functions
(01-05-2016 12:42 PM)Pekis Wrote:  While the number 15.9999999999 (kept in internal memory) is displayed as 16, should the INT function have to display 15 (applying INT directly) or 16 (Rounding to 10 digits then applying INT) ?

Of course any function, including INT, should work on the exact value and not on the one rounded to display precision. Just imagine what would happen in FIX 0 mode. So the result of course has to be 15.

If you want to do it the other way round, simply have the number explicitely rounded before the function is called.

It's the programmer's job to take care of results that may come out as 15,9999999999 although the expected value is 16. This applies to any function. Even a simple x=y? test may go wrong. Luckily most calculators use BCD arithimetics – in binary things may be much worse.

Dieter
01-08-2016, 08:50 AM
Post: #17
 damaltor Member Posts: 230 Joined: Dec 2015
RE: Programming the INT and FRAC functions
Well, i just tried something on the 11c (which has int and frac, but this might similarly work on other calcs, so i tried it anyway).

This will only work if you usually use the same display mode, e.g. FIX 3.

For INT:
Code:
42,21,11   LBL A    42, 2   ->H.MS    42, 2   ->H.MS 42, 7, 0   FIX 0    43,34   RND 42, 7, 3   FIX 3 (or whatever your disp mode is)    43,32   RTN

Explanation:
the two ->H.MS bring the fractional part below 0.5 so that the number is always rounded down. FIX0 rounds the number in the display, RND rounds the number internally to the displayed value, and FIX3 brings back the old display setting (ad has to be replaced by your favourite display mode).

For FRAC:
Code:
42,21,12   LBL B       36   ENTER    32,11   GSB A       30   -    43,32   RTN

Explanation:
The number gets copied to y, then the INT function is run. afterwards the integer part is subtracted from the number in y, so that only the fractional part stays displayed.

This works well and needs 12 steps. Still it only works only with one display mode (but as i personally mostly use ENG3 i can live with that). Also, the T register gets destroyed by using FRAC.
 « Next Oldest | Next Newest »

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