I've been getting unexpected results when trying to convert a FLOAT to an INT.
int recastVar = (int)newDouble;
In my script when my double = 8.0, the recast int value returned is 7. The script below illustrates the problem that I'm having. This has had me rather confused for the past few hours. To note when the float = 9.0 or 7.0, the INT returned is 9 and 7 respectively.
Any help would be very welcome!
double myDouble = 6.9;
void setup()
{
Serial.begin(9600);
}
void loop()
{
cast_num();
delay(2000);
}
void cast_num()
{
if (myDouble == 9.5)
{
myDouble = 6;
}
myDouble = myDouble + 0.1;
double newDouble = myDouble;
int recastVar = (int)newDouble;
Serial.print ("float: ");
Serial.println(newDouble);
Serial.print ("re-cast: ");
Serial.println(recastVar);
Serial.println(" ");
}