$calc » History » Version 7
Per Amundsen, 12/14/2019 08:14 AM
1 | 1 | Per Amundsen | _Added in 1.8.10_ |
---|---|---|---|
2 | |||
3 | *$calc(operations)* |
||
4 | |||
5 | Returns the result of the specified operations. You can perform multiple operations. |
||
6 | |||
7 | 4 | Per Amundsen | [[Arithmetic Operators]]. |
8 | 2 | Per Amundsen | |
9 | Operations can be grouped with parentheses. |
||
10 | 1 | Per Amundsen | |
11 | *Parameters* |
||
12 | |||
13 | operations - The calculations to perform. |
||
14 | |||
15 | 6 | Paul Janson | Note: $calc uses the left-to-right @PE(MD)(AS)@ order of operations, but also includes Floor Divide and Modulo. When parenthesis are not used, operators are evaluated in left-to-right order within each of these levels 1-4. All level N operations are performed left-to-right before any level N+1 operations are performed. |
16 | <pre> |
||
17 | () 1. Parenthesis |
||
18 | |||
19 | ^ 2. Exponent (dont' use 96 or greater) |
||
20 | |||
21 | // 3. Floor division, X // Y same as $floor($calc(X / Y)) |
||
22 | % 3. Modulus X % Y is remainder when dividing X / Y |
||
23 | * 3. Multiplication |
||
24 | / 3. Division |
||
25 | |||
26 | + 4. Addition |
||
27 | - 4. Subtraction |
||
28 | </pre> |
||
29 | 7 | Per Amundsen | *(AdiIRC only)* = $calc is accurate to any integer value up within the range [1-2^96,-1+2^96], but returns result of 0 exponent is 96 or greater. If result of multiply or add is >= 2^96, that intermediate operation has the result of zero. Accuracy of the fraction to the right of the decimal is limited by the size of the number to the left. |
30 | 6 | Paul Janson | |
31 | If needing integer math for values 2^96 and above, see [[$calcint]]. |
||
32 | |||
33 | 1 | Per Amundsen | *Example* |
34 | |||
35 | <pre> |
||
36 | 5 | Per Amundsen | ; Returns 6. |
37 | 1 | Per Amundsen | //echo -ag $calc((4 + 4) - 2) |
38 | 6 | Paul Janson | </pre> |
39 | |||
40 | <pre> |
||
41 | Accuracy above 2^53: //echo -a $calc(2^53 + 1) currectly returns an ODD number and so does $calc(2^95+1) |
||
42 | |||
43 | ; next is 0 because, even though the result is in the valid range, the 2^96 operation has undefined result |
||
44 | //echo -a $calc(2^96-1) |
||
45 | |||
46 | ; result is 7 because the multiply is earlier priority than the addition |
||
47 | //echo -a $calc( 4 + 1 * 3) |
||
48 | |||
49 | ; result is 15 because the parenthesis altered the order of operations |
||
50 | //echo -a $calc( (4 + 1) * 3) |
||
51 | |||
52 | ; While AdiIRC offers many fraction digits, it's up to you to decide how many of the least you wish to keep. |
||
53 | //echo -a $calc( (3/7) *70 ) |
||
54 | result = 30.000000000000000000000000002 |
||
55 | |||
56 | alias calc6 return $round($calc($parms),6) |
||
57 | |||
58 | You can use [[$round]] to ensure the result is rounded to 6 decimal places or to the level you prefer. |
||
59 | //var -p %a (3/7) *70 | echo -a $round($calc( %a ),6) |
||
60 | 1 | Per Amundsen | </pre> |