From WikiChip
Editing mirc/identifiers/$calc
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | {{mirc title|$calc Identifier}}'''$calc''' can be used to perform mathematical calculations from the mIRC editbox line, or from inside of a custom script | + | {{mirc title|$calc Identifier}}'''$calc''' can be used to perform mathematical calculations from the mIRC editbox line, or from inside of a custom script. |
== Synopsis == | == Synopsis == | ||
<pre>$calc(operations)</pre> | <pre>$calc(operations)</pre> | ||
Line 17: | Line 17: | ||
<span style="display: inline-block; width: 30px;">'''^'''</span>2. Exponent | <span style="display: inline-block; width: 30px;">'''^'''</span>2. Exponent | ||
− | <span style="display: inline-block; width: 30px;">'''//'''</span>3. Floor division, X // Y same as $floor($calc(X / Y) | + | <span style="display: inline-block; width: 30px;">'''//'''</span>3. Floor division, X // Y same as $floor($calc(X / Y)) |
<span style="display: inline-block; width: 30px;">'''%'''</span>3. Modulus X % Y is remainder when dividing X / Y | <span style="display: inline-block; width: 30px;">'''%'''</span>3. Modulus X % Y is remainder when dividing X / Y | ||
Line 30: | Line 30: | ||
− | '''Parenthesis used to demonstrate the order. Operations at the same level are executed | + | '''Parenthesis used to demonstrate the order. Operations at the same level are executed right-to-left:''' |
<pre>//echo -a $calc( 99 - 77 * 2 // 4 ^ .5 ^ 3 + 11 % 3 * 2 + 1) is same as $calc( 99 - (((( 77 * 2) // ((4 ^ .5) ^3)))) + (( 11 % 3) * 2) + 1) | <pre>//echo -a $calc( 99 - 77 * 2 // 4 ^ .5 ^ 3 + 11 % 3 * 2 + 1) is same as $calc( 99 - (((( 77 * 2) // ((4 ^ .5) ^3)))) + (( 11 % 3) * 2) + 1) | ||
− | This first performs exponentiation left-to-right, so 4^.5^3 is 4 raised to .5 | + | This first performs exponentiation left-to-right, so 4^.5^3 is 4 raised to .5 before being cubed. |
Then it splits the formula into separate groups delimited by either + add or - subtract. | Then it splits the formula into separate groups delimited by either + add or - subtract. | ||
− | Then the | + | Then the operations within each group are performed left-to-right. |
− | Finally the | + | Finally the groups are added/subtracted together. |
</pre> | </pre> | ||
== Examples == | == Examples == | ||
Line 64: | Line 64: | ||
<source lang="mIRC"> | <source lang="mIRC"> | ||
− | Modulus of N1/N2 is often seen as a number 0 through | + | Modulus of N1/N2 is often seen as a number 0 through N2 less 1, but that applies only to integers. It's the remainder (not the fraction) when dividing the numerator by the denominator. |
//var %x -10 , %y 3.14 | echo -a $calc(%x - %y * $int($calc( %x / %y))) vs $calc(%x % %y) | //var %x -10 , %y 3.14 | echo -a $calc(%x - %y * $int($calc( %x / %y))) vs $calc(%x % %y) | ||
* Shows -0.58 vs -0.58 | * Shows -0.58 vs -0.58 | ||
Line 80: | Line 80: | ||
Any formula where any term reaches the undefined value returns zero for the entire result, and does not simply treating the individual operation's result as zero then continue operating. | Any formula where any term reaches the undefined value returns zero for the entire result, and does not simply treating the individual operation's result as zero then continue operating. | ||
//echo -a $calc( 2 + ((-1) ^ .5) + 2 ) or $calc(2 + (1/0) + 2 ) are both zero | //echo -a $calc( 2 + ((-1) ^ .5) + 2 ) or $calc(2 + (1/0) + 2 ) are both zero | ||
+ | |||
</source> | </source> | ||
Line 90: | Line 91: | ||
//echo -a $calc(9007199254740993) vs $calc(900719925474099.3) returns 9007199254740992 vs 900719925474099.25 | //echo -a $calc(9007199254740993) vs $calc(900719925474099.3) returns 9007199254740992 vs 900719925474099.25 | ||
− | $calc has reduced accuracy for larger integers, and even lower accuracy for fractions of | + | $calc has reduced accuracy for larger integers, and even lower accuracy for fractions of lower numbers. This inaccuracy is often reflected in other identifiers which use same stored values used by $calc. |
For example, $base can't be trusted to be accurate when handling values larger than 2^53. | For example, $base can't be trusted to be accurate when handling values larger than 2^53. | ||
//echo -a $base($str(f,14),16,16) returns 100000000000000 | //echo -a $base($str(f,14),16,16) returns 100000000000000 | ||
− | |||
− | |||
− | |||
− | |||
if() can sometimes return inaccurate results for values larger than 2^53. For example, this executes as if $true: | if() can sometimes return inaccurate results for values larger than 2^53. For example, this executes as if $true: | ||
//if (18014398509481984 == 18014398509481985) echo match | //if (18014398509481984 == 18014398509481985) echo match | ||
− | |||
− | |||
$calc rounds output to 6 decimals, with trailing zeroes dropped. | $calc rounds output to 6 decimals, with trailing zeroes dropped. | ||
Line 125: | Line 120: | ||
When the $+ is inside additional parenthesis, the entire result is zero: | When the $+ is inside additional parenthesis, the entire result is zero: | ||
//echo -a s $calc( 123 + ( 1 $+ 23 ) ) | //echo -a s $calc( 123 + ( 1 $+ 23 ) ) | ||
+ | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
See also the $calc section at {{mIRC|msl_injection#$calc()}} | See also the $calc section at {{mIRC|msl_injection#$calc()}} | ||