0
>     if (((test>=0) && (test<=90)) || ((test>270) && (test<=360))){n_y=1;}
>     else {n_y=-1;}

I need the magnitude of trigonometric function in order to determine the sign of the trigonometric function for an angle falling into a particular quadrant.

My plan is to replace the code above with something equivalent.

Here is what I want to do in pseudo-code.

n_y = cos(test) / (magnitude of cos (test)); 

This will give me same thing. Abs() only takes integers. Any help is appreciated.

masterial
  • 2,166
  • 9
  • 33
  • 48
  • `abs` or the `fabs` family is all you need to find FP magnitude. But it sounds like what you actually want is quadrant from degrees. – Potatoswatter Apr 17 '10 at 20:45

2 Answers2

2

I don't know what Abs() you're using, fabs from the C++ standard takes doubles just fine.

But you don't really want magnitude, because then you're stuck doing an expensive division.

Instead just use a signum function.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Brilliant! no signum in C++, but the link led me to copysign Which does the above in one line: n_y=copysignf(1, cos(angle)); – masterial Apr 17 '10 at 20:50
  • @seaworthy: `copysign` is nonstandard. – Potatoswatter Apr 17 '10 at 20:55
  • @potatoswatter: it's part of math.h, isn't that a standard? – masterial Apr 17 '10 at 21:03
  • @seaworthy: It's standard C99 but not standard C++. – Potatoswatter Apr 17 '10 at 21:38
  • If not being standard C++ bothers you, the answers to the question I linked to supply several other ways of implementing signum. Of course, the original code is much faster than the new code calling `cos`, but didn't handle non-canonical angles. I'd think that modulo would still be faster though. – Ben Voigt Apr 17 '10 at 23:03
  • i guess it's a bit hacky, BUT the sign of float/double should be contained in the most significant bit of the number, just as for signed integers. (For IEEE-754 standard floating point) – nielsbot Apr 30 '11 at 04:31
0

Did you #include <cmath> to get the floating-point overloads for abs?

As for finding the quadrant, if 0 <= test <= 360, and you want to test 90 < test <= 270 just use 90 < test && test <= 270. There is a continuous range between the two discontinuous ranges you are currently testing. However, your particular example defines things asymmetrically as it maps 0 => 1 and 270 => -1.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421