Wait...where's div?

RukiriRukiri Posts: 48Member

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?

Answers

  • Shin-NiLShin-NiL Posts: 111Moderator
    edited October 11

    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: 48Member

    _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: 109Member

    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: 111Moderator

    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.
Sign In or Register to comment.