[Numpy-discussion] Floating point question
Mon Mar 2 14:48:09 CST 2009
On Mon, Mar 2, 2009 at 14:37, Gideon Simpson <firstname.lastname@example.org> wrote:
> I recently discovered that for 8 byte floating point numbers, my
> fortran compilers (gfortran 4.2 and ifort 11.0) on an OS X core 2 duo
> machine believe the smallest number 2.220507...E-308. I presume that
> my C compilers have similar results.
> I then discovered that the smallest floating point number in python
> 2.5 is 4.9065...E-324. I have been using numpy to generate data,
> saving it with savetxt, and then reading it in as ASCII into my
> fortran code. Recently, it crapped out on something because it didn't
> like reading it a number that small, though it is apparently perfectly
> acceptable to python.
> My two questions are:
> 1. What is the best way to handle this? Is it just to add a filter
> of the form
> u = u * ( np.abs(u) > 2.3 e-308)
You can get the precise value from finfo:
In : from numpy import finfo
In : f = finfo(float64)
In : f.tiny
I'd probably do something like this:
u[abs(u) < f.tiny] = 0.0
> 2. What gives? What's the origin of this (perceived) inconsistency
> in floating points across languages within the same platform?
4.9065...E-324 is a denormalized float.
"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
More information about the Numpy-discussion