Float value problem. -
Norck - 18.03.2011
With this code:
pawn Код:
printf("%f | %f | %f | %f | %f | %f",10.1,20.1,30.1,40.1,50.1,60.1);
I have this result:
Код:
10.100000 | 20.100000 | 30.100000 | 40.099998 | 50.099998 | 60.099998
Only first three values are correct. What's wrong with other?
Re: Float value problem. -
Sascha - 18.03.2011
sa-mp is rounding these floats... it does it many floats.... just try to enter "30.1000" and it will work better I guess..
or try floatround
https://sampwiki.blast.hk/wiki/Floatround
Re: Float value problem. -
Norck - 18.03.2011
Thanks for answer.
If I enter "30.10000", "40.100000" and so on, it will not help. I already tried it.
And these numbers are some kind of player's stats, so I need to show it as float, not integer, so floatround will not help here.
Re: Float value problem. -
Kyosaur - 18.03.2011
Well actually that IS correct, but if you're looking to trim it down to only one decimal place you have to specify that within printf.
instead of "%f" try "%.1f" and let me know if thats what you're looking for (i may have misunderstood the issue).
Re: Float value problem. -
Norck - 18.03.2011
Yes, i know, that it IS correct, but it doesn't work properly, hehe. Also, i know that %.1f will show only 1 digit as the fractional part.
So
pawn Код:
printf("%.1f | %.1f | %.1f | %.1f | %.1f | %.1f",10.1,20.1,30.1,40.1,50.1,60.1);
Gives
Код:
10.1 | 20.1 | 30.1 | 40.0 | 50.0 | 60.0
The problem is that in this code
Variable f1 should store 40.1, but it stores 40.099998 !!!
Another example:
The result is:
But it is works fine with 30.1:
The result is:
I hope you get my problem now.
Re: Float value problem. -
Kyosaur - 18.03.2011
Why do you need it to be EXACT? I cant think of a single time where that small of a different would affect anything really. I honestly dont know what i can do to help / what you expect :\ sorry.
Re: Float value problem. -
Norck - 18.03.2011
Believe me, i have a code, which is doesn't work properly because of this inaccuracy. Otherwise i'd not have started this topic
Just another example:
pawn Код:
// SomeStats[playerid] = 40.0
new str[32];
format(str,sizeof(str),"Your xxx is %.1f !",SomeStats[playerid]); // "Your xxx is 40.0 !"
SendClientMessage(playerid,COLOR_WHITE,str);
//....
//Now SomeStats[playerid] = 40.1
new str[32];
format(str,sizeof(str),"Your xxx is %.1f !",SomeStats[playerid]); // It will still show "Your xxx is 40.0 !"
SendClientMessage(playerid,COLOR_WHITE,str);
Re: Float value problem. -
Kyosaur - 18.03.2011
Quote:
Originally Posted by Norck
Believe me, i have a code, which is doesn't work properly because of this inaccuracy. Otherwise i'd not have started this topic
Just another example:
pawn Код:
// SomeStats[playerid] = 40.0 new str[32]; format(str,sizeof(str),"Your xxx is %.1f !",SomeStats[playerid]); // "Your xxx is 40.0 !" SendClientMessage(playerid,COLOR_WHITE,str);
//....
//Now SomeStats[playerid] = 40.1 new str[32]; format(str,sizeof(str),"Your xxx is %.1f !",SomeStats[playerid]); // It will still show "Your xxx is 40.0 !" SendClientMessage(playerid,COLOR_WHITE,str);
|
AFAIK there's nothing we can do about it not being SUPER precise (im sure there's a reason...i dont think its just a "derp", that would be a big fail). If you post the code that doesnt work due to this inaccuracies THEN we can do something (and i mean physically doesnt work, not printing a value that is slightly off).
Other than that, your kind of screwed unless someone else in this community has an answer and fix for you. Either way, i wish you luck and hope you get an answer!
Re: Float value problem. -
Norck - 18.03.2011
I just wondering: why it's work fine with 10.1, 20.1, 30.1 and doesn't work with 40.1, 50.1 and above.
Thanks anyway.
Re: Float value problem. -
Norck - 18.03.2011
Thank you very much. This is really useful information for me, now i know how is floats looks like in the binary representation.
But i still don't get this: why 30.1 shows correctly and 40.1 is not?
So with 16 bits:
Код:
30.1 = 0b00011110 00011001
40.1 = 0b00101000 00011001
There is the same fractional part: 0.1, so the only difference is in the first octet.
So it's seems like the integer part of a float affects on the output format of the fraction part? But why?