1

So I am trying to solve this EQUATION in Python 2.7.3:

import math

y = 68
x = -5/4*(-463 + math.sqrt(1216881-16000 * y))
print x

x should print 130, but instead is printing 208.

I cannot see where I have gone wrong.

Jonathan Davies
  • 852
  • 2
  • 12
  • 26

2 Answers2

3
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> -5/4
-2
>>> from __future__ import division
>>> -5/4
-1.25

See PEP 238 -- Changing the Division Operator. Python 2 returns the floor on integer division, Python 3 returns a float where needed.

Steven Rumbalski
  • 42,094
  • 8
  • 83
  • 115
3

the problem is that the result of a division of an integer by another integer returns a integer, change one of your constants to float and you'll get the correct result

try this:

import math

y = 68.0
x = -5/4.0*(-463 + math.sqrt(1216881-16000 * y))
print x

for more information please read:

https://www.python.org/dev/peps/pep-0238/