Forum

> > CS2D > Scripts > Game() Percentages/Floats
Forums overviewCS2D overview Scripts overviewLog in to reply

English Game() Percentages/Floats

4 replies
To the start Previous 1 Next To the start

old Game() Percentages/Floats

Apache uwu
User Off Offline

Quote
I've noticed on some commands that use floats or percentages (ex. mp_kickpercent, mp_zombiedmg, just to name a few), they seem to have some freaky weird return value like...

1
2
3
4
10% = 0.100000001
30% = 0.300000012
60% = 0.600000024
80% = 0.800000012

Even when I set the console command @ a certain value it returns back to those values.

Prove of concept
More >

old Re: Game() Percentages/Floats

archmage
User Off Offline

Quote
You don't need 10^-8 precision use this to remove the incorrect/uneeded data
1
2
3
4
5
function setprec(n, p)
	return math.floor(n*10^p)/10^p
end

print(setprec(tonumber(game("mp_kickpercent")), 2))

old Re: Game() Percentages/Floats

Apache uwu
User Off Offline

Quote
It's not a problem, but I would like a explanation why floats are stored at that value.

EDIT: Nevermind FlooD's link explains it pretty well.

old Re: Game() Percentages/Floats

Lee
Moderator Off Offline

Quote
Floats are stored using a finite number of bits, which is itself imperfect. But beyond the floating point inaccuracy problem, most languages implement some form of a floating point printer that also uses an error correcting algorithm to round to the closest representation.

See Steele & Guy's paper - http://pic.plover.com/p372-steele.pdf

As you can see, certain numbers have larger error margins than those that can be correctly compensated for by the DRAGON or one of its derivative algorithms, so they return imprecise results.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview