Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > floating point arithmetic

Reply
Thread Tools

floating point arithmetic

 
 
fred8865
Guest
Posts: n/a
 
      08-26-2008
Hi all,

I understand that due to different arithmetic used in floating points
they are just approximations. Hence, 180/100=1 in my python interpreter.
How can I tackle this problem of inaccurate floating point numbers?
thank you

regards
xtd
 
Reply With Quote
 
 
 
 
Mensanator
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 4:11 pm, fred8865 <(E-Mail Removed)> wrote:
> Hi all,
>
> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.
> How can I tackle this problem of inaccurate floating point numbers?


Try actually using floating point numbers, not integers:

>>> 180.0/100

1.8

or

>>> float(180)/100

1.8


> thank you
>
> regards
> xtd


 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      08-26-2008
On Aug 27, 7:11 am, fred8865 <(E-Mail Removed)> wrote:

> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.


It's not "hence". What you are seeing is truncating integer division.

> How can I tackle this problem of inaccurate floating point numbers?


>>> 180 / 100

1
>>> 180 / 100.

1.8
>>> 180 / float(100)

1.8
>>> from __future__ import division
>>> 180 / 100

1.8
 
Reply With Quote
 
Rob Clewley
Guest
Posts: n/a
 
      08-26-2008
> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.


No, that's not the reason you get 1, it's because the current version
of python does integer division by default. Try doing 180.0/100 or
including

from __future__ import division

at the top of your scripts before dividing your numbers.

> How can I tackle this problem of inaccurate floating point numbers?


There are few occasions for "regular" users to worry about the
inaccuracy of floating point, unless you are doing very technical
calculations at high precision. If you need decimals to be represented
"perfectly" in python (e.g. you are writing scripts for financial
applications), try importing the decimal package (look it up in the
python docs).

-Rob
 
Reply With Quote
 
fred8865
Guest
Posts: n/a
 
      08-26-2008
thanks guys

fred8865 wrote:

> Hi all,
>
> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.
> How can I tackle this problem of inaccurate floating point numbers?
> thank you
>
> regards
> xtd


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      08-27-2008


John Machin wrote:
> On Aug 27, 7:11 am, fred8865 <(E-Mail Removed)> wrote:
>
>> I understand that due to different arithmetic used in floating points
>> they are just approximations. Hence, 180/100=1 in my python interpreter.

>
> It's not "hence". What you are seeing is truncating integer division.
>
>> How can I tackle this problem of inaccurate floating point numbers?

>
>>>> 180 / 100

> 1
>>>> 180 / 100.

> 1.8
>>>> 180 / float(100)

> 1.8
>>>> from __future__ import division
>>>> 180 / 100

> 1.8
> --


Or start using 3.0, which fixed this

>>> 180/100

1.8

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-27-2008
En Tue, 26 Aug 2008 18:11:30 -0300, fred8865 <(E-Mail Removed)> escribi�:

> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.
> How can I tackle this problem of inaccurate floating point numbers?
> thank you


In the current Python versions (2.2 and up), 180/100 means integer
division (because both operands are integer).
If you want a floating point result, use 180.0/100 or
float(some_variable)/100

Starting with Python 3.0, the / operator will return a floating point
result ("true division"). So in that Python version, 180/100 gives 1.8
To enable that behavior on Python 2.x, execute "from __future__ import
division":

>>> 180/100

1
>>> from __future__ import division
>>> 180/100

1.8

In any Python version, 180//100 always means integer division:

>>> 180//100

1

--
Gabriel Genellina

 
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
Status of floating point arithmetic in Java? Robert Dodier Java 1 07-31-2007 05:50 PM
Fixed/Block Floating Point Arithmetic Support... Satpreet C Programming 1 02-27-2006 07:06 PM
Beginner question: Precision of floating point arithmetic... Shawn C Programming 11 07-21-2004 01:50 AM
Floating point arithmetic. Amit Bhatia C Programming 5 07-17-2004 11:32 PM
Floating point arithmetic. Amit Bhatia C++ 14 07-14-2004 07:55 PM



Advertisments