Hello everyone, I have a problem with floating point variables. I am defining a variable as float, to which I add a constant, with a value of 54.3. The value is stored in the variable as 54.2999999, this happens because of a hexadecimal conversion issue, but how can I avoid it? Where can I set the variables somehow ?
Thank you very much.
I t would be best to show the related code here.
However ...
> I am defining a variable as float, to which I add a constant, with a value of 54.3.
This number is not a float, but a double. To avoid C type promotion issues, be more specific with constants. In your case, it would be 54.3f. Otherwise, a double constant is created by the compiler, and then rounded / converted to float.
And second, float has a quite limited accuracy, consisting of only 24 bit mantissa.
But what is your problem ?
The float format has a limited amount of numbers it can express exactly, and thus a limited accuracy. But does it affect the correctness of your code, or the stability of algorithms you are using ?
If you get hung up about the deviation in the smallest digit, I cannot help you.
@frank_m My problem is as follows:
I create a variable type float, and I save a value in it, specifically I save the constant 54.3. With this variable I work, and the problem is that when I save it as float it becomes the value 54.299999, and I would like to know if this can be defined in some configuration and that the value saved in this variable does not change to 54.29999 and stays at 54.3.
I hope I have explained myself well.
Thank you very much.
You write the constant "54.3" in your code - what is actually stored (and how) is a different matter.
> ...and the problem is that when I save it as float it becomes the value 54.299999
I suspect with "becoming the value 54.299999" you mean printing out as double number, probably using printf().
Not sure if you are aware, but the POSIX / standard library function printf() only knows of double arguments, and will convert floats accordingly. For the clib variants coming with MCUXpresso, you would need to check the manual, what printf and similar functions actually implement.
However, reducing the precision (e.g. with "%.1f" instead of "%f") will give you a rounded result, i.e. "54.3" instead of "54.299999" in your case.
Example:
float fn = 54.3f;
printf "float number = %.1f ", fn);
54.3 cannot be represented exactly, using float (or double, for that matter) and 54.299999 is as close as you can get. There is nothing wrong, it is a fundamental limitation of using binary to represent floating point numbers - you can only ever get a (close) approximation.
Thank you very much.
I know that it can't be represented as 54.3 because it works in binary, but I wanted to know if there was a way to configure the variables to save the exact number.
Thank you very much again.
Best regards.
it all depends on what you are trying to do.
You can always save and use as a double keeping in mind that when you print you need to round to 1 decimal place.
Another option is to save it in 0.1 units, for example, you can save it as integer 543 keeping in mind that this means 543 x 0.1
We can't say what is the best way to do it because we don't know what you are trying to achieve