'Error writing Sec^2(x) -0.5 x in define function in C++ [closed]
Okay I'm trying to make a root finder calculator and right now I was testing it out trigonometric functions. Now I'm getting errors whenever a sec is involved prompting either a "sec has not been defined"
Here's what it looks like Can someone explain to me whats wrong and how can I write "Sec^2(x)-0.5"
Solution 1:[1]
C++ standard library doesn't provide secant function, you have to define it yourself.
double sec(double x)
{
return 1 / cos(x);
}
Also, ^2 does not mean "square" in C++, it's "bitwise XOR". You need to use * or pow:
sec(x) * sec(x) - 0.5;
pow(sec(x), 2) - 0.5;
And don't use macros, they are going to bite you. Functions are much easier to use and will always behave as you expect:
double g(double x)
{
return sec(x) * sec(x) - 0.5;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
