a=b//c VS a=math.floor(b/c) — Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# a=b//c VS a=math.floor(b/c)

Member
edited August 2022
I've realized that if b/c is e.g. half of a negative integer then the above two do not give the same result. (although perhaps they should? at least for me that would be logical)
the reason is that // rounds 'towards 0' and not 'to the lower value'.

• Maintainer
// is an integer divide, it justs truncates of any fractional part, whereas math.floor will round to the lower integer and math.ceil to upper integer
• Member
in this case in the docs it should be made more clear:
https://wiki.gideros.rocks/index.php/Integer_Divide_Operator
in particular here:
a=b//c -- faster than a=math.floor(b/c)
could be replaced by
a=b//c -- faster than a=math.floor(b/c) and the same if b/c is positive

btw then i guess if i need math.floor(b/c) also for negative numbers, then i cannot make it faster using // somehow?
• Maintainer
Yes, it will be equivalent to math.floor for positive numbers and math.ceil for negative numbers. If you want an equivalent to math.floor for negative numbers, then you could try something like
 `a=b//c if a>b/c then a-=1 end`
but it may not be more efficient than math.floor

Likes: keszegh

+1 -1 (+1 / -0 )Share on Facebook
• Member
yes, probably not much faster or at least it does not matter if it is called a few times per frame.