Wait...where's div?

RukiriRukiri Posts: 58Member

I'm mainly waiting for C# to move over a game maker project to Godot, but I have one question.
Doesn't GDScript have div? I know / and % is generally the same thing but they do sometimes give different results.

so... whatsup?


  • Shin-NiLShin-NiL Posts: 158Moderator
    edited October 2017

    What's div? Is it a division operator? If so, the only division operator I've used along all my life was just / :wink:

    Can you show us an example of the different results you mentioned?

  • RukiriRukiri Posts: 58Member

    _tileSize = 32;
    _tilew = _tileSize
    _tileh = _tileSize
    _tileX = x div _tilew
    _tileY = y div _tielh

    / works but doesn't snap the object in place like div does, also it's very common in C#, C++, etc.

  • TwistedTwiglegTwistedTwigleg Posts: 1,843Admin

    Looking at the documentation for div (for C/C++) it looks like it divides and returns both the quotient and the remainder in a struct.

    Probably the reason you're getting different values is that your using the quotient when you're calling div, while / returns the quotient plus the remainder. You can get the quotient with GDScript using floor(x / y) and the remainder with (x/ y) - floor(x / y), or you might be able to use (x % y) to get the remainder if GDScript supports it.

  • Shin-NiLShin-NiL Posts: 158Moderator

    Alright, now I got, thanks @TwistedTwigleg .

    Another way to get the quotient is casting the numbers to int:
    var a = 7.0 var b = 5 var quotient = int(a) / int(b) # 1
    The modulo operator % just works with integers, so to use it or you need to cast both number to integer or use fmod() function.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file