Math Functions

By Jonesy44 on Jun 07, 2008

I hope there is no one liner $regex's for these, or knowing my luck some of these are prolly built into mirc >.>

****: The numbers must be in numerical order from 0 - whatever. for now. i will add an auto arrange soon :P

;; If a given number is even, returns $true ;;
alias iseven {
  if ($1 isnum) {
    if (. !isin $calc($1 / 2)) {
      return $true
    }
    else { 
      return $false
    }
  }
}

;; If a given number is odd, returns $true ;;
alias isodd {
  if ($1 isnum) {
    if (. !isin $calc($1 / 2)) {
      return $true
    }
    else { 
      return $false
    }
  }
}

;; Returns the average of the values supplied ;;
alias avg {
  if ($1) {
    var %x = 1, %avg = 0
    while (%x <= $0) {
      if ($gettok($1-,%x,32) isnum) {
        var %avg = $calc(%avg + $gettok($1-,%x,32))
        inc %x 1
      }
      else {
        echo -a 4Error: ($avg): $gettok($1-,%x,32) is not a number
        halt
      }
    }
    return $calc(%avg / $0)
  }
}

;; returns the median (middle number when layed out in row **** ;;
alias median {
  if ($1) {
    if ($iseven($0)) {
      var %res = $calc(($gettok($1-,$calc($0 / 2),32) + $gettok($1-,$calc(($0 / 2) + 1),32)) / 2)
    }
    else {
      var %res = $gettok($1-,$calc(($0 / 2) + 0.5),32)
    }
    return %res
  }
}

;; returns the mode of the numbers given (the one(s) that occur most often)
alias mde {
  if ($1) {
    write -c modetemp.dat
    write -c modetempcalc.dat
    var %x = 1
    while (%x <= $0) {
      if ($read(modetemp.dat, s, $gettok($1-,%x,32))) {
        var %p = $read(modetemp.dat, s, $gettok($1-,%x,32))
        write -ds $+ $gettok($1-,%x,32) modetemp.dat
        write modetemp.dat $gettok($1-,%x,32) $calc(%p + 1)
      }
      else {
        write modetemp.dat $gettok($1-,%x,32) 1
      }
      inc %x 1
    }
    var %x = 2, %xy1 = $gettok($read(modetemp.dat,1),1,32), %xy2 = $gettok($read(modetemp.dat,1),2,32)
    while (%x <= $lines(modetemp.dat)) {
      if ($gettok($read(modetemp.dat,%x),2,32) > %xy2) {
        var %xy1 = $gettok($read(modetemp.dat,%x),1,32), %xy2 = $gettok($read(modetemp.dat,%x),2,32)
      }
      elseif ($gettok($read(modetemp.dat,%x),2,32) == %xy2) {
        write modetempcalc.dat $gettok($read(modetemp.dat,%x),1,32) $gettok($read(modetemp.dat,%x),2,32)
      }
      inc %x 1
    }
    write modetempcalc.dat %xy1 %xy2
    echo -a Mode: $gettok($read(modetempcalc.dat,$lines(modetempcalc.dat)),1,32) occured $gettok($read(modetempcalc.dat,$lines(modetempcalc.dat)),2,32) times
    var %x = 1
    while (%x <= $calc($lines(modetempcalc.dat) - 1)) {
      echo -a Mode: $gettok($read(modetempcalc.dat,%x),1,32) occured $gettok($read(modetempcalc.dat,%x),2,32) times
      inc %x
    }
    return Mode Calculation complete
    .remove modetemp.dat
    .remove modetempcalc.dat
  }
}

Comments

Sign in to comment.
Kirby   -  Apr 29, 2009

Whatever works jonesy. :D

 Respond  
Jonesy44   -  Apr 29, 2009

an even number can be divided by two to resolve a full number, that is an integer, with no decimal.

Thanks, perhaps i should just use a $prop or something?

 Respond  
Kirby   -  Apr 27, 2009

Old script jonesy44, I know, but I would like to say something about the isodd and iseven functions.
I don't think checking to see if there is a decimal or not in the script, because 24.42, for example, is even but has a decimal point in it. Not to mention, both of the isodd and iseven functions are exactly the same.
Good identifiers though.

 Respond  
Jonesy44   -  Jun 11, 2008

That is true ! lol,

i was thinking in PHP terms at the time lol. which doesn\'t support the efficient methods that mirc does.

Thank you Spartacus

Although, im not sure i get the N & !?

 Respond  
Spartacus   -  Jun 11, 2008

As for even/odd:

alias iseven { if (N & 1) return $false | else return $true }

average:

alias avg { if ($remove($1-,$chr(32)) isnum) return $calc(($replace($1-,$chr(32),+)) / $0) }

Just some cool, efficient ideas I thought I\'d share with you.

 Respond  
Jonesy44   -  Jun 08, 2008

wtf Eugenio?

 Respond  
Jonesy44   -  Jun 08, 2008

-.-

Helps me :p

 Respond  
Eugenio   -  Jun 08, 2008

lmao failz for me ....
/me rates 3

 Respond  
Jonesy44   -  Jun 08, 2008

it says what each function does above each alias lol

 Respond  
Eugenio   -  Jun 08, 2008

What the bloody hell does this do ?!?

 Respond  
Jonesy44   -  Jun 08, 2008

Hehe, that would be real easy, but okay :P

 Respond  
^Neptune   -  Jun 08, 2008

Scratch that, just the range lol

 Respond  
^Neptune   -  Jun 08, 2008

This looks really impressive, will have to try sometime. Maybe make a mode and range function :)

 Respond  
Jonesy44   -  Jun 08, 2008

:[ no reviews :P

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.