Hello there, i discovred that arduino uno give us wrong value when we declare a float variables.
you can try it and see.
i declare float a= 9.76;
after the serial print i saw a =9.76000022888183
i don't know how to resolve it.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
float a= 9.76;
void loop() {
// put your main code here, to run repeatedly:
Serial.println(a, 14);
}
jone31:
yes i have a big big problem because i should to do an incrementing of a valur of 9.76 but after several samples i have error
All floating point implementations have such errors. Even on supercomputers. You might have to redesign that part of your program to use integers.
It happens because the computer can not store an infinite decimal fractional part. In your mind, you think
9.76 + 0.1 = 9.86 exactly, because
9.76000000000000000000000000000000000...
+
+0.100000000000000000000000000000000000000000000000...
...and most unhelpful name for a function ever...
get_sample() was just too hard to type?
Regardless, has it not been made clear that you should be using integers? You will have to change everything you're doing. There is no fix doing it your way. chThdSleepUntil() must be modified to accept an integer. You must pass a scaled value to it like 976.
jone31:
could you tell me how to get a write value ?
Variable type 'float' is accurate up to 6-7 significant digits at most.
So you can at most print 7 'significant' (counting starts with the first non-zero digit) digits when using 'float'.
Higher accuracy can be obtained with integer type numbers only.
Example (using unsigned long):
unsigned long a= 976000000UL;
char buf[21];
snprintf(buf,sizeof(buf),"%lu.%08lu",a/100000000UL,a%100000000UL);
Serial.println(buf);
This is doing some fixed decimals math and accuracy with unsigned numbers is 8 digits after the decimal point (9-10 significant digits total).
When using 64-bit 'long long' integers you could achieve even 18 significant digits accuracy.
jone31:
But i want to use float value not integer value
'float' values are ALWAYS inaccurate in decimal representation.
If you strictly 'want to use float', you strictly want to have inaccuracies with the numbers.
That's absolutely the same: 'float' and 'decimals inaccuracy', you cannot have one without the other.