Hi, it seems that log10(999999) gives the result of 6 instead of 5.99999956. This causes me a calculation problem.
Do you know a way to solve it?
Many thanks.
S_/S
Hi, it seems that log10(999999) gives the result of 6 instead of 5.99999956. This causes me a calculation problem.
Do you know a way to solve it?
Many thanks.
S_/S
It doesn't. Show your code, tell us your board.
Below prints 5.99999952
void setup()
{
Serial.begin(57600);
float f = log10(999999);
Serial.println(f, 8);
}
void loop()
{
// put your main code here, to run repeatedly:
}
Floating point numbers have a 6 to 7 digit precision.
Hi,
thank you for your reply.
My code is inside this user function:
void StampNum(int x, int y, long num, int nero, int totcifre, char suff[3]) {
numCifre=log10(num)+1;
If I call it from the main section by:
StampNum(106,5,999999,0,1,"");
the result inside numCifre is 7. Even if I Serial.print(numCifre) I obtain 7.00
I've temporary written the following lines in order to avoid the problem:
if (num>99999) numCifre=log10(num/10000)+5;
else numCifre=log10(num)+1;
but, of course I should prefer a better solution
I've added you function to my example
float numCifre;
void setup()
{
Serial.begin(57600);
float f = log10(999999);
Serial.println(f, 8);
StampNum(106, 5, 999999, 0, 1, "");
}
void loop()
{
// put your main code here, to run repeatedly:
}
void StampNum(int x, int y, long num, int nero, int totcifre, char suff[3])
{
numCifre = log10(num) + 1;
Serial.println(numCifre, 8);
}
Result:
5.99999952
6.99999952
Please post a full code example that we can compile and exhibits the behaviour.
Hi,
I tried again with the following code, that unfortunately confirmed the bug:
float numCifre;
numCifre=log10(num);
int q;
q=numCifre;
Serial.print("511 num=");Serial.print(num);Serial.print(" numCifre=");Serial.print(numCifre,8);Serial.print(" q=");Serial.println(q);
Results are:
511 num=999999 numCifre=7.00000000 q=7
511 num=1000000 numCifre=7.00000000 q=7
The main code uses a math expression like:
StampNum(106, 5, 999999+var, 0, 1, "");
Maybe the value passed to the function, as a result of the math expression, is really rounded in some way.
Anyway, thank you for your help.
You can easily test that; remove the "+var" and test.
Please post a simple example that compiles and shows the behaviour. And please use code tags as I did.
Hi,
pls forgive me if I don't write an example. I was not able to recreate the problem in a few of lines. It seems to appear only inside my (huge) code.
Anyway:
It's worrying that the problem only shows its face in bigger code. There can be several reasons for that that you actually need to address instead of working around.
In the worse case scenario, you have memory issues; common causes are e.g. the extensive use of Strings (capital S) or writing outside the boundaries of an array. This will result in a program that can crash at random times. Please scrutinise your code.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.