-3

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"

  • 2
    `^` doesn't do what you think it does! – πάντα ῥεῖ Apr 08 '22 at 10:36
  • 2
    Copy-paste text (especially code) *as text* into your questions. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mre] and how to [edit] your questions to improve them. – Some programmer dude Apr 08 '22 at 10:37
  • 1
    Also, `2(x)` doesn't do what you want, it's actually not valid syntax. I recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn the basics (and that includes not using macros). – Some programmer dude Apr 08 '22 at 10:38
  • Why using macros? A short inline function would do the same and you are type-safe, can use variables, ... e.g.: `double g(double x) { x = sec(x); return x*x - .5; }` – Aconcagua Apr 08 '22 at 10:40

1 Answers1

3

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;
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52