Jump to content

Arithmetic operator


Stremik

Recommended Posts

This should be an easy one.

It's actually kinda embarrassing because what I'm going to ask

is probably something that people learn it 5th grade.

Well. Guess I flanked out on that day. :)

Anyway. I don't fully understand how do "exponent" and "modulus" work.

Especially exponent. For instance: example from Expression Language PDF.

2 * 3^2 + 4 * 6 / 2 = 48 (that is: 36+12)

I don't understand how 2*3^2 evaluates to 36.

Anyone care to explain?

Thank you!

Vladimir

Link to comment
Share on other sites

The order of execution should be

"BOMDAS"

Brackets, Operators, Multiplication, Division, Add, Subtract

Better put would be

First (brackets) , then second (operators), third(multiplication, division) , last (add, subtract)

But this seems to be behaving as if

First (brackets) , then second (operators, multiplication, division) , last (add, subtract)

I can

Link to comment
Share on other sites

Hello Stremik,

Anyway. I don't fully understand how do "exponent" and "modulus" work.

Especially exponent. For instance: example from Expression Language PDF.

I too had to re-learn elementry math. I just bought one of those math books for dummies. Exponents is known as power, so 6^2 means "six to the second power" or 6 * 6 = 36.

6^3 means "six to the third power" or 6*6*6 = 216. You get the picture.

When a number is raised to the second power (x^2), it is squared.

Example: 6^2 can be written/said as "six squared."

When a number is raised to the third power (x^3), it is cubed.

Example: 6^3 can be written/said as "six cubed."

I don't know if this is relevant to 3d but that's what my physics book says ;)

Law of Exponents tutorial:

http://oakroadsystems.com/math/expolaws.htm

Modulus is still confusing to me. I "think" it's the remainder of division operation.

For example: 5 % 3 = 2. Because 5 /3 = 1 with 2 remainder, the modulus will just give you the remainder. Again this is what "I think." I hope somebody correct me if I am wrong.

Good luck!

Perry

Link to comment
Share on other sites

This comes from the link renderpipe sent out, and it's what I learned in school. Houdini however doesn't follow this rule, it seems.

One warning: Remember the order of operations. Exponents are the

first operation (in the absence of grouping symbols like parentheses),

so the exponent applies

only to what it's directly attached to.

3x

Link to comment
Share on other sites

Houdini however doesn't follow this rule, it seems.

Yes, algebraically speaking, the behaviour of the expression language is *not* correct when applying the exponent. The reference is.

The exponent should have the same precedence as the unary minus operator - i.e: immediately after grouping ops (like parentheses). But oh well... :P

VEX doesn't suffer from this because it doesn't have an "exponent" operator, and precedence, in general, is the same as C++. But it does have a set of "gotchas" relating to automatic type promotion.

I'd say that, as a rule of thumb, never use the "^" operator in the expression language. Instead, use the power function pow(). So the sample expression becomes 2 * pow(3,2) + 4 * 6 / 2

And it doesn't hurt to over-parenthesize either. For example; you may think that mult (*) has higher precedence than div (/) and think that the following results in 0.5

2 / 2*2

... when in reality, the result is 2. So always write it as 2 / (2*2). It won't hurt performance to have too many parentheses.

All languages have their little ideosyncrasies though. ;)

Link to comment
Share on other sites

Yes, algebraically speaking, the behaviour of the expression language is *not* correct when applying the exponent.

This really threw me off a while back. Kind of not what I expect from a package like houdini either. :rolleyes:

...

Actually, to get 36 the first part of the expression would have to be pow(2*3,2)

It won't hurt performance to have too many parentheses.

That's good to know :P

Link to comment
Share on other sites

Boy! The docs are really screwed up.

Looks like here:

It is given as:

1. ( )                          Parentheses
2. -                           Negation
3. * / % ^                   Mult, Div, Mod, Pow
4. + -                         Add, Sub
5. < > == != || && !       Logical ops

And operators at the same level (as well as the expression as a whole) get evaluated from left to right (which is the final form of precedence).

the order of precedence actually given in a fashion similar to Arabic languages.

At least it's true for mod and pow

Mario, thanks again! From now on I'll do as you suggest and check the stuff

in texport first.

Say. Is there an expression for Modulus like there is one for Exponent?

I looked and didn't find anything.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...