473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Single precision floating point calcs?

I'm pretty sure the answer is "no", but before I give up on the
idea, I thought I'd ask...

Is there any way to do single-precision floating point
calculations in Python?

I know the various array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...

--
Grant Edwards grante Yow! -- I have seen the
at FUN --
visi.com
May 9 '07 #1
8 10367

"Grant Edwards" <gr****@visi.comwrote in message
news:13*************@corp.supernews.com...
| I'm pretty sure the answer is "no", but before I give up on the
| idea, I thought I'd ask...

| Is there any way to do single-precision floating point
| calculations in Python?

Make your own Python build from altered source. And run it on an ancient
processor/OS/C compiler combination that does not automatically convert C
floats to double when doing any sort of calculation.

Standard CPython does not have C single-precision floats.

The only point I can think of for doing this with single numbers, as
opposed to arrays of millions, is to show that there is no point. Or do
you have something else in mind?

Terry Jan Reedy

May 9 '07 #2
Grant Edwards wrote:
I'm pretty sure the answer is "no", but before I give up on the
idea, I thought I'd ask...

Is there any way to do single-precision floating point
calculations in Python?

I know the various array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...
We also have scalar types of varying precisions in numpy:
In [9]: from numpy import *

In [10]: float32(1.0) + float32(1e-8) == float32(1.0)
Out[10]: True

In [11]: 1.0 + 1e-8 == 1.0
Out[11]: False
If you can afford to be slow, I believe there is an ASPN Python Cookbook recipe
for simulating floating point arithmetic of any precision.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 9 '07 #3
On 2007-05-09, Terry Reedy <tj*****@udel.eduwrote:
>| I'm pretty sure the answer is "no", but before I give up on the
| idea, I thought I'd ask...
|
| Is there any way to do single-precision floating point
| calculations in Python?

Make your own Python build from altered source. And run it on
an ancient processor/OS/C compiler combination that does not
automatically convert C floats to double when doing any sort
of calculation.
It wouldn't have to be that ancient. The current version of
gcc supports 32-bit doubles on quite a few platforms -- though
it doesn't seem to for IA32 :/

Simply storing intermediate and final results as
single-precision floats would probably be sufficient.
Standard CPython does not have C single-precision floats.
I know.
The only point I can think of for doing this with single numbers, as
opposed to arrays of millions, is to show that there is no point.
I use Python to test algorithms before implementing them in C.
It's far, far easier to do experimentation/prototyping in
Python than in C. I also like to have two sort-of independent
implementations to test against each other (it's a good way to
catch typos).

In the C implementations, the algorithms will be done
implemented in single precision, so doing my Python prototyping
in as close to single precision as possible would be "a good
thing".
Or do you have something else in mind?
--
Grant Edwards grante Yow! Yow! Is my fallout
at shelter termite proof?
visi.com
May 10 '07 #4
On 2007-05-09, Robert Kern <ro*********@gmail.comwrote:
Grant Edwards wrote:
>I'm pretty sure the answer is "no", but before I give up on the
idea, I thought I'd ask...

Is there any way to do single-precision floating point
calculations in Python?

I know the various array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...

We also have scalar types of varying precisions in numpy:

In [9]: from numpy import *

In [10]: float32(1.0) + float32(1e-8) == float32(1.0)
Out[10]: True
Very interesting. Converting a few key variables and
intermediate values to float32 and then back to CPython floats
each time through the loop would probably be more than
sufficient.

So far as I know, I haven't run into any cases where the
differences between 64-bit prototype calculations in Python and
32-bit production calculations in C have been significant. I
certainly try to design the algorithms so that it won't make
any difference, but it's a nagging worry...
In [11]: 1.0 + 1e-8 == 1.0
Out[11]: False

If you can afford to be slow,
Yes, I can afford to be slow.

I'm not sure I can afford the decrease in readability.
I believe there is an ASPN Python Cookbook recipe for
simulating floating point arithmetic of any precision.
Thanks, I'll go take a look.

--
Grant Edwards grante Yow! It's the RINSE
at CYCLE!! They've ALL IGNORED
visi.com the RINSE CYCLE!!
May 10 '07 #5
Grant Edwards <gr****@visi.comwrote:
>In the C implementations, the algorithms will be done
implemented in single precision, so doing my Python prototyping
in as close to single precision as possible would be "a good
thing".
Something like numpy might give you reproducable IEEE 32-bit floating
point arithmetic, but you may find it difficult to get that out of a
IA-32 C compiler. IA-32 compilers either set the x87 FPU's precision to
either 64-bits or 80-bits and only round results down to 32-bits when
storing values in memory. If you can target CPUs that support SSE,
then compiler can use SSE math to do most single precision operations
in single precision, although the compiler may not set the required SSE
flags for full IEEE complaince.

In other words, since you're probably going to have to allow for some
small differences in results anyways, it may not be worth the trouble
of trying to get Python to use 32-bit floats.

(You might also want to consider whether you want to using single
precision in your C code to begin with, on IA-32 CPUs it seldom makes
a difference in performance.)

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
May 10 '07 #6
On May 9, 6:51 pm, Grant Edwards <gra...@visi.comwrote:
Is there any way to do single-precision floating point
calculations in Python?
Yes, use numpy.float32 objects.
I know the various array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...

Numpy has scalars as well.
>>import numpy
a = numpy.float32(2.0)
b = numpy.float32(8.0)
c = a+b
print c
10.0
>>type(c)
<type 'numpy.float32'>
>>>


May 10 '07 #7
On 2007-05-10, Ross Ridge <rr****@caffeine.csclub.uwaterloo.cawrote:
Grant Edwards <gr****@visi.comwrote:
>>In the C implementations, the algorithms will be done
implemented in single precision, so doing my Python prototyping
in as close to single precision as possible would be "a good
thing".

Something like numpy might give you reproducable IEEE 32-bit
floating point arithmetic, but you may find it difficult to
get that out of a IA-32 C compiler.
That's OK, I don't run the C code on an IA32. The C target is
a Hitachi H8/300.
(You might also want to consider whether you want to using
single precision in your C code to begin with, on IA-32 CPUs
it seldom makes a difference in performance.)
Since I'm running the C code on a processor without HW floating
point support, using single precision makes a big difference.

--
Grant Edwards grante Yow! I have many CHARTS
at and DIAGRAMS..
visi.com
May 10 '07 #8
Off-topic, but maybe as practical as "[making] your own Python build
from altered source." ---

Fortran 95 (and earlier versions) has single and double precision
floats. One could write a Fortran code with variables declared REAL,
and compilers will by default treat the REALs as single precision, but
most compilers have an option to promote single precision variables to
double. In Fortran 90+ one can specify the KIND of a REAL, so if
variables as

REAL (kind=rp) :: x,y,z

throughout the code with rp being a global parameter, and one can
switch from single to double by changing rp from 4 to 8. G95 is a
good, free compiler. F95 has most but not all of the array operations
of NumPy.

May 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: km | last post by:
Hi all, does python currently support 80 bit precision Floating Point Unit ? regards, KM
24
by: Philipp | last post by:
Hello (not sure this is the right forum for that question so please redirect me if necessary) How can I know how many double values are available between 0 and 1? On my machine (pentium 3) I get...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
21
by: syntax | last post by:
hi, i need to get high presion float numbers. say, i need pi = 22/7.0 = 3.142857....(upto 80 digits) is it possible ? does gcc/g++ compiler can give such type of high precision?? plz...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
1
by: TC | last post by:
I am getting small math errors when I use a query to convert a column's data type from Text to Single. I am using a calculated column with the following expression. HighSchoolGPA:...
11
by: rshepard | last post by:
I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: . When I concatenate lists, I end up...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.