Xanthippe

Veteran
Veteran
Joined
Jan 21, 2013
Messages
24
Reaction score
11
First Language
English
Primarily Uses
OK can someone explain to me how this {100 - 25 * 3 % 4} = 97

the % mod is something of a complete mystery for me

P.S. I am only just starting lessons for ruby.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,693
Reaction score
3,027
First Language
Tagalog
Primarily Uses
RMVXA
100 - 25 * 3 % 4


that would be


25*3 = 75


75 % 4 = (75 - 72) = 3


100 - 3 = 97


the multiplication comes, first, then the modulo, then the subtraction.


You should learn how to use parentheses for grouping. if you want the modulo to be last for example, it should be


(100 - 25 * 3) % 4


which would be


100 - 75 = 25


25 % 4 = 1
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,699
Reaction score
944
First Language
English
Primarily Uses
N/A
Mod, modulo, or modulous are all programming names for %. It means the remainder of division, since sometimes we want to work with whole numbers instead of decimals. When following the order of operations, % is the same as division (I believe)
 

Kaelan

Veteran
Veteran
Joined
May 14, 2012
Messages
811
Reaction score
567
First Language
Portuguese
Primarily Uses
RMMV
If you're ever in doubt about order of operations, you should always be explicit use parenthesis to get the order of evaluation you want. Even if you know the order, you probably should still add parenthesis once you start doing more than 2 operations on the same line. It gets really hard to read really fast otherwise.
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,088
Reaction score
2,684
First Language
Greek
Primarily Uses
RMMZ
First off let's talk about a LITTLE mathematics.

There is a thing called an "operator".

Now you might heard of military operations, but what on earth is an operator in mathematics?

An operator is a symbol that tells you that an action, an operation is taking place here.

Operators have one or two things that participate on that operation.

These "things" are called operands.

An operand can be a number, a variable or a whole expression.

So we got an operator for addition.

It is the symbol +

and it adds 

We say

12 + (7 * X - 83)

12 is the one operand of + operator and the other operand is the WHOLE expression (7*X - 83) because of the parenthesis.

So we got it. We know what operator is.

And we know what an operand is.

Are there any other operators here?

Of course!

- for subtracting 

* for multiplying

and.... what is THAT % ????

This is called a MOD operator from the word "modulo".

It's nothing exotic really. You do it all the time already. :D

Imagine the time, It's 17:00.

What time is it?

Since we measure time using 12 hour circles, we can divide 17 with 12.

Then the remainder will give us what time it is.

So 12 fits one time in 17 right? We don't care about how many times it fits.

We care about what remains 17 - 12 *1 = 5. The remainder as we say.

OH! It's 5 O CLOCK IN THE EVENING.

So  X MOD Y means you take X and divide it with Y.

You must find how many times, this Y fits as a WHOLE in X.

Let's say it fits Z times.

Then you subtract the Y*Z number from X.

What remains is the MOD.

Another example of MOD.

Now it's 12 o' clock! 

What time will be after 79 HOURS?

6 * 12 = 72

79 - 72 = 7

Oh It will be 7 O clock!

79 MOD 12 = 7

79 % 12 = 7

Finally, there is a priority in operators.

Some operators are just faster sexy than the others.

In general, when you see a multiplier or a division, they go first!!!

(Division is the same as multiplication.  A / B = A * (1/B) correct? )

So they have same priority cause it is the same thing.

So both multilication and division wear boots of haste!

Then comes what? The modulo.  WRONG! 

Alongside with them goes the modulo as Shaz correctly mentions.

This is the remainder so it is slightly slower.

Finally a subtraction is the same as addition. It's like adding a negative number.

( A - 3 = A + (-3)  right? )

So addition and subtracion come to the end.

They run but they are more like marathon runners... they don't sprint.

Parentheses give higher priority than addition-subtraction.

It's like telling a marathon runner who runs slow... to suddenly sprint a little.

Parentehses apply to everything, giving an extra boost, like a power pill.

So... let's see...

100 - 25 * 3 % 4 = 97

100 - 75 % 4 = 97  (75 / 4 = 18,75. Take that 18 alone without decimals and do 18 * 4 = 72. So 75 - 72 = 3!!! ) 

100 - 75 % 4 = 97[SIZE=13.63636302948px] [/SIZE]

100 - 3 = 97

Got it?  ;)

EDIT: Correcting the chaos I made (thnaks Shaz) 

Now I go to sleep. :p
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,911
First Language
English
In division, there is the quotient and the remainder.

Code:
10 divide by 3equals3 remainder 1.
The % operator (modulus) returns the remainder.
 
Last edited by a moderator:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,741
Reaction score
15,878
First Language
English
Primarily Uses
RMMV
That's a nice, long, and colourful explanation. Unfortunately it contains a lot of mistakes.

We say


12 + (7 * X - 83)


12 is the one operand of + operator and the other operand is the WHOLE expression (7*X - 83) I added some  parentheses to feel more neat to the eye.
Parentheses do more than make it "feel more neat to the eye" - they completely change the result of that expression. This particular example, and where you chose to put the parentheses, is an exception (and it really doesn't make it neater - if anything, it makes it confusing, as they're completely unnecessary in those positions)


12 + (7 * X - 83)


gives exactly the same result as


12 + 7 * X - 83


which also gives the same result as


12 + (7 * X) - 83


In all cases, 7 * X will be done first, then it'll either subtract 83 and add 12, or add 12 and subtract 83 - which order does not matter.


The expressions below will give different results to the expressions above (and different results to each other as well), because the parentheses are being used to override the normal mathematical order of operations:


(12 + 7) * X - 83


12 + 7 * (X - 83)


(12 + 7) * (X - 83)

In general, when you see a multiplier or a division, they go first!!!


(Division is the same as multiplication. A / B = A * (1/B) correct? )


So they have same priority cause it is the same thing.


So both multilication and division wear boots of haste!


Then comes what? The modulo.


This is the remainder so it is slightly slower.


The modulo is a sprinter but does not wear boots of haste and always comes after them, cursing!
Close, but no. * / and % all have the same weight, so would be processed in a left to right order. 10 % 2 * 3 gives 0, not 4 - it does the 10 % 2 first and multiplies the result by 3


See group 5 (also do a test to confirm it).

Parentheses give higher priority than addition-subtraction
Parentheses give higher priority than ANYTHING. Everything already has a higher priority than addition/subtraction. If you want to perform + or - before * or / or %, THEN you use parentheses.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,693
Reaction score
3,027
First Language
Tagalog
Primarily Uses
RMVXA
yeah, grouping (parenthesis) has the highest priority. followed by exponents. then multi/divi/mod, last would be addition/subtraction
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,088
Reaction score
2,684
First Language
Greek
Primarily Uses
RMMZ
Yes indeed Shaz. I used that parenteses in the begining just to show the second operand as a whole.

I wanted to state that the parenthesis should NOT be there and I just put it so someone can understand easier that the second operand is the whole expression.

That's what I was talking about. My English are rusty so spare me that...  :p

Does % have the same priority as * and / ?

Wow that's something I honestly didn't know.

Makes sense but I was always thinking of it like a little slower than the multiplier and division.

Good to know.  :thumbsup-left:   :)

Parentheses give higher priority than ANYTHING. Everything already has a higher priority than addition/subtraction. If you want to perform + or - before * or / or %, THEN you use parentheses.

"Parentheses give higher priority than addition-subtraction.

It's like telling a marathon runner who runs slow... to suddenly sprint a little.

Parentheses apply to everything, giving an extra boost, like a power pill."

That's what I was trying to tell. You are right Shaz.

Oh my... that's embarrasing...

It  seems that trying to explain something in another language, makes things harder than I thought.

What was really nice though was that I learned something too during the proccess.

Thanks!  :)   :thumbsup-right:

Corrected these parts.
 
Last edited by a moderator:

Xanthippe

Veteran
Veteran
Joined
Jan 21, 2013
Messages
24
Reaction score
11
First Language
English
Primarily Uses
holy {#%*/}

lol

I'm going to make this page a favorites for a point of reference now. I was not expecting a huge response and so quickly.

This is a mountain of gold for me 

Thank you much people :)

As a side note, and I'm surprised I didn't see any mention of it here. It is being taught that the order is as fallows PEDMAS Parentheses, Exponents, Multiplication, Division, Addition and Subtraction

I know you guys already know this what I'm surprised about is that with all that has been said about the order no one mentioned this. I thought it was an old lesson. I guess this way must be new?

Thanks again guys. :)
 

BoluBolu

Veteran
Veteran
Joined
Apr 24, 2014
Messages
452
Reaction score
118
Primarily Uses
the % mod is something of a complete mystery for me
Alright there's already enough explanation above, I just want to give more example about modulo.

alpha = 10 % 5

alpha value will be 0, because 10 / 5 is 2 without any VALUE left, or we know 5 x 2 = 10, then 10 - 10 = 0

beta = 10 % 3

beta value will be ? that's right 1, because 10 / 3 is 3 but will left 1 as a remainder , or we know to reach 10, number 3 need to be multiplied by three times and then it becomes 9, the closest value to 10, but not pass over it.  But then it still give a remainder from 10, which is 1 and 1 is not  that can be divided by 3

So beta balue will be 1.

delta = 10 % 6

delta value will be 4, you know why right? Yes, 6 multiplied by 1 is 6, then 10 - 6 = 4

Here's some explanation again, and the main idea.

my_modulo = A % B, Does A divisible by B? If yes then the value of my_modulo will be 0. If not? Then B need to be multiplied with by a number, and the result of the multiplication must reach the closest value of A but not passing over it. Finally just substract A with the B, the result will become the value of my_modulo.

Here's the basic idea :

The general formula to calculate the mod

A % B would be to find a multiple of B, that less than A, but closest to A. Subtract A with the result of multiplication,then you got the value of the mod.

IMPORTANT : All my explanation above is good on positive operands, if you try to use negative operands,

For example:  10 % -3 or -5 % 8, or -30 % - 34(though the last will give same result like positive and positive)

Then you need more deep different logic explanation(although the algorythm is still same). 

Modulo with negative operands

alpha = -10 % 6

alpha will result in 2. Surprised? Unfortunately this is not wrong, let's get the workflow.

1. Does number -10 divisible by 6? No is not. Then alpha value will never be 0.

2. 6 need to be multiplied by a NUMBER to get into a value nearest to -10(negative 10). Let's see, in order to get negative value 6 must be multiplied by a negative value too(remember this), so let's try with -1,  the result is -6, Oh yeah we got the value closest to -10, DO WE..?, Wrong.. For negative number we know that -6 is greater than -10, so it's not legal(or what is the word?). So then we need to multiplied it again until we get the nearest value but again, NOT PASSING OVER IT, for this we will try -2, which then resulting in -12, Yaaay, finally we get the number we want.

3. Lastly check the remainder, for this case -10 - (-12) will result in 2 <= This is the final result.

Alright, that some explanation about modulo, hope it helps =), sorry if I made a mistake.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,741
Reaction score
15,878
First Language
English
Primarily Uses
RMMV
I've never heard of PEDMAS. At school we learned BOMDAS - Brackets, Of (powers), Multiplication, Division, Addition, Subtraction. At the time I wrote my post I was going to mention it, but I didn't want to use the term "of" and was having a brain malfunction and couldn't think of a better term. Exponent was what I was looking for :)
 

whitesphere

Veteran
Veteran
Joined
Mar 14, 2014
Messages
1,688
Reaction score
784
First Language
English
@Xanthippe

There is one other important operator set in Ruby:  Logical operators.  These are the AND &&, and OR || operators.   The Logical operators don't return a number.  They return True or False.  I don't know where they fit in the precedence of the operators.

Also, in Ruby, the last expression evaluated before a function returns is its return value.  So a function like:

def dummy()

3

end

Will return 3.    You'll see this from time to time.   In fact it's how the damage formula works in RPG Maker.  The engine evaluates the damage formula as a Ruby statement (where "a" refers to the source and "b" refers to the destination) and whatever it returns is the value.

You can also use the "return" line to return a value and exit the function.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,693
Reaction score
3,027
First Language
Tagalog
Primarily Uses
RMVXA
@whitesphere@ AFAIK, they would actually act like a grouping mechanism, separating the left-hand side and the right-hand side...


like


1 + 3 + 5 and 5 + 10 * 5


would be


9 and 55


which will then be evaluated to either true or false


That would be outside the scope of the topic at hand though, IMO
 
Last edited by a moderator:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
614
Reaction score
326
First Language
German
Primarily Uses
Though my post could be a bit off-topic I fear...

There is one other important operator set in Ruby:  Logical operators.  These are the AND &&, and OR || operators.   The Logical operators don't return a number.  They return True or False.  I don't know where they fit in the precedence of the operators.
While I would agree with that in general, it is not entirely correct for Ruby.
Rubys conditions can handle any data. A condition is fulfilled when the statement evaluates to something other than false or nil.

The logical AND operator returns the first operand when it is false or nil, otherwise the second operand.

The logical OR operator returns the first operand when it is neither false nor nil, otherwise the second operand.

In the Aces default scriptset you sometimes find constructs like this example:

@cache ||= {}

which is short for

@cache = @cache || {}

EDIT: There is a little difference between the two forms, although the result should be the same in most cases. When using ||= there is apparently no assignment when the variable already holds a valid value. See Tsukihimes post for details.

I didn't know about that, thanks. :)

since instance variables are initialized with nil the OR operator is used to assign a new Hash to the variable when it is executed for the first time.

Using parenthesis explicitely is indeed helpful sometimes to avoid behaviour like this:
true || false && false  # => true
true or false and false # => false


 
 
Last edited by a moderator:

Ralpf

Veteran
Veteran
Joined
Jun 5, 2014
Messages
590
Reaction score
153
First Language
English
I've never heard of PEDMAS. At school we learned BOMDAS - Brackets, Of (powers), Multiplication, Division, Addition, Subtraction. At the time I wrote my post I was going to mention it, but I didn't want to use the term "of" and was having a brain malfunction and couldn't think of a better term. Exponent was what I was looking for :)
They used a different one when i was in elementary school, I think. But mnemonics don't work for me most of the time, so I can't remember it.

But I found it online: "Please Excuse My Dear Aunt Sally" = Parenthesis, exponents, multiplication, division, addition, subtraction
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,911
First Language
English
In the Aces default scriptset you sometimes find constructs like this example:


@cache ||= {}


which is short for


@cache = @cache || {}


since instance variables are initialized with nil the OR operator is used to assign a new Hash to the variable when it is executed for the first time.
That is not entirely accurate and you may run into some problems depending on what you're working with


http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html


Though for "simple" things I'd say it's ok to interpret it like that.
 
Last edited by a moderator:

Latest Threads

Latest Posts

Latest Profile Posts

ScreenShot_6_28_2022_3_2_40.png

There's 7 main areas in the game that are all connected. This train get's you from an underground lab to the center of someone's subconscious. inside the train you can talk to some of the passengers. but going to the top of it, there's battles.
Speedrunning all character portraits just so they all have the same quality. Not sure if this was a good idea, but now I'm almost done.
We now have a gameplay video!
DrBuni wrote on Akiritas's profile.
That is a cute Link picture, reminds me of the Samurai Jack artstyle.
I had an idea on how to get my encounters to leash after chasing the player for a little bit. After going down the Javascript rabbit hole, I realized I could do it in a regular movement route. Derp!

Forum statistics

Threads
123,145
Messages
1,154,373
Members
161,489
Latest member
Nordak
Top