marco
marco

Reputation: 893

Using trunc function in python

I have the mathematical formula which looks like this:

enter image description here

I want to convert it into 2.7 python syntax. I assumed upper "trunc" has to be the same thing as math.trunc:

a = (x - 360)*math.trunc(x/360)

Is this the correct python 2.7 syntax?

Thank you.

Upvotes: 1

Views: 3110

Answers (2)

Anton Savin
Anton Savin

Reputation: 41301

You translated the formula incorrectly. You don't need parentheses around x - 360, also you can use Python integer division instead of math.trunc:

a = x - 360 * (x // 360)

Note that it works even for negative x unlike math.trunc.

Upvotes: 3

Kyle_S-C
Kyle_S-C

Reputation: 1177

I ran:

from __future__ import division
import math

for i in xrange(0, 1080, 1):
   print i, math.trunc(i/360)

Seemed to give the right answer.

You will want to use from __future__ import division to force Python 3 style division (see here for more details: https://www.python.org/dev/peps/pep-0238).

Upvotes: 1

Related Questions