Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 3.2*2 is 9.6 ... or maybe it isn't?

Reply
Thread Tools

3.2*2 is 9.6 ... or maybe it isn't?

 
 
Bojan Sudarevic
Guest
Posts: n/a
 
      06-25-2009
Hi,

I'm PHP developer and entirely new to Python. I installed it (version
2.5.2, from Debian repos) today on the persuasion of a friend, who is a
Python addict.

The first thing I typed into it was 3.2*3 (don't ask why I typed *that*,
I don*t know, I just did). And the answer wasn't 9.6.

Here it is:

>>> 3.2*3

9.6000000000000014

So I became curious...

>>> 3.21*3

9.629999999999999
>>> (3.2*3)*2

19.200000000000003
.... and so on ...

After that I tried Windows version (3.1rc2), and...

>>> 3.2*3

9.600000000000001

I wasn't particularly good in math in school and university, but I'm
pretty sure that 3.2*3 is 9.6.

Cheers,
Bojan
 
Reply With Quote
 
 
 
 
Mark Dickinson
Guest
Posts: n/a
 
      06-25-2009
On Jun 25, 7:04*pm, Bojan Sudarevic <(E-Mail Removed)> wrote:
> Hi,
>
> I'm PHP developer and entirely new to Python. I installed it (version
> 2.5.2, from Debian repos) today on the persuasion of a friend, who is a
> Python addict.
>
> The first thing I typed into it was 3.2*3 (don't ask why I typed *that*,
> I don*t know, I just did). And the answer wasn't 9.6.
>
> [examples snipped]


Hi Bojan,

This is a FAQ. Take a look at:

http://docs.python.org/tutorial/floatingpoint.html

and let us know whether that explains things to your
satisfaction.

Mark
 
Reply With Quote
 
 
 
 
Tomasz Zieliński
Guest
Posts: n/a
 
      06-25-2009
On 25 Cze, 20:04, Bojan Sudarevic <(E-Mail Removed)> wrote:

>
> I wasn't particularly good in math in school and university, but I'm
> pretty sure that 3.2*3 is 9.6.
>


It's not math, it's floating point representation of numbers - and its
limited accuracy.
Type 9.6 and you'll get 9.5999999999999996

--
Tomasz Zielinski
http://pyconsultant.eu

 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      06-25-2009
On 6/25/2009 11:04 AM Bojan Sudarevic said...

<snip>

>>>> 3.2*3

> 9.600000000000001
>
> I wasn't particularly good in math in school and university, but I'm
> pretty sure that 3.2*3 is 9.6.


Yes -- in this world. But in the inner workings of computers, 3.2 isn't
accurately representable in binary. This is a faq.

ActivePython 2.6.2.2 (ActiveState Software Inc.) based on
Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 3.2

3.2000000000000002
>>>



Emile


 
Reply With Quote
 
Paul Rudin
Guest
Posts: n/a
 
      06-25-2009
Bojan Sudarevic <(E-Mail Removed)> writes:

> Hi,
>
> I'm PHP developer and entirely new to Python. I installed it (version
> 2.5.2, from Debian repos) today on the persuasion of a friend, who is a
> Python addict.
>
> The first thing I typed into it was 3.2*3 (don't ask why I typed *that*,
> I don*t know, I just did). And the answer wasn't 9.6.
>
> Here it is:
>
>>>> 3.2*3

> 9.6000000000000014
>
> So I became curious...
>
>>>> 3.21*3

> 9.629999999999999
>>>> (3.2*3)*2

> 19.200000000000003
> ... and so on ...
>
> After that I tried Windows version (3.1rc2), and...
>
>>>> 3.2*3

> 9.600000000000001
>
> I wasn't particularly good in math in school and university, but I'm
> pretty sure that 3.2*3 is 9.6.


This is almost certainly nothing to do with python per se, but the
floating point implementation of your hardware. Floating point
arithmetic on computers is not accurate to arbitrary precision. If you
want such precision use a library that supports it or make you own
translations to and from appropriate integer sums (but it's going to be
slower).
 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      06-25-2009
On Jun 25, 7:04*pm, Bojan Sudarevic <(E-Mail Removed)> wrote:
> The first thing I typed into it was 3.2*3 (don't ask why I typed *that*,
> I don*t know, I just did). And the answer wasn't 9.6.


It looks like it's false in PHP too, by the way (not
that I know any PHP, so I could well be missing
something...)


bernoulliy3k dickinsm$ php -a
Interactive mode enabled

<?
$a = 3.2*3;
$b = 9.6;
var_dump($a);
float(9.6)
var_dump($b);
float(9.6)
var_dump($a == $b);
bool(false)


Mark
 
Reply With Quote
 
Bojan Sudarevic
Guest
Posts: n/a
 
      06-25-2009
In article <26be756e-5a0a-40cd-b9a2-133ce14ac7d7
@n30g2000vba.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> This is a FAQ. Take a look at:
>
> http://docs.python.org/tutorial/floatingpoint.html
>
> and let us know whether that explains things to your
> satisfaction.
>


Hi Mark,

Yes, that explains things to my satisfation. Now I'm embarrassed that I
didn't know that before.

Thanks,
Bojan
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      06-25-2009
Bojan Sudarevic wrote:
> The first thing I typed into it was 3.2*3 (don't ask why I typed *that*,
> I don*t know, I just did). And the answer wasn't 9.6.
>
> Here it is:
>
>>>> 3.2*3

> 9.6000000000000014


I'm surprised how often people encounter this and wonder about it. As I
began programming back in the day using C, this is just something I grew
up with (grudging acceptance).

I guess PHP artificially rounds the results or something to make it seem
like it's doing accurate calculations, which is a bit surprising to me.
We all know that IEEE floating point is a horribly inaccurate
representation, but I guess I'd rather have my language not hide that
fact from me. Maybe PHP is using BCD or something under the hood (slow
but accurate).

If you want accurate math, check out other types like what is in the
decimal module:

>>> import decimal
>>> a=decimal.Decimal('3.2')
>>> print a * 3

9.6

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      06-25-2009
On 2009-06-25 13:41, Michael Torrie wrote:

> If you want accurate math, check out other types like what is in the
> decimal module:
>
>>>> import decimal
>>>> a=decimal.Decimal('3.2')
>>>> print a * 3

> 9.6


I wish people would stop representing decimal floating point arithmetic as "more
accurate" than binary floating point arithmetic. It isn't. Decimal floating
point arithmetic does have an extremely useful niche: where the inputs have
finite decimal representations and either the only operations are addition,
subtraction and multiplication (e.g. many accounting problems) OR there are
conventional rounding modes to follow (e.g. most of the other accounting problems).

In the former case, you can claim that decimal floating point is more accurate
*for those problems*. But as soon as you have a division operation, decimal
floating point has the same accuracy problems as binary floating point.

--
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

 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      06-25-2009
On Jun 25, 7:41*pm, Michael Torrie <(E-Mail Removed)> wrote:
> I guess PHP artificially rounds the results or something to make it seem
> like it's doing accurate calculations, which is a bit surprising to me.


After a bit of experimentation on my machine, it *looks* as though PHP
is using the usual hardware floats internally (no big surprise there),
but implicit conversions to string use 14 significant digits. If
Python's repr used '%.14g' internally instead of '%.17g' then we'd see
pretty much the same thing in Python.

> We all know that IEEE floating point is a horribly inaccurate
> representation [...]


That's a bit extreme! Care to elaborate?

, but I guess I'd rather have my language not hide that
> fact from me. *Maybe PHP is using BCD or something under the hood (slow
> but accurate).
>
> If you want accurate math, check out other types like what is in the
> decimal module:


As Robert Kern already said, there really isn't any sense in which
decimal
floating-point is any more accurate than binary floating-point, except
that---somewhat tautologically---it's better at representing decimal
values exactly.

The converse isn't true, though, from a numerical perspective: there
are some interesting examples of bad things that can happen with
decimal floating-point but not with binary. For example, given any
two Python floats a and b, and assuming IEEE 754 arithmetic with
default rounding, it's always true that a <= (a+b)/2 <= b, provided
that a+b doesn't overflow. Not so for decimal floating-point:

>>> import decimal
>>> decimal.getcontext().prec = 6 # set working precision to 6 sig figs
>>> (decimal.Decimal('7.12346') + decimal.Decimal('7.12348'))/2

Decimal('7.12345')

Similarly, sqrt(x*x) == x is always true for a positive IEEE 754
double x (again
assuming the default roundTiesToEven rounding mode, and assuming that
x*x neither overflows nor underflows). But this property fails for
IEEE 754-compliant decimal floating-point.

Mark
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Maybe it will; maybe it won't iL_weReo Computer Support 7 10-13-2009 08:42 AM
Neighbors have wireless..maybe causing problems for my wirless con =?Utf-8?B?Q3Jld2UgRGF2ZQ==?= Wireless Networking 3 01-03-2005 09:19 PM
Of Intrest ? Maybe Yes, Maybe No Old Gringo Computer Support 0 10-11-2004 01:35 AM
Maybe, just maybe @decorator syntax is ok after all Ville Vainio Python 11 08-10-2004 06:01 PM
Some concepts (Maybe LOL maybe not...) C++ 5 08-18-2003 05:48 PM



Advertisments