고등학교에서 배우는 삼각함수는 프로그램을 개발할 때 종종 사용되고, 개인적인 필요에 따라 삼각함수를 사용해야 할 수도 있습니다. 이 글에서는 여러 가지 프로그래밍 언어에서 삼각함수를 구현하는 방법을 소개하겠습니다. 학교 과제용으로 활용할 수도, 삼각함수를 공부할 때 개인적으로 사용할 수도 있을 것입니다.
1. 파이썬에서 구현하기
파이썬은 사용하기 정말 편리합니다. 개발자 관점에서 이렇게 편한 개발 언어는 없죠. 속도가 느리다는 단점이 있기는 하지만 편의성이 모든 것을 해결해줄 정도로 파이썬의 편의성은 우수합니다. 파이썬에서는 기본적으로 삼각함수를 계산하는 함수를 제공합니다. math 라이브러리를 이용하여 삼각함수 계산 함수를 만들어보겠습니다.
import math def sin(x): return math.sin(x) def cos(x): return math.cos(x) def tan(x): return math.tan(x) def cot(x): return 1 / math.tan(x) def sec(x): return 1 / math.cos(x) def csc(x): return 1 / math.sin(x)
위와 같이 math 라이브러리에서 제공하는 삼각함수인 sin(), cos(), tan() 함수를 사용하여 각 삼각함수를 계산하는 함수를 만들어줄 수 있습니다. cot(), sec(), csc() 함수는 각각 tan(), cos(), sin() 함수의 역수를 계산해주는 함수입니다.
이제 위 함수들을 이용하여 각도를 라디안으로 변환하여 각 삼각함수의 값을 계산하는 예제 코드를 작성해보겠습니다.
import math def sin(x): return math.sin(math.radians(x)) def cos(x): return math.cos(math.radians(x)) def tan(x): return math.tan(math.radians(x)) def cot(x): return 1 / math.tan(math.radians(x)) def sec(x): return 1 / math.cos(math.radians(x)) def csc(x): return 1 / math.sin(math.radians(x)) # 각도를 입력받아 각 삼각함수의 값을 계산하여 출력 angle = float(input("각도를 입력하세요: ")) print("sin({}) = {}".format(angle, sin(angle))) print("cos({}) = {}".format(angle, cos(angle))) print("tan({}) = {}".format(angle, tan(angle))) print("cot({}) = {}".format(angle, cot(angle))) print("sec({}) = {}".format(angle, sec(angle))) print("csc({}) = {}".format(angle, csc(angle)))
위 코드를 실행하면 각도를 입력받아 입력한 각도의 삼각함수 값을 계산하여 출력합니다.
2. C언어에서 구현하기
C 언어에서는 math.h 라이브러리를 이용하여 삼각함수 계산 함수를 제공합니다. 이 라이브러리를 이용하여 각 삼각함수를 계산하는 함수를 만들어보겠습니다.
#include <math.h> double sin_func(double x) { return sin(x); } double cos_func(double x) { return cos(x); } double tan_func(double x) { return tan(x); } double cot_func(double x) { return 1.0 / tan(x); } double sec_func(double x) { return 1.0 / cos(x); } double csc_func(double x) { return 1.0 / sin(x); }
위와 같이 math.h 라이브러리에서 제공하는 삼각함수인 sin(), cos(), tan() 함수를 사용하여 각 삼각함수를 계산하는 함수를 만들어줄 수 있습니다. cot(), sec(), csc() 함수는 각각 tan(), cos(), sin() 함수의 역수를 계산해주는 함수입니다.
이제 위 함수들을 이용하여 각도를 라디안으로 변환하여 각 삼각함수의 값을 계산하는 예제 코드를 작성해보겠습니다.
#include <stdio.h> #include <math.h> double sin_func(double x) { return sin(x); } double cos_func(double x) { return cos(x); } double tan_func(double x) { return tan(x); } double cot_func(double x) { return 1.0 / tan(x); } double sec_func(double x) { return 1.0 / cos(x); } double csc_func(double x) { return 1.0 / sin(x); } int main() { double angle; printf("각도를 입력하세요: "); scanf("%lf", &angle); printf("sin(%f) = %f\n", angle, sin_func(angle * M_PI / 180.0)); printf("cos(%f) = %f\n", angle, cos_func(angle * M_PI / 180.0)); printf("tan(%f) = %f\n", angle, tan_func(angle * M_PI / 180.0)); printf("cot(%f) = %f\n", angle, cot_func(angle * M_PI / 180.0)); printf("sec(%f) = %f\n", angle, sec_func(angle * M_PI / 180.0)); printf("csc(%f) = %f\n", angle, csc_func(angle * M_PI / 180.0)); return 0; }
위 코드를 실행하면 각도를 입력받아 입력한 각도의 삼각함수 값을 계산하여 출력합니다.
3. Java로 구현하기
Java에서는 java.lang.Math 클래스를 이용하여 삼각함수 계산 함수를 제공합니다. 이 클래스를 이용하여 각 삼각함수를 계산하는 함수를 만들어보겠습니다.
public class TrigFunctions { public static double sin(double x) { return Math.sin(Math.toRadians(x)); } public static double cos(double x) { return Math.cos(Math.toRadians(x)); } public static double tan(double x) { return Math.tan(Math.toRadians(x)); } public static double cot(double x) { return 1.0 / Math.tan(Math.toRadians(x)); } public static double sec(double x) { return 1.0 / Math.cos(Math.toRadians(x)); } public static double csc(double x) { return 1.0 / Math.sin(Math.toRadians(x)); } }
위와 같이 Math 클래스에서 제공하는 삼각함수인 sin(), cos(), tan() 함수를 사용하여 각 삼각함수를 계산하는 함수를 만들어줄 수 있습니다. cot(), sec(), csc() 함수는 각각 tan(), cos(), sin() 함수의 역수를 계산해주는 함수입니다. toRadians() 메소드를 사용하여 각도를 라디안으로 변환해줍니다.
이제 위 함수들을 이용하여 각도를 라디안으로 변환하여 각 삼각함수의 값을 계산하는 예제 코드를 작성해보겠습니다.
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("각도를 입력하세요: "); double angle = scanner.nextDouble(); System.out.println("sin(" + angle + ") = " + TrigFunctions.sin(angle)); System.out.println("cos(" + angle + ") = " + TrigFunctions.cos(angle)); System.out.println("tan(" + angle + ") = " + TrigFunctions.tan(angle)); System.out.println("cot(" + angle + ") = " + TrigFunctions.cot(angle)); System.out.println("sec(" + angle + ") = " + TrigFunctions.sec(angle)); System.out.println("csc(" + angle + ") = " + TrigFunctions.csc(angle)); } }
위 코드를 실행하면 각도를 입력받아 입력한 각도의 삼각함수 값을 계산하여 출력합니다.
4. Javascript로 구현하기
JavaScript에서는 Math 객체를 이용하여 삼각함수 계산 함수를 제공합니다. 이 객체를 이용하여 각 삼각함수를 계산하는 함수를 만들어보겠습니다.
function sin(x) { return Math.sin((x * Math.PI) / 180); } function cos(x) { return Math.cos((x * Math.PI) / 180); } function tan(x) { return Math.tan((x * Math.PI) / 180); } function cot(x) { return 1 / Math.tan((x * Math.PI) / 180); } function sec(x) { return 1 / Math.cos((x * Math.PI) / 180); } function csc(x) { return 1 / Math.sin((x * Math.PI) / 180); }
위와 같이 Math 객체에서 제공하는 삼각함수인 sin(), cos(), tan() 함수를 사용하여 각 삼각함수를 계산하는 함수를 만들어줄 수 있습니다. cot(), sec(), csc() 함수는 각각 tan(), cos(), sin() 함수의 역수를 계산해주는 함수입니다. 각도를 라디안으로 변환해주기 위해 Math.PI를 사용합니다.
이제 위 함수들을 이용하여 각도를 라디안으로 변환하여 각 삼각함수의 값을 계산하는 예제 코드를 작성해보겠습니다.
let angle = prompt("각도를 입력하세요:"); console.log(`sin(${angle}) = ${sin(angle)}`); console.log(`cos(${angle}) = ${cos(angle)}`); console.log(`tan(${angle}) = ${tan(angle)}`); console.log(`cot(${angle}) = ${cot(angle)}`); console.log(`sec(${angle}) = ${sec(angle)}`); console.log(`csc(${angle}) = ${csc(angle)}`);
위 코드를 실행하면 각도를 입력받아 입력한 각도의 삼각함수 값을 계산하여 출력합니다.