Forum

> > CS2D > Scripts > operation "//" - idiv
Forums overviewCS2D overview Scripts overviewLog in to reply

English operation "//" - idiv

3 replies
To the start Previous 1 Next To the start

old operation "//" - idiv

-3Jlou_nTu4-
User Off Offline

Quote
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?

old Re: operation "//" - idiv

ohaz
User Off Offline

Quote
@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.

old Re: operation "//" - idiv

VADemon
User Off Offline

Quote
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview