Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

GDScript: Modulo operator does not function on floats #10004

Closed
erodozer opened this issue Jul 31, 2017 · 12 comments
Closed

GDScript: Modulo operator does not function on floats #10004

erodozer opened this issue Jul 31, 2017 · 12 comments

Comments

@erodozer
Copy link

erodozer commented Jul 31, 2017

Operating system or device - Godot version:
Windows 10
Godot 3 alpha-1

Issue description:
Using the modulo operator in GDScript only works for integers. It should also work for floats.

Steps to reproduce:
Attempting to do the following math results in compilation errors, preventing your app from running

var x = 10.0
var y = 4
print( x % 4)
@ghost
Copy link

ghost commented Jul 31, 2017

Can't reproduce on latest master 3a3915b
print( x % 4) prints 2

Edit: tried on Godot 3 alpha 1 Linux x86_64, no error.

@bojidar-bg
Copy link
Contributor

Can you screenshot/copy the compilation error?

@erodozer
Copy link
Author

erodozer commented Jul 31, 2017

My apologies, turns out the issue in particular is that you can not use modulo on floats. In particular I get the error Invalid operands 'float' and 'int' in operator '..'. whenever I try to use % on a float

4.0 % 2 and 4.0 % 2.0 will both result in the error Invalid operands for operator but if it's a variable you won't know until runtime. Either way, modulo on floats is something available in other programming languages, so I see no reason why it shouldn't be available in GDScript

@erodozer erodozer changed the title GDScript: Modulo operator does not function on non-strings GDScript: Modulo operator does not function on floats Jul 31, 2017
@RandomShaper
Copy link
Member

We could make it call fmod when one or both operands are floats.

But as long as GDScript is dynamically typed, keeping it only available for ints at least allows you to be warned about a potential mistake.

@RandomShaper
Copy link
Member

So you can cast if computing with the integral part is what you really want or use fmod if you want to work in the floating point realm.

That also makes your code more explicit and your intent more clear.

@reduz
Copy link
Member

reduz commented Aug 4, 2017

it's not a bug, it's a feature (tm),intended to avoids mistakes. Use fmod() for floats.

@bojidar-bg
Copy link
Contributor

Note that JavaScript allows the % operator to be used on floats.

@akien-mga
Copy link
Member

I also think it's good to keep as a feature; people complain about duck typing precisely because of such things, so let's not make things more confusing needlessly.

@reduz
Copy link
Member

reduz commented Aug 31, 2017

ok, will close this as discussion is exhausted and there is no consense

@farberbrodsky
Copy link

I know this is a weird problem, but I made a workaround.
How do you do a modulo without a modulo?
Well, here is how it's done:

x = x - y * floor(x / y)

if you think about it, it works in all situations and functions the same way as modulo does.

@akien-mga
Copy link
Member

Well that's the definition of the modulo, so yeah, it works :) But it's simpler to just use the builtin fmod which does exactly that for you already.

@farberbrodsky
Copy link

I know this is a weird problem, but I made a workaround.
How do you do a modulo without a modulo?
Well, here is how it's done:

x = x - y * floor(x / y)

if you think about it, it works in all situations and functions the same way as modulo does.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

7 participants