I tried searching for the answer to this question but to no avail. What I am trying to do is count the 0s of cos(x) between 0 and a number provided by a user in radians. I'm new to c++, so be gentle if this is trivial.
This is an excerpt from my code, (also note I'm not exactly sure about the difference between #import
and #include
, but I digress).
#define _USE_MATH_DEFINES
#import <iostream>
#import <algorithm>
#include <math.h>
#include <stdio.h>
int count = 0;
if (r > 0) {
for (float i = 0; i < r; i += M_PI_2) {
printf("\n\n%s: %f: %f\n\n", cos(i) == -float(0) ? "true":"false", \
cos(i), -float(0));
if (cos(i) == float(0) || cos(i) == -0) {
count++;
printf("Count: %d", count);
}
}
Using the value 2R as r, I should get the count to increase once but the if statement evaluates to false. I'm not sure I should be casting float to zero, nor checking against -0. Anyway this is the output to the print statements in this snippet of code.
It looks like cos(M_PI_2)
in fact equals -float(0)
so why does cos(M_PI_2) == -float(0)
evaluate to false
?? Again I just started to pick up c++ days ago, so my apologies if this is trivial.