# Thread: python return functions incorrect

1. Gee! These Aren't Roasted!
Join Date
Jan 2012
Beans
161

## python return functions incorrect

Hi people, this is some python code I have been working on...

return a + b

def divide(a, b):
return a / b

def subtract(a, b):
return a - b

Code:
```print " I will attempt to get 995.6 from computational math.  I hope it works."

print "Give me the first number."

var1 = float(raw_input(">"))

print "Give me number 2."

var2 = float(raw_input(">"))

print "Ok, number 3."

var3 = float(raw_input(">"))

print "Ok, the fourth number."

var4 = float(raw_input(">"))

formula1 = divide(var2, var3)

formula2 = subtract(formula1, var4)

these are the values I provide:

var1=24
var2=34
var3=100
var4=1023

i get -998.66 from the program, but the math says the answer should be -995.6

Why is that?

2. Gee! These Aren't Roasted!
Join Date
Jan 2012
Beans
161

## Re: python return functions incorrect

whoops! Those function definitions are part of the code too

3. ## Re: python return functions incorrect

Originally Posted by wingnut2626
i get -998.66 from the program, but the math says the answer should be -995.6
Your math is wrong, Python is right.

4. ## Re: python return functions incorrect

Code:
```metulburr@ubuntu:~\$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
>>> 34 / 100
0.34
>>> 0.34 - 1023
-1022.66
>>> 24 + -1022.66
-998.66
>>>```
Code:
```metulburr@ubuntu:~\$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
>>> 34 / 100
0
>>> 0 - 1023
-1023
>>> 24 + -1023
-999
>>>```

5. ## Re: python return functions incorrect

What are you trying to say here?

Originally Posted by micahpage
Code:
```metulburr@ubuntu:~\$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
>>> 34 / 100
0.34
>>> 0.34 - 1023
-1022.66
>>> 24 + -1022.66
-998.66
>>>```
Code:
```metulburr@ubuntu:~\$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
>>> 34 / 100
0
>>> 0 - 1023
-1023
>>> 24 + -1023
-999
>>>```

6. ## Re: python return functions incorrect

was just elaborating more on:
Your math is wrong, Python is right.

7. ## Re: python return functions incorrect

Originally Posted by micahpage
was just elaborating more on:
And why the two Python outputs (one of which is incorrect)?

8. ## Re: python return functions incorrect

And why the two Python outputs (one of which is incorrect)?
wasnt paying attention to the fact he float'd the input values

9. ## Re: python return functions incorrect

i see two different python versions, does python3 really drop to float during integer division?

10. ## Re: python return functions incorrect

i see two different python versions, does python3 really drop to float during integer division?
in 3.x the / operator does the floating-point division, and the // operator to do truncating division
Last edited by micahpage; February 5th, 2013 at 07:13 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•