Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error writing Sec^2(x) -0.5 x in define function in C++

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading