Big problem

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);
}

could you tell me how to get a write value ?

Don't print 14 digits, you only get about 8 in a float.

And this is perfectly normal.

i still get error even the 8 digit after my value

float is good for 6-7 significant digits AT BEST. What you show is correct to 7 digits.

Regards,
Ray L.

RayLivingston:
float is good for 6-7 significant digits AT BEST. What you show is correct to 7 digits.

Regards,
Ray L.

I think what he wants is 9.76

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 :frowning:

Serial.print() is supposed to default to 2 decimal places, according to the reference. Is that wrong?

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 :frowning:

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...

9.860000000000000000000000000000000000000000000000000000000...

But the computer must draw the line somewhere and limit the precision. That is why the error. It is normal, as stated in reply#1.

Integers don't have that problem, by definition.

but i should to increment this value, how to solve this problem ?

jone31:
but i should to increment this value, how to solve this problem ?

You have to post your code (please use code tags!), in order for us to answer that question.

i use a rtos in arduino
the ticks in arduino = 976 so 1MS=976. me i have to get a sample every 10 MS in 1 seconde so i did this code

float timeT =0;

my thread

for (b = 0; b <100;b++)
{
timeT += (9.76);
my_func(); // geting sample
chThdSleepUntil((timeT));//

}
if i want to calculate for 5 minute i will get errors because the wrong value of float variable :frowning:

my_func();

Most useful code provided as yet.

Johnny010:

my_func();

Most useful code provided as yet.

...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.

Simple, use unsigned long and do it in Ticks - float is too slow anyway.

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.

If 9.76 was the quantity required, then a scaled int would easily handle it. A long would be overkill. :slight_smile:

But i want to use float value not integer value

LOL, why?

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.

jone31:
But i want to use float value not integer value

I think you have designed the problem into your system, by using a flawed approach. You are simply too stubborn to admit it.