I am doing a project which rounds microsecond pulses(1000-2000). I have decided that the best way to round them is to divide them by 1000 and round the decimal, then multiply them by 1000. What is a varible that can work with decimals but is also precise?
I tried using int, it gave me 0s 1s and 2s
I tried using float, which gave me 0.00 1.00 and 2.00
These some examples of the pulses that I was dividing by 1000 were
1540
1558
1562
1475
1558
1562
1540
1558
1540
Is there a way I could get the quotient of the numbers to be 1.54 1.558 1.562 etc?
Thanks!
DPduino88:
I am doing a project which rounds microsecond pulses(1000-2000). I have decided that the best way to round them is to divide them by 1000 and round the decimal, then multiply them by 1000.
Most likely you DO NOT want to do an "integer division" by 1000, but perhaps you want to do a "float division" by a float divisor 1000.0 instead integer divisor 1000?
that's because you probably did integer maths and stored the result in the float
int x = 1540;
float f0 = x / 1000; // --> the compiler will calculate the right side as integer math, so truncates
float f1 = x / 1000.0; // the .0 changes everything, the compiler will compute the right side as float
float f2 = ((float) x) / 1000; // would work too. compiler will use float calculation as soon as one member is floating point
float f3 = ((float) x) / 1000.0; //of courseĀ would work too