The software library here has an excellent program with two routines that switch from floating mode to integer mode or back. It says that if the floating value is non integer it will be truncated. Unfortunately there is a bug in it. Sometimes it will truncate, e.g. 1234.567 will convert to 1234 correctly. But 1.2 converts to 872415233 decimal, instead of 1. I can't get my mind around what is happening. Can you help? Or suggest coding to work around the problem?
The reason this is an issue is that if for example you are in float mode with 3 decimal places and the number is 1.0001, you see it as 1.000. But it will convert incorrectly.
Here is the code:
# HP-16C Integer-to-Float/Float-to-Integer
# -Jamie Cox
# Usage:
# Press GSB A to convert a float number to 32-bit HEX
# Press GSB 9 to convert an integer to float
# Mnemonic: A is a hex digit, 9 is a decimal digit.
# A & 9 are at opposite ends of the keypad
#
#001 43,22,A g LBL A # Convert Float to Integer
002 23 HEX # Leaves value in Y and shift count in X
003 42 F RRn # Apply shift
004 2 2 # 20 Hex gives 32-bit wordsize
005 0 0
006 42 44 f WSIZE
007 43,4,3 g SF 3 # display leading zeros
008 43 21 g RTN009 43,22,9 g LBL 9 # Convert Integer to Float
010 0 0
011 42, 45,3 f FLOAT 3 # Set # digits after decimal
012 43 21 g RTN # That was easy
Edited: 5 July 2008, 6:30 p.m.