Mattbuck's Maths function aliases

These functions provide some useful mathematical tools not included in the standard mIRC release.

 

Pre-requisites - Usage - Script

Pre-requisites:

None

 

 

Usage:

To use the script, make sure you have all prerequisites, then copy the script below into a new section of the aliases tab of mIRC script editor.  It provides the following commands (capital letters in parameters represent required fields):

$neg(A) Returns -A
$sign(A) Returns + if A > 0, - if A < 0.
$Hfunc(A) Returns 0 if A = 0, 1 if A > 0, -1 if A < 0.
$quotient(A,B) Returns the integer part of A/B
$mod(A,B) Returns the remainder of A/B
$de(A,B) Returns A-B
$in(A,B) Returns A+B
$fact(A) Returns A!
$nPr(A,B) Returns the number of permutations of choosing B from A items = A!/(A-B)!
$nCr(A,B) Returns the number of unique ways of choosing B from A items = A!/(A!(A-B)!)
$triangle(A) Returns the Ath triangular number = A(A+1)/2
$zint(A) Returns the integer value of A, but returns -0 if -1 < A < 0.
$max(A,b,c,d,...) Returns the maximum value of the parameters.
$min(A,b,c,d,...) Returns the minimum value of the parameters.
$fix(A,B) Returns A to B decimal places.
$fixsf(A,B) Returns A to B significant figures.
$comma(A,B) Returns A with commas inserted every B digits.
$rcomma(A) Returns A with commas removed.
$root2(A) Returns the positive square root of A, with imaginary numbers for negative A.
$diff(A,B,c,d) Returns A-B, with sign included, fixed to c decimal places, with the sign of 0 decided by d (+ or -)
$roundoff(A,B,c,d) Rounds off A to the closest multiple of B, minimum value c, maximum value d.

 

 

Script:

;Maths Functions

;Negative
/neg {
  if ($1 !isnum) { return Error }
  return $calc(-1 * $1)
}

/sign {
  if ($1 !isnum) { return Sign error - non-numeric argument }
  if ($1 > 0) { return + }
  if ($1 < 0) { return - }
}

/Hfunc {
  if ($1 !isnum) { Return HFunc error - non-numeric parameter }
  if ($1 = 0) { return 0 }
  if ($1 < 0) { return -1 }
  return 1
}

/quotient {
  if ($1 !isnum || $2 !isnum || !$2) { return Error }
  return $int($calc($1 / $2))
}

/mod {
  if ($1 !isnum || $2 !isnum || !$2) { return Error }
  return $calc($1 - $2 * $quotient($1,$2))
}

/de {
  if ($1 isnum && $2 isnum) { return $calc($1 - $2) }
  else { return Error }
}

/in {
  if ($1 isnum && $2 isnum) { return $calc($1 + $2) }
  elseif ($1 !isnum) { return Error - non-numeric parameter 1 }
  elseif ($2 !isnum) { return Error - non-numeric parameter 2 }
}

;Factorial Numbers
/fact {
  if ($1 !isnum || $1 < 0) { return Error }
  var %fact = $int($1)
  if (%fact == 1 || %fact == 0) { return 1 }
  var %result 1
  :result
  %result = %fact * %result
  dec %fact 1
  if (%fact == 1) { return %result }
  goto result
}

;n Permutation r
/nPr {
  if ($1 !isnum || $2 !isnum || !$2) { return Error }
  return $calc($fact($1) / ($fact($calc($1 - $2))))
}

;n Choose r
/nCr {
  if ($1 !isnum || $2 !isnum || !$2) { return Error }
  return $calc($fact($1) / ($fact($calc($1 - $2)) * $fact($2)))
}

;Triangular numbers
/triangle {
  if ($1 !isnum || $1 < 0) { return error }
  return $calc(($1 ^ 2 + $1) / 2)
}

/zint {
  if ($1 !isnum) { return error }
  if ($1 >= 0 || $1 <= -1) { return $int($1) }
  else { return -0 }
}

/max {
  var %max = $1, %N = 2
  :max
  if ($ [ $+ [ %N ] ] > %max) { %max = $ [ $+ [ %N ] ] }
  inc %N 1
  if (%N > $0) { return %max }
  goto max
}

/min {
  var %min = $1, %N = 2
  :min
  if ($ [ $+ [ %N ] ] < %min) { %min = $ [ $+ [ %N ] ] }
  inc %N 1
  if (%N > $0) { return %min }
  goto min
}

;Fixed decimal places
/fix {
  if ($1 !isnum || $2 !isnum || $2 < 0) { return Error in fix - non-numeric argument, or DP < 0 }
  if (!$2) { return $int($1) }
  var %stop = $pos($1,$chr(46),1)
  if (%stop == $null) { var %string 0 }
  else { var %string $right($1,$neg(%stop)) }
  if ($len(%string) > $2) { return $round($1,$2) }
  :string
  if ($len(%string) = $2) { return $zint($1) $+ $chr(46) $+ %string }
  else { %string = %string $+ 0 | goto string }
}

;Fixed significant figures
/fixsf {
  if ($1 !isnum) { return Error in fixsf - non-numeric parameter 1 }
  if ($2 !isnum) { return Error in fixsf - non-numeric parameter 2 }
  var %sf = $round($2,0), %len = $len($remove($1,-,.)), %lint = $len($remove($int($1),-))
  if (%sf < 1) { return Error in fixsf - SigFig argument < 1 }
  if (%len = %sf) { return $1 }
  if (%len < %sf && . isin $1) { return $1 $+ $str(0,$de(%sf,%len)) }
  if (%len < %sf && . !isin $1) { return $1 $+ . $+ $str(0,$de(%sf,%len)) }
  if (%lint = %sf) { return $int($1) }
  if (%lint > %sf && - isin $1) { return $round($left($1,$in(%sf,1)) $+ . $+ $mid($1,$in(%sf,2),1),0) $+ $str(0,$de(%lint,%sf))) }
  if (%lint > %sf && - !isin $1) { var %a = $left($1,%sf)) $+ . $+ $mid($1,$in(%sf,1),1), %round = $round(%a,0) | return %round $+ $str(0,$de(%lint,%sf))) }
  var %sf2 = $de(%sf,%lint), %string = $right($1,$neg($len($int($1))))
  return $fix($calc($int($1) $sign($1) $fix(%string,%sf2)),%sf2)
}

;Insert commas in $1 every $2 digits
/comma {
  if ($1 !isnum || $2 !isnum || $2 < 1) { return Error }
  var %order = $int($2)
  var %decimal $pos($1,$chr(46),1)
  if (%decimal == $null) { var %result = $1 }
  else { var %result = $left($1,$calc(%decimal - 1)) | var %append = $right($1,$neg($calc(%decimal - 1))) }
  var %place = $quotient($len($int($1)),%order)
  if ($mod($len($int($1)),%order) = 0) { dec %place 1 }
  :result
  if (%place == 0) { return %result $+ %append }
  var %comma $pos(%result,$chr(44),1)
  if (%comma == $null) { %result = $left(%result,$neg(%order)) $+ $chr(44) $+ $right(%result,%order) }
  else { %result = $left(%result,$calc(%comma - %order - 1)) $+ $chr(44) $+ $right(%result,$calc((%comma - %order - 1) * -1)) }
  dec %place 1
  goto result
}

;Remove commas
/rcomma { return $remove($1-,$chr(44)) }

;Find square root
/root2 {
  if ($1 !isnum) { return Error }
  var %root = $1
  if ($1 < 0) { var %root = $neg(%root) | var %append = i }
  %root = $sqrt(%root) $+ %append
  return %root
}

;Difference $1 - $2 include +/-, fix to $3 dp, 0 specified by $4
/diff {
  var %zero = 0 | var %diff $calc($1 - $2)
  if ($3 == $chr(43) || $3 == $chr(45)) { %zero = $3 $+ 0 }
  elseif ($3 isnum && $3 >= 0) {
    if (%diff = 0) { %zero = $fix(0,$3) }
    elseif (%diff != 0) { %diff = $fix($calc($1 - $2),$int($3)) }
    if ($4 && ($4 == $chr(43) || $4 == $chr(45))) { %zero = $4 $+ %zero }
  }
  if (%diff > 0) { return $chr(43) $+ %diff }
  if (%diff = 0) { return %zero }
  if (%diff < 0) { return %diff }
}

;Round $1 to nearest multiple of $2, max $4, min $3
/roundoff {
  if ($1 !isnum || $2 !isnum) { return Error }
  var %unit = $calc($1 / $2), %unitmax, %unitmin, %rounded
  %rounded = $round(%unit,0)
  if ($3 isnum) {
    %unitmax = $floor($calc($4 / $2))
    if (%rounded > %unitmax) { %rounded = %unitmax }
  }
  if ($4 isnum) {
    %unitmin = $ceil($calc($3 / $2))
    if (%rounded < %unitmin) { %rounded = %unitmin }
  }
  return $calc(%rounded * $2)
}
 


Click to return to the mattbot index.