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. The accuracy of results is limited to that of the 53-bit 'doubles' range, including the bits representing any fraction. The results are returned with any fraction rounded to the 6th decimal place, with the period or any zeroes-within-the-fraction stripped from being the rightmost digit of the output.
+
{{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 9: Line 9:
  
 
== Operators ==
 
== Operators ==
'''The TLDR version of most of this is: Use plenty of parenthesis, which improves readability as well as ensuring the operations are performed in the order you wish.'''
+
When parenthesis are not used to override the order of evaluation, these are the operators and the order in which they are used in calculating the result:
  
When parenthesis are not used to override the order of evaluation, these are the operators and the order in which they are used in calculating the result:<br />
+
<span style="display: inline-block; width: 30px;">'''^'''</span>1. Exponent
  
<span style="display: inline-block; width: 30px;">'''()'''</span>1. Parenthesis
+
<span style="display: inline-block; width: 30px;">'''//'''</span>2. Floor division, same as $floor($calc(X / Y))
  
<span style="display: inline-block; width: 30px;">'''^'''</span>2. Exponent
+
<span style="display: inline-block; width: 30px;">'''%'''</span>3. Modulus (remainder when dividing X / Y )
  
<span style="display: inline-block; width: 30px;">'''//'''</span>3. Floor division, X // Y same as $floor($calc(X / Y)) (Added v7.41)
+
<span style="display: inline-block; width: 30px;">'''*'''</span>4. Multiplication
  
<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>4. Division
  
<span style="display: inline-block; width: 30px;">'''*'''</span>3. Multiplication
+
<span style="display: inline-block; width: 30px;">'''+'''</span>5. Addition
  
<span style="display: inline-block; width: 30px;">'''/'''</span>3. Division
+
<span style="display: inline-block; width: 30px;">'''-'''</span>5. Subtraction
  
<span style="display: inline-block; width: 30px;">'''+'''</span>4. Addition
+
'''Parenthesis used to demonstrate the order:'''
 
+
<pre>//echo -a $calc( 99 // 4 ^ .5 % 11 * 2 + 1) is same as $calc( ((( 99 // (4 ^ .5))  % 11) * 2) + 1)</pre>
<span style="display: inline-block; width: 30px;">'''-'''</span>4. Subtraction
 
 
 
 
 
'''Parenthesis used to demonstrate the order. Operations at the same level are executed left-to-right:'''
 
<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 and that result of '2' is then cubed.
 
Then it splits the formula into separate groups delimited by either + add or - subtract.
 
Then the level#3 operations within each group are performed left-to-right.
 
Finally the + and - groups groups are added/subtracted together from left-to-right order.
 
</pre>
 
 
== Examples ==
 
== Examples ==
 
'''Echo a simple addition calculation to the active window'''
 
'''Echo a simple addition calculation to the active window'''
Line 64: Line 54:
  
 
<source lang="mIRC">
 
<source lang="mIRC">
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.
+
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
  
Order of Operation PEMDAS means Multiplication and Division and Floor Divide and Modulo are applied at the same time, as do Addition and subtraction together. It does not mean doing all multiplications before any divisions, nor doing all additions before doing any subtractions.
+
Order of Operation PEMDAS means Multiplication and Division are applied at the same time, as do Addition and subtraction together. It does not mean doing all multiplications before any divisions, nor doing all additions before doing any subtractions.
  
 
//echo -a $calc( 2 / 4 * 5 )
 
//echo -a $calc( 2 / 4 * 5 )
Line 75: Line 65:
 
yields 4 because it goes left-to-right performing adds and subtracts. It subtracts 3 from 2 = -1, then adds 5.
 
yields 4 because it goes left-to-right performing adds and subtracts. It subtracts 3 from 2 = -1, then adds 5.
  
//echo -a $calc( 0-2^8*3-1 ) vs $calc( -2^8*3-1 ) calc like $calc( (-2)^8*3-1 )
+
//echo -a $calc( -2^8*3-1 )
 +
//echo -a $calc( 0-2^8*3-1 )
 
produce 2 different answers because the presence of the 0 changes the meaning of the hyphen. In the 1st example it's -2 to the 8th power which is +256. That's then multiplied by 3, before having 1 subtracted from it. In the 2nd example, it starts by taking 2 to the 8th power, then multiplying by 3 to obtain the 2nd term 768. Then it starts with zero, subtracts the 768, then subtracts another 1.
 
produce 2 different answers because the presence of the 0 changes the meaning of the hyphen. In the 1st example it's -2 to the 8th power which is +256. That's then multiplied by 3, before having 1 subtracted from it. In the 2nd example, it starts by taking 2 to the 8th power, then multiplying by 3 to obtain the 2nd term 768. Then it starts with zero, subtracts the 768, then subtracts another 1.
 
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
 
 
</source>
 
</source>
  
Line 90: Line 78:
 
//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 smaller numbers. This inaccuracy is often reflected in other identifiers which use same stored values used by $calc.
+
$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
 
Even numbers much smaller than 2^53 can return inaccurate results in their fraction, if the number to the left of the decimal is at least as large as 2^33:
 
 
//echo -a $calc( 8589934592 + .999999) => 8589934592.999998
 
  
 
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
  
The 'doubles' math also applies to the if() statement, where it can accurately discern whether 2^53 is greater than 2^53-1, but considers 2^53 to be the same as 1+2^53.<br/>
+
$calc limits output to 6 decimals, with trailing zeroes dropped.
 
 
$calc rounds output to 6 decimals, with trailing zeroes dropped.
 
 
//echo -a $pi vs $calc($pi)
 
//echo -a $pi vs $calc($pi)
 
It can operate on terms who have more than 6 places, but the value with more than 6 decimal places is then rounded to 6:
 
//var %x 0.1111111 , %y 6 |  echo -a $calc(%x * %y)) vs $calc($calc(%x) * %y) is 0.666667 vs 0.666666
 
  
 
//echo -a $calc( 123 + 1abc + 2def )
 
//echo -a $calc( 123 + 1abc + 2def )
Line 123: Line 102:
 
//echo -a $calc( 5 ^ $+ + .1)
 
//echo -a $calc( 5 ^ $+ + .1)
  
When the $+ is inside additional parenthesis, the entire result is zero:
+
When the $+ is inside additional parenthesis, the entire result is zero. However when $+ is inside a variable, it is handled correctly:
//echo -a s $calc( 123 + ( 1 $+ 23 ) )
+
//var %a 123 + ( 1 $+ 23 ) | echo -a $calc( %a ) vs $calc( 123 + ( 1 $+ 23 ) )
 
+
</source>
You should use $+() instead of $+ inside the $calc string.
 
//var %a 7 | echo -a $calc( %a $+ 000 ) vs $calc(%a * 1000) vs $calc( $+(7,000) )
 
result: 7 vs 7000 vs 7000
 
 
 
Because of the rounding of $calc output, it's not always accurate to use the $calc output as the argument inside $int() or $floor.
 
  
//var %i 0 | while (%i isnum 0-99) { echo -a %i : $calc( $+(%i,.9999995) ) | inc %i }
 
In the above example, sometimes the result is rounded up to the next integer, and other times it's rounded down to having the .999999 fraction.
 
//var -s %top 2000000 , %div %top + 1 | echo -a $calc(9 + (%top / %div) ) vs $calc(9 + (%top // %div) ) vs $calc(9 + (%top - (%top % %div)) / %div)
 
result: 10 vs 9 vs 9
 
In this case, the first result rounded to the nearest 9 decimal places is 10.000000, so the fraction and the decimal were stripped. The input for $int would have been 10, so the wrong result would not be caused by $int. The accuracy of the 'floor divide' operator provides the accurate result because the floor divide happens before the result is rounded to 6 decimal places. The last method is a way to return accurate results without using the floor divide.
 
 
</source>
 
 
See also the $calc section at {{mIRC|msl_injection#$calc()}}
 
See also the $calc section at {{mIRC|msl_injection#$calc()}}
 
+
== Compatability ==
== Compatibility ==
 
 
{{mIRC compatibility|5.1}}
 
{{mIRC compatibility|5.1}}
 
 
== See Also ==
 
== See Also ==
 
* {{mIRC|$base}}
 
* {{mIRC|$base}}

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)