Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
a=b//c VS a=math.floor(b/c) — Gideros Forum

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

keszeghkeszegh Member
edited August 12 in Bugs and issues
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'.


  • hgy29hgy29 Maintainer
    Accepted Answer
    // 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
  • in this case in the docs it should be made more clear:
    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?
  • hgy29hgy29 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
  • yes, probably not much faster or at least it does not matter if it is called a few times per frame.
Sign In or Register to comment.