473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Working with decimal points

How come:

sum = 1/4
print sum

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

-- /usr/bin/byte

Apr 8 '06 #1
8 1943
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


Apr 8 '06 #2
That dosnt work either:

sum = 0.1+1/4
print sum

Just returns 0.1

Apr 8 '06 #3
"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>

Apr 8 '06 #4
Fredrik Lundh's way works: thank a million!

Apr 8 '06 #5
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

Apr 8 '06 #6
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.

Apr 8 '06 #7

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

Apr 8 '06 #8
"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

Apr 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Enno Middelberg | last post by:
Hi, I'm sure someone asked this question before, but I can't find the solution on the web or in the groups. I want to print out columns of numbers which are aligned at the decimal point,...
15
by: Kay Schluehr | last post by:
I wonder why this expression works: >>> decimal.Decimal("5.5")**1024 Decimal("1.353299876254915295189966576E+758") but this one causes an error 5.5**1024 Traceback (most recent call...
2
by: intrepid_dw | last post by:
All I am writing a C# WinForms application which is giving me some problems. The application consists of a form containing an empty Tab control to which TabPages are added dynamically (at...
0
by: Sam | last post by:
I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data type of 'Money...
1
by: ianhen | last post by:
Hi I have a Web Form which I need to get results shown to 2 decimal points only. In the Head I have: - function heatoutput() { var vWidth=document.heatcalculator.roomwidth.value; var...
0
by: apple27 | last post by:
Hi, Appreciate your guys help.. Im a newbie in OWC.Trying to search for OWC objects to complete a few charts. Still having difficulty on this :- My questions: 1. I need to come out with a...
3
neo008
by: neo008 | last post by:
Hi all, I am using some variables as "Double" and some text boxes set "DataFormat" property to "Number with two decimal points" for currency calculations. I perform some typical mathematical...
3
by: Mike Howarth | last post by:
Hi Seem to be having a bit of brainfreeze this evening. Basically I'm reducing an array of prices like so: This gives me a string of '86.00.00' which I am trying to use with decimal...
6
Mohan Krishna
by: Mohan Krishna | last post by:
Hi everyone! I am entering only numbers with decimal points/values in a TEXTBOX on the VB form. I need to get only the decimal values. How can I? Please help me as I tried in this way (for 2...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.