Python Programming Tutorials - How to Calculate Sine Cosine Tangent

in python •  7 years ago 

Hello everybody in this tutorial we're going to show you how to calculate the sine cosine and tangent using Python.

python-logo.png

The first thing we're going to do is import some modules, so let's import math let's import numpy and let's import fraction using from fractions import fraction.

import math
import numpy
from fractions import Fraction

okay so let's start with sine and notice that we're going to use the module math with sine and that returns the sine of X and radians.

Sine

So if you want to input degrees instead of radians I will show you how to do that as well.

# Sine input as radians
sine_radians = math.sin(0.523599)

Let's create our first sine variable, sine radians, let's use our math module and access sine and we're going to use what's equivalent to 30 degrees, so that in radians I believe is zero point five two three five nine nine okay.

# Display sine normal and rounded (radians)
print('sine of 0.523599 radians (30 degrees) is ', sine_radians, 'rounded ', round(sine_radians, 2))

Now let's go ahead and use a print to display this, print let's put it in a little note so sine of zero point five two three five nine nine radians, just put in a little note that that is equivalent to thirty degrees is comma, put in our variable and what you're gonna see is this will print out quite a large number so we're gonna use round to give us something that there's a little bit more readable and something you would expect to see, so let's use round and put in our variable and let's round the number to two places.

====================== RESTART: /root/Desktop/main5.py ======================
sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5

let's go ahead and see if we can run this and see what we get the sign of 0.5 two three five nine nine radians equivalent to thirty degrees is 0.5 and you can see it's quite a large number, so that's why we put in the rounded and we get just a simple 0.5, which is more an answer you would probably expect.

Okay so now let's say you wanted to instead of inputting radians you want to be able to input your degrees number, instead of this 0.5 2 3 5 9 9 you might be more used to just or more familiar with putting in 30 degrees. So let's show you how you can do that so let's go ahead and create our variable

# Sine input as degress
sine_degrees = math.sin(math.radians(30))

sine degrees let's use our math module and access the sine and again we want to use the math module and access radians. And this might be a little bit counter intuitive at first, but just note that the math that radians, converts angle X from degrees to radians which allows you to input your parameter as degrees, okay so we're gonna put in 30 degrees.

Now let's go ahead and display this

# Display sine normal and rounded (degress)
print('sine of 30 degrees is', sine_degrees, 'rounded', round(sine_degrees, 2))

use our print sine of 30 degrees is comma but in our sine degrees variable, another comma was put in a rounded note string comma, use round put in sine degrees and comma two to round to two places. Let's print this out see what we get

sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5
sine of 30 degrees is 0.49999999999999994 rounded 0.5

sine of 30 degrees is 0.499 repeating and rounded you get 0.5. So essentially you get the same answer in either case using the radians input or the degrees input.

Okay so now let's say that you wanted to see the answer as a ratio or a fraction, to do that we can use our previous input the from fractions import fraction

# Display sine as fraction ratio
print('sine of 30 degress ratoi is', Fraction(sine_degrees))

so let's just go ahead and use a print statement to display, let's put in a little note here sine of 30 degrees ratio is, comma, type out fraction friends put in your sine degrees variable and what you're gonna see is if you print this out as is you get quite a large number

====================== RESTART: /root/Desktop/main5.py ======================
sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5
sine of 30 degrees is 0.49999999999999994 rounded 0.5
sine of 30 degress ratoi is 9007199254740991/18014398509481984

so let's show you what you get, you can see sine of 30 degrees ratio is and you get this great big huge fraction, so to fix that or turn that into something that's a little bit more readable or what you might expect

# Display sine as fraction ratio
print('sine of 30 degress ratoi is', Fraction(sine_degrees).limit_denominator())

you can use dot limit denominator and don't forget the parentheses right there, now let's print this out

====================== RESTART: /root/Desktop/main5.py ======================
sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5
sine of 30 degrees is 0.49999999999999994 rounded 0.5
sine of 30 degress ratoi is 1/2

and we should get one half when we do sine of 30 degrees ratio is one half, okay. So let's do one more example for the sine and let's use the numpy module in this case.

# Example using numpy
sine_with_numpy = numpy.sin(numpy.deg2rad(30))
print('sine of 30 degrees using numpy is', Fraction(sine_with_numpy).limit_denominator())

So let's create our variable sine with numpy equals numpy access the sine, and in this case we're gonna use numpy again and we're gonna access the degrees to radians, we can just put in out degrees and let's print this to display it put in a little note sine of 30 degrees using numpy is, comma, let's go ahead and use our fraction again put in our variable sine with numpy, and we're gonna use the limit denominator again to give us a ratio of our fraction that's more readable. So let's print this out

sine of 30 degrees using numpy is 1/2

see what we get ,sine of 30 degrees using numpy is ½, okay. So now let's go ahead and move on to cosine and tangent, but just know that most of the examples we showed here for the sine will carry over to the cosine and the tangent.

Cosine

Okay so let's create our cosine variable

# Cosine
cosine_degrees = math.cos(math.radians(60))
print('cosine of 60 degrees is', round(cosine_degrees, 2), 'or', Fraction(cosine_degrees).limit_denominator)

cosine let's just do degrees, use the math module acces cosine, and use the math module again to access the radians, and let's put in 60 degrees. Let's go ahead and print to display put in our hour note cosine of 60 degrees is, comma, let's use our round cosine degrees around the two places and put in the or string and we're also going to show it as a fraction, and don't forget to limit the limit denominator if you want to show it as a more readable fraction.
Let's print this and see what we get

====================== RESTART: /root/Desktop/main5.py ======================
sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5
sine of 30 degrees is 0.49999999999999994 rounded 0.5
sine of 30 degress ratoi is 1/2
sine of 30 degrees using numpy is 1/2
cosine of 60 degrees is 0.5 or <bound method Fraction.limit_denominator of Fraction(4503599627370497, 9007199254740992)>

cosine of 60 degrees is 0.5 or ½.

Tangent

So for our last example let's show you how to calculate the tangent create our tangent variable

# Tangent
tangent_degrees = math.tan(math.radians(45))
print('tangent of 45 degrees is', round(tangent_degrees, 2))

tangent degrees use our math module tangent math module again and use of radians let's put in 45 degrees, and let's print with a note tangent of 45 degrees is, let's use our round and in this case the answer should just be one so we don't really need to do a ratio tangent degrees round it to two places let's run this and see what we get

====================== RESTART: /root/Desktop/main5.py ======================
sine of 0.523599 radians (30 degrees) is 0.5000001943375613 rounded 0.5
sine of 30 degrees is 0.49999999999999994 rounded 0.5
sine of 30 degress ratoi is 1/2
sine of 30 degrees using numpy is 1/2
cosine of 60 degrees is 0.5 or <bound method Fraction.limit_denominator of Fraction(4503599627370497, 9007199254740992)>
tangent of 45 degrees is 1.0

tangent of 45 degrees is 1.

Okay that is it for this tutorial on how to calculate sine cosine and tangent, we'll be doing many more Python tutorials in the near future join us for those and we'll see you next time.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!