The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Truncating to Integer & Fraction--HP-35A/21A
Message #1 Posted by Matt Agajanian on 29 Mar 2012, 11:01 a.m.

Hello all. Fantastic job on the D.MS<-->D.DDD conversion routines. I tried developing some algorithms last night. In comparison, yours were quite streamlined.

Another pair of techniques I've been considering: How do I extract the fractional and integer parts of values. I tried using logs, adding/subtracting 1/2 to round the decimal portion out, etc. and a memory location with some arithmetic manipulation but my methods are pretty cumbersome.

Just as well, I've also been trying to create routines to truncate to n decimal places. My ideas were to add some power of 10 to the number and then subtract it down to the original number with the desired decimal. But, the internal rounding of the 35 would round my values up if the n+1th digit was >=5. Any thoughts?

Edited: 29 Mar 2012, 7:12 p.m. after one or more responses were posted

      
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #2 Posted by Luiz C. Vieira (Brazil) on 29 Mar 2012, 1:33 p.m.,
in response to message #1 by Matt Agajanian

HI.

Some calculators have these 'functions' already implemented

INT, IP - returns the integer portion of a number (*)
FRAC, FP - returns fractional part of a number

(*) The HP12C has INTGR, intended to make no confusion with INT, which refers to interest rate in financial calculators.

I am curious: which calculator you are willing to implement? Or is it just a matter of developing the algorithm yourself? I am asking these questions because I sometimes challenge myself by writing routines and algorithms to compute some calculations already implemented. Lately I am trying to implement integer, binary multiplication in microcontrollers with the fewest steps possible.

Cheers.

Luiz (Brazil)

Edited: 29 Mar 2012, 1:34 p.m.

            
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #3 Posted by Matt Agajanian on 29 Mar 2012, 7:10 p.m.,
in response to message #2 by Luiz C. Vieira (Brazil)

Hello there. As the message title indicates, I'm looking for an integer and fractional truncation routines for the 1972 HP-35 and its successor the HP-21.

Edited: 29 Mar 2012, 7:11 p.m.

                  
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #4 Posted by Eric Smith on 29 Mar 2012, 8:09 p.m.,
in response to message #3 by Matt Agajanian

As you may have figured out, there are actually reasons why some people bought midrange or high-end calculators, rather than the bottom of the line.

                  
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #5 Posted by Luiz C. Vieira (Brazil) on 29 Mar 2012, 10:44 p.m.,
in response to message #3 by Matt Agajanian

HI.

I failed to understand HP-35A/21A as both the HP35 and HP21, I actually did not figure that out.

In this case I second the other guys: I'd store the value in one available register (the HP21 has only one memory register out of the memory stack, IIRC) and key in the integer or factional part whenever needed. I'd guess that such resource in a non-programmable, ROM-limited calculator might not have been taken as essential, though.

Cheers.

Luiz (Brazil)

            
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #6 Posted by Gerson W. Barbosa on 29 Mar 2012, 7:44 p.m.,
in response to message #2 by Luiz C. Vieira (Brazil)

Hello Luiz,

Quote:
Lately I am trying to implement integer, binary multiplication in microcontrollers with the fewest steps possible.

On the Z80, I used to do the following. Not the fewest number of bytes, I fear:

	org $c000
	ld hl,0
	ld c,$ff
	ld de,$ff
loop:	ld a,c
	cp 0
	ret z 
	and 1
	jr z,loop0
	add hl,de
loop0:  srl c
	sla e
	rl d
	jr loop

Cheers,

Gerson.

                  
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #7 Posted by Luiz C. Vieira (Brazil) on 29 Mar 2012, 10:40 p.m.,
in response to message #6 by Gerson W. Barbosa

HI, Gerson.

Thank you, very interesting.

	org $c000
	ld hl,0
	ld c,$ff
	ld de,$ff
loop:	ld a,c
	cp 0
	ret z 
	and 1
	jr z,loop0  The key for the process...
	add hl,de   ... and the resulting product is in hl, right?
loop0:  srl c
	sla e
	rl d
	jr loop
I'll try to port it to the ATMEL structure, I think it is not going to be that hard. Thanks a lot!

Cheers.

                        
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #8 Posted by Gerson W. Barbosa on 29 Mar 2012, 10:58 p.m.,
in response to message #7 by Luiz C. Vieira (Brazil)

Hi Luiz,

It's based on Russian peasant multiplication. It looks like their peasants are more creative than ours. Remember this Semp Toshiba ad? :-)

Cheers,

Gerson.

                              
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #9 Posted by Matt Agajanian on 31 Mar 2012, 10:45 p.m.,
in response to message #8 by Gerson W. Barbosa

You know, that Russian Peasant Multiplication trick is pretty cool! They say you learn something new everyday. Well, this is the coolest one yet!

      
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #10 Posted by Andrés C. Rodríguez (Argentina) on 29 Mar 2012, 2:58 p.m.,
in response to message #1 by Matt Agajanian

IMHO, for that machine class, manual truncation is the way.

            
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #11 Posted by Gerson W. Barbosa on 29 Mar 2012, 5:19 p.m.,
in response to message #10 by Andrés C. Rodríguez (Argentina)

Ditto!

            
Re: Truncating to Integer & Fraction--HP-35A/21A
Message #12 Posted by Matt Agajanian on 31 Mar 2012, 10:36 p.m.,
in response to message #10 by Andrés C. Rodríguez (Argentina)

You've got a point there. But, even so, it was a challenging exercise in developing a unique algorithm.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall