Hello,
In my code I am reading from a sensor and want to check if the float value returned is out of range. In order to do so I would like to be able to calculate the absolute value of the float. Using abs() is not an option because it returns an integer: abs(-0.5) = 0
I found out the function fabs() which as I have seen on the internet, it is documented to do the abs operation with floats: fabs(-0.5) = 0.5. When using it, it does not seem to work as expected.
The following code:
float one, two; one = 0.028; two = -0.085; printk("one-two=three[%d]\n",(int32_t)(one-two)); float three = one-two; printk("three/one*100=percentage[%d]%%\n",(int32_t)(three/one*100.0)); float percentage = three/one*-100.0; printk("abs(percentage)[%d]\n",abs(percentage)); printk("fabs(percentage)[%d]\n",(int32_t)fabs(percentage)); printk("fabs(percentage) > 400: %u\n",(fabs(percentage)> 400.0)); printk("percentage < -400: %u\n",(percentage< -400.0)); printf("%f %f\n",percentage,fabs(percentage)); return;
Gives the following output:
Hello World! one-two=three[0] three/one*100=percentage[403]% abs(percentage)[403] fabs(percentage)[-2147483648] fabs(percentage) > 400: 0 percentage < -400: 1 -403.571411 0.000000
Hopefully I can find the answer to this. At the moment I would be able to substitute the fabs() functions for a custom made function that changes the sign if the float is negative.
Thanks,
Aleix.