Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Simple calculation error

Reply
Thread Tools

Re: Simple calculation error

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-04-2008
Francois Liot wrote:
>
> I observed a strange calculation answer, on both python 2.3.4 and 2.4.4
>
>> >> print 753343.44 - 753361.89

>
> -18.4500000001
>
>> >> print ( (753361.89*100) - (753343.44*100) ) / 100

>
> 18.45
>
> Can somebody help me to play correctly with decimal values?


A 64-bit binary floating point number can hold values between -1e308 and
+1e308, in only 64 bits of memory. Since 1e308 is a *lot* larger than
float(2**64) = ~1.8e19, it does this by splitting the number in a binary
fraction, and a multiplier (stored as an exponent).

Unfortunately, very few decimal fractions can be *exactly* represented
as binary fractions, so you often get representation errors:

http://docs.python.org/tut/node16.html

This is usually not much of a problem, since you usually end up rounding
things to a suitable number of decimals or significant digits when you
print them anyway (see below), but repr() doesn't do that -- it always
outputs enough digits to get back the *exact* binary representation if
you convert the string back to a floating point value again.

In practice, you can usually ignore this; just use the appropriate
output methods, and things will just work:

While pathological cases do exist, for most casual use of
floating-point arithmetic you'll see the result you expect
in the end if you simply round the display of your final
results to the number of decimal digits you expect. str()
usually suffices, and for finer control see the discussion
of Python's % format operator: the %g, %f and %e format codes
supply flexible and easy ways to round float results for
display.

(from the above link)

If you really need full control over this, no matter what, use the
Decimal type, as provided by the decimal module in the standard library.
See the library reference for the full story.

</F>

 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      01-04-2008
On Jan 4, 12:30*pm, Fredrik Lundh <(E-Mail Removed)> wrote:
> Francois Liot wrote:
>
> > I observed a strange calculation answer, on both python 2.3.4 and 2.4.4

>
> >> >> print 753343.44 - 753361.89

>
> > -18.4500000001

>
> >> >> print ( (753361.89*100) - (753343.44*100) ) / 100

>
> > 18.45

>
> > Can somebody help me to play correctly with decimal values?

>


If the OP is questioning the change in sign, the operands are reversed
in the second statement.

-- Paul
 
Reply With Quote
 
 
 
 
Francois Liot
Guest
Posts: n/a
 
      01-04-2008
No the change of sign is due to a fake copy and past,
My question was related to decimal calculation.

Thanks,

Francois Liot

-----Original Message-----
From: python-list-bounces+fliot=(E-Mail Removed) [mailtoython-list-bounces+fliot=(E-Mail Removed)] On Behalf Of Paul McGuire
Sent: Friday, January 04, 2008 1:46 PM
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Re: Simple calculation error

On Jan 4, 12:30*pm, Fredrik Lundh <(E-Mail Removed)> wrote:
> Francois Liot wrote:
>
> > I observed a strange calculation answer, on both python 2.3.4 and 2.4.4

>
> >> >> print 753343.44 - 753361.89

>
> > -18.4500000001

>
> >> >> print ( (753361.89*100) - (753343.44*100) ) / 100

>
> > 18.45

>
> > Can somebody help me to play correctly with decimal values?

>


If the OP is questioning the change in sign, the operands are reversed
in the second statement.

-- Paul
--
http://mail.python.org/mailman/listinfo/python-list
 
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
A Simple Integrated Circuit Design with Factorial Calculation serkany VHDL 6 04-17-2009 07:04 PM
Structural Calculation Software ( simple & easy ) EMPIRE2D MCAD 0 07-07-2007 04:22 AM
Problem with returning prime number in a simple calculation program QHorizon@gmail.com Python 2 03-04-2007 12:32 AM
Layout depending on simple results of a calculation Badass Scotsman ASP .Net 1 04-24-2006 05:56 PM
Simple Calculation in Form - 3 textboxes - 1 function rdavis7408@gmail.com Javascript 9 01-25-2006 07:34 PM



Advertisments