mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Trying to write an alias where if I type 2 numbers and the script does a $calc($1 / $2), and if the remainder is 0, stops there, if not, then it keeps dividing and keeps tracks of the remainder. This means if the answer has a decimal, like N.25, it has a remainder, which would be 4. Then at the end, it collects all the remainders, and echo's the order of remainders in reverse..

So doing something like /convert 11 2

11 / 2 = 5.5

0.5 * $2 = 1 (remainder 1)

5 / 2 = 2.5

.5 * $2 = 1 (remainder 2)

2 / 2 = 1 (remainder 3 is 0)

No decimal.

1 / 2 = 0.5 (remainder 4 is 1)

Stops at 0..

So the remainders are 1 1 0 1. Read them backwards, and you get, 1011.

Anyone think they got a shot?

The 1 I made doesn't while loop, it has to stop somewhere, unless the code goes forever...

Thanks.

-Neal.

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
You're trying to do a binary conversion?

Use $base() already! smile

Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Well as Bekar said, $base() will do this for you. But if you just want to know how or intend to do something else that prevents you using $base() here's the code anyway:
Code:
convert {
  var %input = $1, %base = $2, %r
  while %input {
    %r = $+($calc(%input % %base),%r)
    %input = $int($calc(%input / %base))
  }
  return %r
}


In particular you might want to note the use of the modulus operator (%) which returns the remainder of division between two numbers


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Okay thank you.

If anyone cared to know I had something completely ridiculous.

My failed attempt.

/convert <number to convert> <base to convert from> <base to convert to>...

Code:
convert {
  if ($2 > $3) {
    echo -a So $2 is bigger than $3 $+ , converting $1 in base $2 to base $3 $+ .
    if (. isin $calc($1 / $3)) {
      var %digit = $gettok($calc($1 / $3),1,46)
      var %remainder = . $+ $gettok($calc($1 / $3),2,46)
      %remainder = $calc(%remainder * $3)
      set %n 1
      while (%digit > 0) {
        var %digit = $gettok($calc(%digit / $3),1,46)
        inc %n 
        var $eval($+(%,remainder,%n)) = . $+ $gettok($calc(%digit / $3),2,46) 
        var $eval($+(%,remainder,%n)) = $calc($eval($+(%,remainder,%n),2) * $3)
        var %remainder = $addtok(%remainder,$eval($+(%,remainder,%n),2),32)
      }
    }
    if (. !isin $calc($1 / $3)) {
      var %remainder = 0
    }
  }
  echo -a %remainder
  ;Then echo -a $wordreverse(%remainder)
}


I got into this whole $eval() mess for having %remainder1 %remainder2, etc., all the way to however many. Clearly the amount of remainders will go as however long this code is, so clearly I needed to while loop.

Basically I just tried to make %remainder = %remainder1 %remainder2 etc.

I believe this code only stops at 2 remainders and I was having some $eval() problems before hand.

-Neal.

Last edited by LostShadow; 30/09/07 06:06 PM.

Link Copied to Clipboard