[Tutor] numerical problem
Gabriele Brambilla
gb.gabrielebrambilla at gmail.com
Tue Mar 4 04:51:32 CET 2014
for example I read this:
On Pythons prior to 2.7 and 3.1, once you start experimenting with
floating-point numbers, you're likely to stumble across something that may
look a bit odd at first glance:
>>> 3.1415 * 2 # repr: as code (Pythons < 2.7 and 3.1)
6.2830000000000004
>>> print(3.1415 * 2) # str: user-friendly
6.283
The first result isn't a bug; it's a display issue. It turns out that there
are two ways to print every object in Python--with full precision (as in the
first result shown here), and in a user-friendly form (as in the second).
Formally, the first form is known as an object's as-code repr, and the
second is its user-friendly str. In older Pythons, the floating-point repr
sometimes displays more precision than you might expect. The difference can
also matter when we step up to using classes. For now, if something looks
odd, try showing it with a print built-in function call statement. Better
yet, upgrade to Python 2.7 and the latest 3.X, where floating-point numbers
display themselves more intelligently, usually with fewer extraneous
digits--since this book is based on Pythons 2.7 and 3.3, this is the display
form I'll be showing throughout this book for floating-point numbers:
>>> 3.1415 * 2 # repr: as code (Pythons >= 2.7 and 3.1)
6.283
Besides expressions, there are a handful of useful numeric modules that
ship with Python--modules are just packages of additional tools that we
import to use:
>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(85)
9.219544457292887
The math module contains more advanced numeric tools as functions, while
the ran dom module performs random-number generation and random selections
(here, from a Python list coded in square brackets--an ordered collection of
other objects to be introduced later in this chapter):
>>> import random
>>> random.random()
0.7082048489415967
>>> random.choice([1, 2, 3, 4])
1
Could the problem be something like this?
thanks
Gabriele
2014-03-03 22:44 GMT-05:00 Gabriele Brambilla <
gb.gabrielebrambilla at gmail.com>:
> ok,
> the things I get is very specific.
> my results differs from the other guy's ones with which I compare them for
> slightly details.
> We fit the data with a function and we obtain different but similar
> values. our fit routines on the same data return the same results. So we
> produce different data.
> the values in www are like 1.01e-134 and also smaller, and I sum values
> like this: there could be any problem summing so small numbers? is there a
> Python guide that helps in using strange values numbers?
>
> thanks
>
> Gabriele
>
>
>
>
> 2014-03-03 21:02 GMT-05:00 Steven D'Aprano <steve at pearwood.info>:
>
> On Mon, Mar 03, 2014 at 08:00:52PM -0500, Gabriele Brambilla wrote:
>> > Hi,
>> >
>> > I'm doing a sum in a for loop:
>> >
>> > www is the quantity I add.
>> >
>> > MYMAP[i, j, k] = MYMAP[i, j, k] + www
>> >
>> > MYMAP is a numpy array
>> >
>> > I have strong reasons to think that in this operation happens some
>> > numerical error...Have you suggestions to discover where it is?
>>
>> You are almost certainly correct. Unless all the numbers are exact
>> integers, nearly every floating point operation will add some error.
>>
>> Without seeing your code, and some representative sample data, it is
>> impossible to tell whether the error is tiny or huge.
>>
>> Tiny error:
>>
>> MYMAP[1, j, k] = 1.001
>> www = 0.25
>>
>>
>> Huge error:
>>
>> MYMAP[1, j, k] = 1.001e19
>> www = 0.25
>>
>>
>>
>> --
>> Steven
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140303/5696dadf/attachment.html>
More information about the Tutor
mailing list