i am trying to find bpm by taping. where "A" is gap of micros() in taps
you can see calculation in my code
unsigned long A = 525348;
float result = 0;
void setup() {
Serial.begin(9600);
result = (1000000/A);
Serial.println(result);
}
void loop() {
}
as result i am getting "1.00"
but by my calculater it should be "1.903500156087013"
and i want it "1.90"
then 1.90 * 60 = x , where x should be without decimal
what code is needed to write
By default, calculations are done using integers. So you need to force one of the parts of the devision to be a float; below will print 1.90350017.
unsigned long A = 525348;
float result = 0;
void setup() {
Serial.begin(57600);
result = (1000000.0 / A);
Serial.println(result, 8);
}
void loop() {
}
and i want it "1.90"
You can not really want that; a float is what it is and has the additional digits.
You can multiply by 100 and cast to an int or long. Next you can do the calculation and divide the result by 100.
unsigned long A = 525348;
float result = 0;
void setup() {
Serial.begin(57600);
result = (1000000.0 / A);
Serial.println(result, 8);
long x = result * 100L;
Serial.println(x);
x *= 60;
Serial.println(x);
x /= 100;
Serial.println(x);
}
void loop() {
}
Please be warned that the 'float' and 'double' types on the Ardiono UNO are only accurate to 6.9 significant digits. That means that you can count on the first six (1.90350) and usually the seventh digit (0) but the 8th and 9th significant digits (1 and 7) are not to be trusted.