Calculation of trigonometric functions in python - python

How does Python calculate trigonometric functions?
I try to calculate using
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
I'm getting
x = 0.1
why is that? in a usual calculator (radian) i'm getting 0.001

In Python 2, / is integer division,you need to import __future__ .division for floating division :
>>> from __future__ import division
>>> import math
>>> x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
>>> x
0.001

In python2.x, python takes the floor of integer division. Thus, you need to import division from the __future__ library at the top of your program.
from __future__ import division
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
print x

Just make your integers such as 2 float 2.0, else Python 2.x uses integer division, also known as floor division (rounding towards minus infinity e.g. -9/8 gives -2, 9/8 gives 1), when dividing integers by other integers (whether plain or long):
x = ((0.1-0.001)/2.0)*math.sin(((1/20.0)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2.0)+0.001
and:
print x
0.001

Related

How to prevent automatic rounding in print function when printing a float number?

Let's consider this situation:
from math import sqrt
x = sqrt(19) # x : 4.358898943540674
print("{:.4f}".format(x))
# I don't want to get 4.3589
# I want to get 4.3588
The print() function rounds the number automatically, but I don't want this. What should I do?
If you want to round the number down to the 4th decimal place rather than round it to the nearest possibility, you could do the rounding yourself.
x = int(x * 10**4) / 10**4
print("{:.4f}".format(x))
This gives you
4.3588
Multiplying and later dividing by 10**4 shifts the number 4 decimal places, and the int function rounds down to an integer. Combining them all accomplishes what you want. There are some edge cases that will give an unexpected result due to floating point issues, but those will be rare.
Here is one way. truncate function courtesy of #user648852.
from math import sqrt, floor
def truncate(f, n):
return floor(f * 10 ** n) / 10 ** n
x = sqrt(19) # x : 4.358898943540674
print("{0}".format(truncate(x, 4)))
# 4.3588
Do more work initially and cut away a fixed number of excess digits:
from math import sqrt
x = sqrt(19) # x : 4.358898943540674
print(("{:.9f}".format(x))[:-5])
gives the desired result. This could still fail if x has the form ?.????999996 or similar, but the density of these numbers is rather small.

Extracting of the arithmetic root of arbitrary degree

How to extract an arithmetic root of arbitrary degree in Python?
I know that algorithm:
z = pow(x,(1/N))
Is this correct?
Is this the only way?
This is indeed the way to go. However, you need to use 1.0/N to ensure float division (unless N is always a float):
>>> import math
>>> math.pow(27, 1/3)
1.0
>>> math.pow(27, 1.0/3)
3.0
>>> math.pow(27, 1/3.0)
3.0
You could also use from __future__ import division to make / the "regular" division operator (for integer division you would then use //):
>>> from __future__ import division
>>> import math
>>> math.pow(27, 1/3)
3.0
Instead of math.pow(x, y) you could also use the x ** y operator:
>>> 27**(1.0/3)
3.0
You can use the ** operator
>>>x = 32
>>>n = 5
>>>x**(1.0/n)
2.0

Difference between Python 2.6.4 and Python 3.1.2 integer handling

Why does
x = 15
if (x/2) * 2 == x:
print 'Even'
else: print 'Odd'
Evaluate to Odd in Python 2.6.4?
While in 3.1.2 (with parentheses around print statements of course) is evaluates to Even?
In Python 2.x, the / operator uses integer division by default. Since Python 3.x (or if you start your 2.x program with from __future__ import division), the / operator performs floating-point division. This is documented in PEP238.
You should use // if you want integer division, or start your programs with from __future__ import division if you want floating point division under 2.x.
Note that the generic way to check whether a number is even or odd is modulo division with the % operator; like this:
x = 15
print ('even' if x % 2 == 0 else 'odd')
For details on these and other operators, refer to the Python manual.
The / operator performs integer division in Python 2.x, while it does floating-point division in Python 3.x. You can use // to get integer division.
Checking the parity of an integer is easier using the modulo operator:
if x % 2:
print("odd")
else:
print("even")

Python and Floats

I have this code :
N = 10
for i in range(1,N):
P[i,i] = (i/N) + pow((1-i/N),2)
But my division operations are getting rounded down to the nearest integer.
How can I instruct Python to do floating-point division?
You are doing integer division. Try something like this:
N = 10
for i in range(1,N):
P[i,i] = (float(i)/N) + pow((1-float(i)/N),2)
Add this line to the top of your script:
from __future__ import division
This will allow division of integers to give floats with the usual division operator /. If you also need to perform integer division, you can use //:
>>> 9/10
0.90000000000000002
>>> 9//10
0
This will be the standard behavior in Python 3.
In Python 2.x, division between two integers is the mathematical division, but floored. Since you're dividing two integers, one being equal or smaller than the other, you get 1s and 0s.
To have the correct behavior, use floats:
N = 10.0
for i in range(1, int(N) ):
P[i,i] = (i/N) + pow((1-i/N),2)
Note that Python 3.x does mathematical division with two integers

Convert fraction to decimal in Python

I want to convert 1/2 in python so that when i say print x (where x = 1/2) it returns 0.5
I am looking for the most basic way of doing this, without using any split functions, loops or maps
I have tried float(1/2) but I get 0...
can someone explain me why and how to fix it?
Is it possible to do this without modifying the variable x= 1/2 ?
In python 3.x any division returns a float;
>>> 1/2
0.5
To achieve that in python 2.x, you have to force float conversion:
>>> 1.0/2
0.5
Or to import the division from the "future"
>>> from __future__ import division
>>> 1/2
0.5
An extra: there is no built-in fraction type, but there is one in Python's standard library:
>>> from fractions import Fraction
>>> a = Fraction(1, 2) #or Fraction('1/2')
>>> a
Fraction(1, 2)
>>> print a
1/2
>>> float(a)
0.5
and so on...
If the input is a string,then you could use Fraction directly on the input:
from fractions import Fraction
x='1/2'
x=Fraction(x) #change the type of x from string to Fraction
x=float(x) #change the type of x from Fraction to float
print x
You're probably using Python 2. You can "fix" division by using:
from __future__ import division
at the start of your script (before any other imports). By default in Python 2, the / operator performs integer division when using integer operands, which discards fractional parts of the result.
This has been changed in Python 3 so that / is always floating point division. The new // operator performs integer division.
Alternatively, you can force floating point division by specifying a decimal or by multiplying by 1.0. For instance (from inside the python interpreter):
>>> print 1/2
0
>>> print 1./2
0.5
>>> x = 1/2
>>> print x
0
>>> x = 1./2
>>> print x
0.5
>>> x = 1.0 * 1/2
>>> print x
0.5
EDIT: Looks like I was beaten to the punch in the time it took to type up my response :)
There is no quantity 1/2 anywhere. Python does not represent rational numbers with a built-in type - just integers and floating-point numbers. 1 is divided by 2 - following the integer division rules - resulting in 0. float(0) is 0.

Categories