strong/weak typing and pointers

Alex Martelli aleaxit at yahoo.com
Thu Nov 4 15:21:57 EST 2004


Steven Bethard <steven.bethard at gmail.com> wrote:

> Does this make my concern any clearer?  What I'm asking for is an example
> like yours above that not only shows that you can treat, say, the bits of
> a float as an integer, but also shows why this would be useful.

Given a float, extract the (so-called) "mantissa" (what a misnomer!) and
exponent.  Can you see the usefulness of _that_?  Can you see that
treating the bits that compose the float as an int and using masking and
shifting is the obvious way to perform this task?

Say I need to compute some unary float function, such as 'sin', with
high speed and precision.  One reasonable approach: normalize the float
input to a standard range (say 0 to pi/4, remembering what kind of sign
inversions &c you need to perform at result time); get "mantissa" (pah!)
and exponent and use the latter, partly to select the right lookup table
and partly to shift the mantissa appropriately to make it an index into
said result table, while keeping track of the bits that shifted out;
read out the result base and the multiplier for interpolation, multiply
the latter by the bits that shifted out and add the result to the result
base; perform sign or other symmetry inversions as previously recorded.

There -- you have the function computer to whatever precision is
requested, typically an ULP.  Depending on your CPU, you may have HW
that obviates the need for some or all of these manipulations - but you
won't have it for all transcendentals, and what you don't have in HW
you'll need to do in SW -- and being able to get at the bits of the
floating point representation is often the best approach for that task.

Although the details were more antiquated, that was elementary stuff
taught in electronic engineers' first-year computing courses back in the
'70s, just in case we ever needed to code our own transcendentals (in
Fortran, of course -- you weren't expected to master machine code unless
you took computing electives in 3rd and later years, much less exoterica
such as Pascal, Lisp or APL).  Is it considered advanced or specialized
these days?!


Alex



More information about the Python-list mailing list