在我的编程语言中尝试使用三角函数时,我得到了一个看似完全错误的结果。
例如,
但是我希望sin(90)的结果是1。
发生了什么事情,我该怎么解决?
例如,
sin(90) = 0.8939966636005579
但是我希望sin(90)的结果是1。
发生了什么事情,我该怎么解决?
sin(90) = 0.8939966636005579
math.radians
函数来实现。三角函数本身通常也会说明它们期望输入的格式。>>> import math
>>> math.sin(math.radians(90))
1.0
PI = 3.1415926535897932; // or your programming language's standard library's pi constant
function toRadians(degrees) {
return degrees * (PI / 180);
}
>>> import math
>>> math.sin(math.radians(90))
1.0
System.out.println(Math.sin(Math.toRadians(90.0)));
sin(deg2rad(90))
#include <stdio.h>
#include <math.h>
double deg2rad(double degrees) {
return (M_PI / 180) * degrees;
}
int main() {
printf("%f", sin(deg2rad(90.0)));
}
M_PI
,尽管很多实现都包括glibc,它声称来自Unix98和4.4BSD。如果你的实现没有定义,你需要自己定义。)#include <cmath>
#include <iostream>
#include <numbers>
float deg2rad(float degrees)
{
return (std::numbers::pi_v<float> / 180) * degrees;
}
int main()
{
std::cout << std::sin(deg2rad(90.0)) << '\n';
}
C# (三角函数文档*,还请参阅文档中的备注部分,了解Sin
,Cos
,Tan
的详细信息)
using Math;
public static double DegreesToRadians(double degrees)
{
return (Math.PI / 180) * degrees;
}
public static int Main() {
Console.WriteLine(Math.Sin(DegreesToRadians(90.0).ToString());
return 0;
}
javascript (MDN上的三角函数文档*,特别是度与弧度之间的转换部分,另请参阅如何使sin、cos和tan使用度而不是弧度?)
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
console.log(Math.sin(degToRad(90)));
sin
、cos
、tan
或类似的函数。sin
函数使用的是double
而不是float
,所以你的deg2rad
函数也应该使用double
。你还可以根据三角函数的类比引入deg2radf
(使用float
)和deg2radl
(使用long double
)。 - undefinedprint (double)3.14...
) - undefined