Forum

> > CS2D > Scripts > operation "//" - idiv
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch operation "//" - idiv

3 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt operation "//" - idiv

-3Jlou_nTu4-
User Off Offline

Zitieren
I didn't find integer division in cs2d lua. And forced use this code to do it
1
2
3
function idiv(value1, value2)
	return math.floor(value1 / value2 + 0.5)
end
Maybe in cs2d an operation is described that performs it quickly such us: value1//value2?

alt Re: operation "//" - idiv

ohaz
User Off Offline

Zitieren
@user Bowlinghead: That's only the CS2D part. The Lua in CS2D however contains all the Lua commands available.

There actually is a // operator in Lua. Have you tried it? Maybe it's just in a newer version, then @user DC would have to update the Lua version.

alt Re: operation "//" - idiv

VADemon
User Off Offline

Zitieren
Integer division was added in Lua 5.3 because it also added a seamless number subtype: integer (numbers ending with .0 like 1.0)

You can simulate // using this:
1
math.floor(x/y)
The number is always rounded down.

https://www.lua.org/cgi-bin/demo :
1
2
print(1//2,  math.floor(1/2))
print(-1//2, math.floor(-1/2))

The real advantage of integer division is that it's faster, not because you lose the fractional part. You always had the math.floor (round down, towards -∞) / math.ceil (round up, towards +∞) option for that.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht