$calc » History » Version 8
Per Amundsen, 02/16/2023 09:13 PM
| 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 | 8 | Per Amundsen | table(ktable). |
| 14 | |*Parameter*|*Description*| |
||
| 15 | | operations | The calculations to perform. | |
||
| 16 | 1 | Per Amundsen | |
| 17 | 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. |
| 18 | <pre> |
||
| 19 | () 1. Parenthesis |
||
| 20 | |||
| 21 | ^ 2. Exponent (dont' use 96 or greater) |
||
| 22 | |||
| 23 | // 3. Floor division, X // Y same as $floor($calc(X / Y)) |
||
| 24 | % 3. Modulus X % Y is remainder when dividing X / Y |
||
| 25 | * 3. Multiplication |
||
| 26 | / 3. Division |
||
| 27 | |||
| 28 | + 4. Addition |
||
| 29 | - 4. Subtraction |
||
| 30 | </pre> |
||
| 31 | 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. |
| 32 | 6 | Paul Janson | |
| 33 | If needing integer math for values 2^96 and above, see [[$calcint]]. |
||
| 34 | |||
| 35 | 1 | Per Amundsen | *Example* |
| 36 | |||
| 37 | <pre> |
||
| 38 | 5 | Per Amundsen | ; Returns 6. |
| 39 | 1 | Per Amundsen | //echo -ag $calc((4 + 4) - 2) |
| 40 | 6 | Paul Janson | </pre> |
| 41 | |||
| 42 | <pre> |
||
| 43 | Accuracy above 2^53: //echo -a $calc(2^53 + 1) currectly returns an ODD number and so does $calc(2^95+1) |
||
| 44 | |||
| 45 | ; next is 0 because, even though the result is in the valid range, the 2^96 operation has undefined result |
||
| 46 | //echo -a $calc(2^96-1) |
||
| 47 | |||
| 48 | ; result is 7 because the multiply is earlier priority than the addition |
||
| 49 | //echo -a $calc( 4 + 1 * 3) |
||
| 50 | |||
| 51 | ; result is 15 because the parenthesis altered the order of operations |
||
| 52 | //echo -a $calc( (4 + 1) * 3) |
||
| 53 | |||
| 54 | ; While AdiIRC offers many fraction digits, it's up to you to decide how many of the least you wish to keep. |
||
| 55 | //echo -a $calc( (3/7) *70 ) |
||
| 56 | result = 30.000000000000000000000000002 |
||
| 57 | |||
| 58 | alias calc6 return $round($calc($parms),6) |
||
| 59 | |||
| 60 | You can use [[$round]] to ensure the result is rounded to 6 decimal places or to the level you prefer. |
||
| 61 | //var -p %a (3/7) *70 | echo -a $round($calc( %a ),6) |
||
| 62 | 1 | Per Amundsen | </pre> |