Modulus/Modulo returns incorrect value

Bugs will be moved here once resolved.

Post » Sun Jun 26, 2016 11:25 pm

Problem Description
modulus returns incorrect value. ex: -180%360 should return 180, but instead it returns -180.

Attach a Capx
https://1drv.ms/u/s!AhHSZHEulqh_gTrqjJ-vqheM0zLp

Description of Capx
on start sets text to the (-180%360)

Steps to Reproduce Bug
  • run

Observed Result
it returns -180.

Expected Result
should return 180

Affected Browsers
  • Chrome: (YES)
  • FireFox: (YES)
  • Internet Explorer: (YES)

Operating System and Service Pack
vista sp2

Construct 2 Version ID
r227
B
47
S
22
G
65
Posts: 1,127
Reputation: 38,395

Post » Mon Jun 27, 2016 12:05 am

It's not really a bug, it's just doing it how the javascript % does it. Also apparently there isn't a consensus in the computer world what % should exactly do when one of the values is negative. See here, the sign of the result varies depending on the language:
https://en.wikipedia.org/wiki/Modulo_operation

For an always positive remainder you could do one of these:
(a%b+b)%b
a-int(a/b)*b
B
94
S
33
G
113
Posts: 5,356
Reputation: 73,273

Post » Mon Jun 27, 2016 12:30 am

thanks! I didn't realize it was different depending on the language.
B
47
S
22
G
65
Posts: 1,127
Reputation: 38,395

Post » Mon Jul 11, 2016 12:12 pm

Closing as not a bug - most languages I've used treat % as allowing numbers to stay negative, but you can always just add in an abs() if you want different behavior.
Scirra Founder
B
399
S
236
G
89
Posts: 24,519
Reputation: 195,361


Return to Closed bugs

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests