To calculate the top left corner of the bounding box of a rotated rectangle (e.g. Calculate Bounding box coordinates from a rotated rectangle) I am trying to act differently based on the angle of rotation (to determine quadrants 0..90°..180°..270°..360°), given in degrees. The angle is convert to radians for the comparison with PI:
// http://codepad.org/zCxKJy30
#include <iostream>
#include "math.h"
using namespace std;
int main()
{
float deg = 90.0;
float rad = deg / 360.0 * 2.0 * M_PI;
if( rad <= M_PI/2.0)
{
cout << "less or equal";
}
else
{
cout << "larger";
}
return 0;
}
I would expect the condition to be true, but it is not.
I was wondering if there is a simple solution, unlike How to compute correctly rounded trigonometric functions in degrees? and How can I account for round-off errors in floating-point arithmetic for inverse trig (and sqrt) functions (in C)? suggest.
I just tried using the double type and then the comparison works as expected: http://codepad.org/UHcee1RE
Why does increasing the precision result in the "correct" evaluation?