ChoiceScript's modulo operator is changing from "%" to "modulo"

ChoiceScript’s modulo operator is changing from a simple percent sign % to the word modulo.

If you’ve written code like this:

*set remainder X % 12

You should upgrade to the latest ChoiceScript on github and use this instead:

*set remainder X modulo 12

What's a modulo?

In 2011, I added an advanced operator to ChoiceScript, the “modulo” operator. Here’s how I described it in the Advanced ChoiceScript guide.

You can also use the modulo operator “%” to calculate the remainder after taking a division. Modulo is pretty weird, but it’s has two particularly interesting uses. First, you can check whether a number X is evenly divisible by a number Y by checking whether X % Y = 0. Second, you can use it to get the fractional part of a number X, the stuff that comes after the decimal point, by calculating X % 1. For example, 3.14 % 1 = 0.14.

modulo is the new modulo

Now, instead of a percent sign % you should use the word modulo as the operator.

Here’s the updated documentation:

You can also use the modulo operator calculate the remainder after taking a division. Modulo is pretty weird, but it’s has two particularly interesting uses. First, you can check whether a number X is evenly divisible by a number Y by checking whether X modulo Y = 0. Second, you can use it to get the fractional part of a number X, the stuff that comes after the decimal point, by calculating X modulo 1. For example, 3.14 modulo 1 = 0.14.

Why change it?

I realized recently that I made a terrible mistake using a percent sign % as the modulo operator; it’s too close to the much more commonly used FairMath operators %+ and %-.

That means that when you want to do this:

*set strength %+ 20

it’s too easy to forget to type a + or a - and do this:

*set strength % 20

ChoiceScript won’t flag that as an error, because it is valid (but baffling) code. It means the same thing as:

*set strength strength % 20

That means: take the current strength, divide it by 20 and compute the remainder. So if your Strength was 50, *set strength % 20 would set your Strength to be 10. That’s almost certainly not what you wanted.

As of the latest version of ChoiceScript, both % and modulo work, but I’ve updated the Quicktest tool to print a warning if it catches you using % as a modulo operator. The warning will link back to this forum thread, inviting you to fix it by replacing your % with the word modulo.

At some point in the future (months from now, at least; maybe a year?) I’ll probably make a breaking change to ChoiceScript to remove the old % operator entirely. In that case, any use of % will be flagged as an error.

FairMath Isn't Changing

We’re not changing FairMath at all. *set strength %+ 20 still works the same way it used to.

If you don’t already know what “modulo” does, you’ll probably never need it, so don’t worry about it.

11 Likes

So does this affect only fairmath then? What else is affected by the change?

If this affects fairmath, I’m going to be pissed… / breathes deep, calms self /This may not affect fairmath at all… I shouldn’t jump to conclusions. I should first ask, will it?

Please, pleeeease, if this change is affecting fairmath, just -add- the use of ‘modulo’ and don’t de-activate %. The difference between typing % and typing modulo may not seem like much, but when you multiply it a few thousand times, it’s a lot of work. -_-’ If it’s not, then- it’s my mistake mistaking what was meant.

Or maybe I should clarify-
If I use *set strength %+4 … is that still valid code as fairmath, or will that be flagged as an error? Or would it only be flagged if I accidentally put *set strength % 4 ? (I can’t actually imagine doing this, but that’s just me)

Dagnabbit Neo, if I got worked up because I saw your post that it affects fairmath and it doesn’t, grr. Just grr. …but I would like to know if fairmath is affected by this change or not.

It doesn’t affect fair math it’s just modulo that’s changing so a % on its on becomes modulo well that’s how I read that

2 Likes

If that’s the case… I’ve got no problem with the change. I just … don’t want to have to change all of my fairmath around. :\ Which… kinda sounds selfish… but… thousands of instances. :scream:

2 Likes

Your code is safe. I read it like @Nocturnal_Stillness as well.

and

You know the engine will be updated sooner or later … us slower writers are going to have to make changes anyways… although with your recent inspiration, your story is really coming along nicely.

This change has no effect on FairMath. *set strength %+ 20 works fine and will continue to work.

(I’ve added a line to the original post to clarify this.)

7 Likes

wilts with relief Oh thank goodness. relief smile

2 Likes

I would imagine the amount of users this will affect is very minimal. Modulo can be used for working out remainders and wether a number is odd or even. At any rate, a fair change, it’s very good to see proper deprecation over a longer period of time.

I don’t suppose I could tempt you to allow “mod” for brevity…?

3 Likes

I like the fact that you can Google modulo and get a Wikipedia page explaining the modulo operation.

Based on the games we’ve published, modulo is used rarely enough that the ulo won’t cause anybody RSI. :wink:

2 Likes

Yeah, I’m probably one of the more programmery ChoiceScript authors, and… (checking) I’ve never used modulo. I’ve never seen the need.

I suppose if I did complex string manipulation (like Aaron Reed did?) I would, but I haven’t used too many special-case functions.