[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

ROUNDING???

katie smith

2/19/2008 2:58:00 AM

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.yaho...

5 Answers

7stud --

2/19/2008 3:06:00 AM

0

On Feb 18, 7:57 pm, katie smith <iceboy...@yahoo.com> wrote:
> 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.yaho...

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

Jeff Schwab

2/19/2008 3:13:00 AM

0

7stud wrote:
> On Feb 18, 7:57 pm, katie smith <iceboy...@yahoo.com> wrote:
>> 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.yaho...
>
> 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.

aahz

2/19/2008 3:37:00 AM

0

In article <mailman.949.1203389866.9267.python-list@python.org>,
katie smith <iceboy127@yahoo.com> wrote:
>
>in python im doing the problem 255/494
>
>it keeps giving me 0 instead of .51....
>what am i doing wrong?

>>> from __future__ import division
>>> 2/5
0.40000000000000002

In addition:

>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

Therefore this works in Python 2.2 and higher.
--
Aahz (aahz@pythoncraft.com) <*> http://www.python...

"All problems in computer science can be solved by another level of
indirection." --Butler Lampson

subeen

2/19/2008 4:13:00 AM

0

You can use the round() function. And if you want to print, use %0.2f


regards,
Subeen.
http://love-python.blo...


On Feb 19, 9:36 am, a...@pythoncraft.com (Aahz) wrote:
> In article <mailman.949.1203389866.9267.python-l...@python.org>,
> katie smith <iceboy...@yahoo.com> wrote:
>
>
>
> >in python im doing the problem 255/494
>
> >it keeps giving me 0 instead of .51....
> >what am i doing wrong?
> >>> from __future__ import division
> >>> 2/5
>
> 0.40000000000000002
>
> In addition:
>
> >>> division
>
> _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
>
> Therefore this works in Python 2.2 and higher.
> --
> Aahz (a...@pythoncraft.com) <*> http://www.python...
>
> "All problems in computer science can be solved by another level of
> indirection." --Butler Lampson

Asun Friere

2/19/2008 5:08:00 AM

0

On Feb 19, 2:05 pm, 7stud <bbxx789_0...@yahoo.com> wrote:

> 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:

>>> 10/3, 10%3
(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"