I have wrote an algorithm for MCU that executes the expression of two paralel Resistances before doing more calculations with the division result. When debugging the values had no sense; they were too much big and float var did not update their initial value.
So I went to https://www.onlinegdb.com/ and I tried a part of my algorithm there. When execute the code it launches a non normally exiting with the arithmetic exception at line:
a = 1/(1/10 + 1/b) + 0.15;
First I was working with float but I have thought that exception could be caused by an overflow, hence I did the variable storage bigger using double, but the same exception appears. Then I tried doing the same but saying:
a = (1/10 + 1/b) + 0.15;
and the execution worked!
So, I have seen the '1' operator was the cause. But I don't understand exactly why and how to fix it (without using math.h).
The code is this:
#include <stdio.h>
float a = 0.0;
int b = 100;
int main()
{
a = 1/(1/10 + 1/b) + 0.15;//Req
b = a; //get int part
a *= 10;//fractionary part converted to dec
if ((a - b*10)>5) b++;
printf("float: %f , int: %i",a,b);
return 0;
}
I was expecting to get (debugging):
- a = 1/(1/10 + 1/100) = 9.09
which I think is not a big value to fit in a float or a double var. Instead of this I get the exception.
How to work with floats/doubles and int values to avoid exceptions when 1/(something smaller)?