Forum

> > CS2D > Scripts > Game() Percentages/Floats
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Game() Percentages/Floats

4 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Game() Percentages/Floats

Apache uwu
User Off Offline

Zitieren
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
Mehr >

alt Re: Game() Percentages/Floats

archmage
User Off Offline

Zitieren
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))

alt Re: Game() Percentages/Floats

Apache uwu
User Off Offline

Zitieren
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.

alt Re: Game() Percentages/Floats

Lee
Moderator Off Offline

Zitieren
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht