0

I'm trying to write this equation with python:

(1/100)*((102/65520)*x - 1)*10

wolfram alpha link

But I don't know how to apply the fractions in order to get the right result.

Does anyone know how I should write it?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • If you are using Python 2 you want `from __future__ import division` - otherwise `/` performs an integer division. Another option would be using e.g. `100.0` instead of `100` to force floating point division. – ThiefMaster Apr 11 '16 at 08:21

1 Answers1

0

You are using 'integer' division, which is default in python 2, so results are being rounded (1/100 is equal to 0, nooooo!).

You have too make the division 'floaty'. I can think of two solutions:

1) Make the dividends become floats directly

(1.0/100)*((102.0/65520)*x - 1)*10

You can cast integers into floats with float(), too. Then, float(102) is equivalent to 102.0

(float(1)/100)*(float(102)/65520)*x - 1)*10

2) Use float division (default in Python 3):

from future import __division__

Now / is float division and your code line works as it is. Note: You should use '//' for integer division, just in case.

Credits on the second one for: How can I force division to be floating point? Division keeps rounding down to 0

Community
  • 1
  • 1
hectorcanto
  • 1,987
  • 15
  • 18