mIRC Home    About    Download    Register    News    Help

Print Thread
J
Joe_Dean
Joe_Dean
J
Why can't I do multiple calculations using $calc?

http://pastebin.com/f5251d393

Joined: Oct 2005
Posts: 1,671
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,671
You can.

(If you want a more elaborate answer, provide a more detailed question.)

-genius_at_work

Joined: Dec 2002
Posts: 2,884
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,884
I assume the question you were trying to ask is why don't you get the result you expected. The answer is operator precedence. Mathematical operators (eg. +, -, /, *) have different precedence (priority) which means that in an equation like the last one in your example each operation does not necessarily occur from left-to-right. Precedences for the operators $calc() supports are as follows (from highest to lowest):
Code:
Operator | Description
----------------------
    ^    | Exponentiation (power of)
 *, /, % | Multiplication, Division, Modulus
  +, -   | Addition, Subtraction


So to use your example again -- 1194 - 1 / 1679 * 100 -- because / and * have higher precedence than - they are performed first and then the result is subtracted from 1194 giving you 1193.940441.

You can force operations to be performed in a certain order by using parentheses. For example (1194 - 1) / 1679 * 100 would give you the answer you expected

Joined: Jan 2007
Posts: 1,155
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,155
lol Genius, let's just assume his question is "How can I perform multiple calculations with $calc?"

The answer is surprisingly found in the mIRC help file! Go figure ....

Quote:
$calc(operations)

Returns the result of the specified operations. This identifiers allows you to perform multiple operations easily. For example:



$calc(3.14159 * (2 ^ %x % 3) - ($ticks / (10000 + 1)))


J
Joe_Dean
Joe_Dean
J
Ah of course, thanks ;P


Link Copied to Clipboard