float / double support in Python?

Brandon Van Every vanevery at 3DProgrammer.com
Wed Feb 12 07:15:00 EST 2003


Martijn Faassen wrote:
> Brandon Van Every <vanevery at 3dprogrammer.com> wrote:
>>
>> How are you "forced" to care?
>
> I'd say in C++ you're forced to care. You get things like "Large Scale
> C++ Programming", a great book, but dealing with lots of low level
> details ("don't use unsigned in the interface",

Hm, I use unsigned in my interfaces all the time.  But of course, it's low
level game code.  For some problems, I want all the storage I can get out of
my bitfields.

> "don't use short in the interface")

I started out using some shorts in my game.  Lately I've been thinking
better of it and depreciating it to ints.  16-bit shorts can make some sense
on an Intel architecture, but they don't make any sense anywhere else.
Although, oddly enough, some other poster thought we should all be using
2-byte chars, and of course those are 16 bits.  :-)

I don't see that I've been "forced to care" about this however.  I *chose*
to care.  Big difference.

> A language feature that is there will have to be considered for use.
> Float versus double will make people wonder about what to use,

I don't see what the big deal is here.  The issues are:
- one's bigger than the other
- one's more accurate than the other
- one's faster for certain operations than the other, but they're the same
for many operations
- conversions between the two can be a performance hit

These aren't terribly tricky issues.  It's not like they have radically
different encodings or purposes.

> and
> what happens on the intersection when converting the one to the other,

But you're already forcing this issue when you cross from Python to C++
anyways.  Some 3D APIs like DirectX only take 4-byte floats.  You only get
to ignore the issue if you code in pure Python.

> and what happens on a library boundary where the library writer used
> float throughout and I used double, is anything going to go wrong,
> etc.

I find it pretty irritating that Python, knowing full well it would be using
C as a lower level component, chose to call the 8-byte floating point type a
"float."  In C this is a "double."  Single precision vs. double precision
are well defined IEEE 754 / 854 standard terms, and were so long before
Python.  If you're worried about people getting confused, this is the most
confusing thing the Python designers could have done.

> Obviously Python has mechanisms (like the struct and array modules)
> to get to basic machine types. They're just not a core part of the
> language but are in libraries, where they belong. :)

"Basic machine types belong in libraries" is not real world enough for me.

> Remember that Python is often used as a glue language.

Pretty weak glue if it doesn't understand single-precison floats natively
and I need to deal with an API that takes only single-precison floats.  I
thank the makers for the busywork.

--
Cheers,                         www.3DProgrammer.com
Brandon Van Every               Seattle, WA

20% of the world is real.
80% is gobbledygook we make up inside our own heads.





More information about the Python-list mailing list