[6.58] Extract Floating-Point Mantissa And Exponent - Texas Instruments TI-89 Tip List

Graphing calculator
Hide thumbs Also See for TI-89:
Table of Contents

Advertisement

local t,k,s
{0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}→t
0→s
for k,1,8
s+t[(n and 0hF)+1]→s
shift(n,⁻4)→n
endfor
return s
EndFunc
For example:
The last example results in a Warning: Operation requires and returns 32-bit value, but the correct
result is returned. 32-bit integers are interpreted as 2's compliment signed integers, so the decimal
range for valid input arguments is -2,147,483,648 to 2,147,483,647. This is 0h0 to 0hFFFF. (Recall that
the TI-89/TI-92 Plus use 0b to prefix binary integers, and 0h to prefix base-16 integers).
sum1s() uses a table lookup (in the list t) to find the number of 1's in each 4-bit nibble of the input
integer n. Each pass through the loop processes one nibble. The nibble is extracted by and-ing the
current value of n with 0hF. I add 1 to the nibble value since list indices start at 1, not zero. After
summing the correct list element, the input argument is shifted right four bits, so I can use the same bit
mask of 0hF to extract the next nibble. The elements of list t are the number of 1s in all possible
nibbles, in sequential order. For example, the nibble 0b0111 is decimal 7 which accesses the eighth
element of t, which is 3.
There are many other ways to accomplish this task. A comprehensive survey of seven methods is the
article Quibbles and Bits by Mike Morton (Computer Language magazine, December 1990). I chose
the table lookup method because it has a simple TI Basic implementation. The number of loop
iterations can be reduced by increasing the number of bits processed, but this increases the table size.
We could process eight bits at a time in four loop iterations, but the table would have 256 entries. The
method in sum1s() seems to be a good tradeoff between table size and loop iterations.
sum1s() executes in about 0.3 seconds/call. You could speed up sum1s() about 8% by making t a
global variable and initializing it before running sum1s(). An even faster version would be coded in C.
Unfortunately, a limitation in AMS 2.05 prevents this simple implementation, which would eliminate the
For loop overhead:
sum(seq(t[(shift(n,-k*4) and 0hF)+1],k,0,7))

[6.58] Extract floating-point mantissa and exponent

In some applications it is useful it obtain the mantissa and exponent of floating point numbers. In
scientific notation, any floating-point number can be expressed as a.bEc, where a.b is the mantissa, c
is the exponent, and a is not equal to zero. While the exponent and mantissa can be obtained starting
out with the log() function, some book keeping is required to account for negative arguments. The
function shown below returns the exponent and mantissa, and retains the full mantissa precision.
mantexp(n)
Func
©(n) return {mantissa,exponent}
©12may02/dburkett@infinet.com
local s,e
sum1s(0)
returns 0
sum1s(7)
returns 3
sum1s(0hF)
returns 4
sum1s(2^32-1)
returns 32
6 - 111

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Ti-92+

Table of Contents