Im writing a code where measurements, that are taken every half second, need to be subtracted from an initial value to reach 0 eventually. Both values are float. Initial value is 140 000 000 and measurements range from 0.320000001 to 0.389999999.
float batt = 140000000.00; //capacity 140M units
float subtr;
/.../
while(1){
batt = float(batt - subtr);
/.../
}
So basically I would need it to subtract 0.3xxxxxxxx every cycle of the loop from 148,000,000.00 but it seems there is a size problem so when I debug it I still get 148M every time.
I tried with 1000x smaller batt
batt value, 148 000, and converted the measurements from 0.3xxxxxxxx to 0.0003xxxxxxxx. When debugging the code, 148000 - 0.000300005049(measurement value) gives me 147999.469 which is .530699 off from the expected result(147,999.999,699).
It seems that float is not accurate enough for my needs, should I convert my values to some other type or is there any other way I could get accurate results? Was thinking of converting measurements to values without decimals but that wouldn't work either, because the initial value would get way too big for float(148*10^15). When using 140,000,000.00 I am expecting to get accuracy of three decimal places(.xxx) and when using 140,000.00 accuracy of six decimal places(.xxx,xxx) accordingly.