Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Working with decimal points

Reply
Thread Tools

Working with decimal points

 
 
Byte
Guest
Posts: n/a
 
      04-08-2006
How come:

sum = 1/4
print sum

returns 0? 1/4=0.25, not 0. How do I fix this?

-- /usr/bin/byte

 
Reply With Quote
 
 
 
 
mtallen@gmail.com
Guest
Posts: n/a
 
      04-08-2006
Byte wrote:
> How come:
>
> sum = 1/4
> print sum
>
> returns 0? 1/4=0.25, not 0. How do I fix this?


Make sure there is at least one float in your equation. In your example
Python is doing interger math for you and returing the floor. You need
to give it a hint that you would like to do floating point math.

>>> sum = 1.0/4
>>> print sum

0.25
>>> sum = 1/4.0
>>> print sum

0.25
>>>


 
Reply With Quote
 
 
 
 
Byte
Guest
Posts: n/a
 
      04-08-2006
That dosnt work either:

sum = 0.1+1/4
print sum

Just returns 0.1

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-08-2006
"Byte" wrote:

> How come:
>
> sum = 1/4
> print sum
>
> returns 0?


because 1 and 4 are integer objects, so 1/4 is an integer division, which
rounds down to the nearest integer.

> 1/4=0.25, not 0. How do I fix this?


use floating point numbers:

1.0/4.0 = 0.25

or convert one of the numbers to a float:

float(1)/4 = 0.25

</F>



 
Reply With Quote
 
Byte
Guest
Posts: n/a
 
      04-08-2006
Fredrik Lundh's way works: thank a million!

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-08-2006
Byte wrote:
> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1


That's because the 1/4 is executed first, and the problem mentioned
still applies (i.e. you get a 0, then add it to 0.1).

The best fix for you might be simply to place this line at the start
(before all other code) of your module:

from __future__ import division

That will change the way simple division works to give you the results
you expected. See the online docs for more background on this.

-Peter

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-08-2006
On Sat, 08 Apr 2006 08:21:06 -0700, Byte wrote:

> How come:
>
> sum = 1/4
> print sum
>
> returns 0? 1/4=0.25, not 0. How do I fix this?


By default, / does integer division, not floating point. In integer
division, 1/4 is 0, exactly as calculated.

(How many fours go into one? Zero fours go into one, with one remainder.)

There are two ways to change this:

(1) Convert at least one of the numbers to a float first:

>>> 1.0/4

0.25
>>> 1/float(4)

0.25

but be careful, this one doesn't do what you want:

>>> float(1/4)

0.0


(2) Run the following command at the start of your program or in your
interactive session:

>>> from __future__ import division


Now division will behave as you expect:

>>> 1/4

0.25

and you can use // for integer division.

Now that you can do floating point division, I peer into my crystal
ball and predict your next question: why is Python so inaccurate?

>>> 1/10

0.10000000000000001

Answer: it isn't. You've discovered a mathematical limitation that 1/10
cannot be written exactly in any fixed number of binary places, just like
1/3 cannot be written exactly in any fixed number of decimal places.

See also:

http://www.python.org/doc/faq/genera...-so-inaccurate

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


Hope this helps.


--
Steven.

 
Reply With Quote
 
mtallen@gmail.com
Guest
Posts: n/a
 
      04-08-2006

Byte wrote:
> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1


You get precedence right? Your equation does not evaluate from left to
right. 1/4 happens first, and since there are no floats you get 0.

in that equation you basically are doing this:

sum = 1/4
print sum
0

sum = 0.1 + sum
print sum
0.1

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-08-2006
"Byte" wrote:

> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1


division has higher precedence than addition, so 1/4 is calculated first,
and the result is then added to 0.1.

and as I've already explained, 1/4 is an integer division, so the result
is rounded down to the the nearest integer (0). in other words, your
expression boils down to

0.1 + 0

which is 0.1 [1].

the precedence order is explained here:

http://docs.python.org/ref/summary.html

</F>

1) or at least a close approximation of it; see
http://docs.python.org/tut/node16.html



 
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
Precision upto n decimal points Pp C Programming 10 12-05-2011 01:55 PM
ASP.NET FormView Money Data Type Binding Problem with 2 Decimal Points.. Sam ASP .Net 1 02-11-2010 02:22 PM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
Trouble in entering numbers after decimal points - JFormattedTextField kishanthany@gmail.com Java 1 06-26-2006 09:20 AM
Displaying data to 2 decimal points Guy Hocking ASP General 8 03-07-2004 09:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57