ROUNDING???

K

katie smith

in python im doing the problem 255/494

it keeps giving me 0 instead of .51....
what am i doing wrong?

please help me I have been looking for hours


____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
 
7

7stud

in python im doing the problem 255/494

it keeps giving me 0 instead of .51....
what am i doing wrong?

please help me I have been looking for hours

      ___________________________________________________________________________ _________
Never miss a thing.  Make Yahoo your home page.http://www.yahoo.com/r/hs

An integer divided by an integer produces an integer. In computer
programming, that's called 'integer arithmetic', and any fractional
part of the result is chopped off(not rounded). If your arithmetic
involves at least one float, then you will get a float as an asnwer:


print 255/494
print 255.0/494
print (255 * 1.0)/494

--output:--
0
0.516194331984
0.516194331984
 
J

Jeff Schwab

7stud said:
An integer divided by an integer produces an integer. In computer
programming, that's called 'integer arithmetic', and any fractional
part of the result is chopped off(not rounded). If your arithmetic
involves at least one float, then you will get a float as an asnwer:


print 255/494
print 255.0/494
print (255 * 1.0)/494

--output:--
0
0.516194331984
0.516194331984

Not that this behavior is expected to change in the future, such that
255 / 494 will actually perform floating-point division. The way to
achieve flooring division will be 255 // 494.
 
A

Aahz

in python im doing the problem 255/494

it keeps giving me 0 instead of .51....
what am i doing wrong?
0.40000000000000002

In addition:
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

Therefore this works in Python 2.2 and higher.
 
A

Asun Friere

An integer divided by an integer produces an integer. In computer
programming, that's called 'integer arithmetic', and any fractional
part of the result is chopped off(not rounded).

In case you care, the "chopped off" bit is given by the modulo
operator '%'. So integer division x/y is really like the everyday y
goes into x, p times, remainder q, for example:
(3, 1)
If your arithmetic
involves at least one float, then you will get a float as an asnwer:

print 255/494
print 255.0/494
print (255 * 1.0)/494

or indeed "print float(255)/494"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top