Created
November 7, 2016 20:49
-
-
Save ldante86/63469560f8f521262b4a3bcebfd5cb31 to your computer and use it in GitHub Desktop.
trig functions for bc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bc -q | |
# | |
# PURPOSE: This script calculates sine, cosine and tangent using | |
# the Taylor series. | |
scale=6 | |
pi = 3.14159265358979323844 | |
define factorial(a) { | |
i = a - 1 | |
for ( ; i >= 1 ; i-- ) { | |
c = a * i | |
a = c | |
} | |
return a | |
} | |
define sin(x) { | |
a=((x^3)/factorial(3)) | |
b=((x^5)/factorial(5)) | |
c=((x^7)/factorial(7)) | |
d=(((x - a) + b) - c) | |
return d | |
} | |
define cos(x) { | |
a=((x^2)/factorial(2)) | |
b=((x^4)/factorial(4)) | |
c=((x^6)/factorial(6)) | |
d=(((1 - a) + b) - c) | |
return d | |
} | |
define tan(x) { | |
return sin(x)/cos(x) | |
} | |
print "\nNumber: " ; x = read () | |
print "\n\tsin: " ; sin(x*pi/180) | |
print "\tcos: " ; cos(x*pi/180) | |
print "\ttan: " ; tan(x*pi/180) | |
print "\n" | |
quit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment