Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

python halp

Name: Anonymous 2008-02-28 1:39

Quick question /prog/,
I'm just starting out a course at school involving python, and this is the first programming language I have had experience in. I have an exercise asking me to predict the output of expression

int (-8.0 / 3)

and i thought -3 since -8.0/3 = -2.666.. and the integer floors it to -3, but the output is -2. I tried -8/3 integer division and it gives -3.

tl;dr why does
int(-8.0/3) = -2
but
-8/3 = -3

Name: Anonymous 2008-02-28 1:43

ONE WORD

Name: Anonymous 2008-02-28 1:47

LACK OF FORCED CASTING OF TYPES

Name: Anonymous 2008-02-28 2:24

THREAD OVER

Name: Anonymous 2008-02-28 3:44

-8/3 is a fixed-point division which subtracts 3 from |-8| until the |remainder| becomes <3.
int(), however, follows arithmetic conventions and rounds the result to the nearest odd integer.

Name: Anonymous 2008-02-28 3:47

THE FORCED OVERNESS OF THREADS

Name: Anonymous 2008-02-28 6:54

int() rounds towards zero

Name: Anonymous 2008-02-28 6:55

>>1
Integer conversion discards the fractional part.
If you want it rounded, int(x/y+.5)

Name: Anonymous 2008-02-28 7:44

DON'T HELP HIM!

Name: Anonymous 2008-02-29 8:27

>>5

BBCODE EXPERT

Name: Anonymous 2011-01-31 21:24

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List