Complex Calculator

By Fumph on Sep 08, 2013

I've been working on this nice little calculator here, it's got some functions a simple $calc($2-) doesn't do! I'd like some feedback, how to improve, any features that might be nice, or anything that looks like a bad practice!

Thanks all

if (($1 == !calc || $1 == !calculate) && $2) {
      var %calc.in $regsubex($remove($replace($2-,ans,%calc.out,pi,3.141592),$chr(44),$chr(32)),/(\d+)([kmbt])/g,$calc(\1 * 1000^ $pos(kmbt,\2)))
      set %calc.out $bytes($calc($regsubex(%calc.in,/[^\d.+-^*/()kmbt]/g,$null)),bd)
      msg # Calculating $qt($regsubex(%calc.in,/[^\d.+-^*/()kmbt]/g,$null)) $+ ... %calc.out 
}

Comments

Sign in to comment.
Sorasyn   -  Sep 09, 2013

Might work on readability of the code, although I'm not sure that's something that can improved since you're using a rather complicated string of regex operations.

However, why are you using a global variable? As it stands, the snippet is using a previous calculated equation in the current execution.

Fumph  -  Sep 10, 2013

%calc.out is the variable containing the previous calculation's answer. That way you can use say, "!calc ans * 5" Does that make sense? I am practicing my regex, as it's something pretty useful! Just by leaving a day from this and coming back I see a few ways to "upgrade" it xD

Yawhatnever  -  Sep 10, 2013

You should make the variable specific to each person, so that using 'ans' won't give you results from somebody else using the script. You may want to unset the variables upon exit with the '-e' switch for cleanliness.

I would also suggest replacing 'pi' with '$pi' rather than '3.141592' which is less precise.

What you posted is not functional without an event. Correct me if I'm wrong, but it appears this conditional would be placed inside a wildcard match text event (which is bad practice). You should instead put the code inside proper matching text event, such as:

on $*:text:/^[!@.]calc(ulate)?\b/Si:#:{
  ;code
}
Fumph  -  Sep 11, 2013

I kept ans available to all as that way anyone can easily get the last answer! I could look into individual answers as well though. -e sounds nice and I'll look into it!

I chose not to use the identifier $pi simply because it contained to many unnecessary digits. That could be completely opinionated, but I didn't think the extra 10+ digits were necessary for an IRC script. Same reason I didn't include sin, cos, and tan.

Sign in to comment

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.