0

I have a float value, that represents an angle. I need the quickest way to determine if the angle is between 0 and PI, or PI and 2 PI. If the float value is outside of the range 0 to 2 PI, it should still work. I have the following, but I fear it is slow compared to other solutions:

// float fltValue;
// bool blnResult;
while (fltValue < 0)
{ fltValue += 2 * PI; }
while (fltValue >= 2 * PI)
{ fltValue -= 2 * PI; }
if (fltValue < PI)
{ blnResult = true; }
else
{ blnResult = false; }

Is there a faster way of doing this? Preferably no loops.

Thank you.

  • 1
    Why isn't this just two `if` conditions? Why isn't this a simple function? What is the deal with adding to `fltValue`? Why a boolean when you have three different outcomes? – tadman Feb 11 '20 at 17:55
  • @tadman The comparsions are intended to be modulo 2*pi. – HolyBlackCat Feb 11 '20 at 17:55
  • 5
    Consider using [`fmod`](https://en.cppreference.com/w/cpp/numeric/math/fmod). – HolyBlackCat Feb 11 '20 at 17:56
  • @HolyBlackCat If it's modulo, then sure, `fmod` is the way to go. – tadman Feb 11 '20 at 17:56
  • 2
    use `fmod` and if it's negative add 2*pi and call it a day. – AlexG Feb 11 '20 at 17:57
  • `If the float value is outside of the range 0 to 2 PI, it should still work.` It is unclear **how** it should "work" when the float value is outside of the range 0 to 2 PI. Such angle is outside of the ranges 0 and PI, or PI and 2 PI, so presumably you want to return false? – eerorika Feb 11 '20 at 17:59
  • 1
    Or if you *really* want to avoid conditionals, use `fmod`, add 2 Pi, then use `fmod` again. – Beta Feb 11 '20 at 18:08

0 Answers0