This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
faubiguy's profile picture

Calculator+

Started by faubiguy, 10 October 2012 - 03:19 AM
faubiguy #1
Posted 10 October 2012 - 05:19 AM
This a text based calculator (Now has buttons too). It will evaluate mathematical expressions you type into it it and give the result. To evaluate an expression, simply type it in to the input box, and the result will be displayed. The calculator follows order of operations, and ignores spaces, so 2+ 7 is equivalent to 2 + 7 and 2+7. To open using the advanced mode use the argument "advanced" when running it.

The default operators are +, -, *, /, ^, % (modulo). It also has several built-in functions, most of the functions from the math library. Functions take a fixed number of arguments, and return the result. The built-in functions are below. Since most of the functions are from the standard lua math library, I'll only give details for the other ones:
Spoiler
  • sin(x)
  • cos(x)
  • tan(x)
  • random(): Random number between 0 and 1; math.random()
  • randomrange(min, max): Random integer between min and max; math.random(min, max)
  • abs(x)
  • acos(x)
  • asin(x)
  • atan(x)
  • atan2(x, y)
  • ceil(x)
  • cosh(x)
  • deg(radians)
  • exp(x)
  • floor(x)
  • fmod(x)
  • frexp(x)
  • ldexp(x, y)
  • log(x): Natural logarithm of x; math.log(x)
  • logten(x): Base 10 logarithm of x; math.log(x, 10)
  • logtwo(x): Base 2 logarithm of x; math.log(x, 2)
  • logbase(x, base): Logarithm of x in base base; math.log(x, base)
  • min(a, b ): Greater of a and b; math.min(a, b )
  • max(a, b ): Greater of a and b; math.max(a, b )
  • pow(x, y)
  • rad(degrees)
  • randomseed(seed)
  • sinh(x)
  • sqrt(x)
  • tanh(x)

You can set variables for temporary storage of expressions. A variable is a sequence of one or more letters. To set a variable, simply use the = operator to assign it. For example: example = 3+5
This will set the variable 'example' to the result of 3+5, which is 8. To use variables in an expression, just use the name. The expression 'example + 3' will now evaluate to 11. To remove a variable (although I'm not sure why you'd want to do that), just type delete variablename into the input box. It has 3 built-in constants as well, pi, e, and inf. These constants are like variables in that you can use them in expressions, but you cannot set their values.

The commands that you can enter into the input box (besides expressions) are:
  • delete <variable>: deletes the given variable
  • vars: list all current variables
  • get <name>: Tells you if name refers to a function, constant, or variable. If it refers to a constant or variable, displays the value.
  • exit: exits the program.
  • buttons: enters button mode.
In addition to the built-in operators, functions, and constants, you can define custom ones in the config file, generated at /Programs/calculator/custom on the first time you run the calculator. Information on how to add custom operators, functions, and constants is given in the file.

You can also use button mode, which is a simple four-function calculator (with order of operations). To enter it, just run the calculator, or use the command "buttons" from the text mode. It doesn't support functions, constants, or variable, and it only has the four basic operators, +, -, *, and /. To use, simply move between buttons with the arrow keys, type in numbers and symbols, or in 1.45 PR1, click the buttons. Pressing enter while a button is selected (with the arrow keys) will press it. The <– button will remove the previous number or symbol, and the adv button will go to the advanced mode. To exit the calculator, just press CTRL.

If you encounter any bugs, report them here, and I'll fix them as soon as I can. Any feedback or suggestions are appreciated.

Link: http://pastebin.com/PJMGB559
Older versions:
Spoilerv1.3: pastebin.com/mySpbdeU
v1.2: pastebin.com/baF9vuXR
Screenshots:
SpoilerText mode:


Button mode:


With color:

Changelog:
Spoilerv1.0:
+ Released

v1.1
* Fixed the calculator identifying expressions as invalid if you use a - after a parentheses.
* Fixed the calculator as incorrectly determining some expressions including nested parentheses as mismatched.

v1.2
+ Added buttons mode.
* Fixed problem with creating custom operators file.
* Fixed error messages about variables displaying for incorrect symbols.

v1.3
+ You can now type in numbers and symbols on buttons mode.
+ In 1.45 PR1, basic colors. Messages are black, errors are red, the background is light gray.
+ In 1.45 PR1, mouse support for buttons mode.
+ Exit buttons mode with CTRL.

v1.3.1
+ Support for mouse in 1.45 PR3+
Exerro #2
Posted 10 October 2012 - 06:12 AM
that looks a lot better than almost all of the other calculators ive seen. ill try it out!
faubiguy #3
Posted 11 October 2012 - 06:33 AM
Updated! Now has buttons mode, which is basically a four function calculator. Right now it doesn't really have any purpose except to make it more like a calculator. Also one or two bug fixes.
tommyroyall #4
Posted 12 October 2012 - 01:01 AM
Could you write up a little guide on how to add more functions or operators? I saw it in the comment, tried and ended up figuring that it might be a little bit easier if you explained this ;)/>/>. I was wondering if I could add a print function to print the text? I'm just seeing the potential in this :P/>/>.
faubiguy #5
Posted 12 October 2012 - 01:10 AM
Could you write up a little guide on how to add more functions or operators? I saw it in the comment, tried and ended up figuring that it might be a little bit easier if you explained this ;)/>/>. I was wondering if I could add a print function to print the text? I'm just seeing the potential in this :P/>/>.

It should be explained in the generated file. If its not, or you think its inadequately explained, then please tell me so I can fix it. I should note that these are mathematical functions, so they get numbers, and are expected to return 1 number. However you could have a side effect such as saving it to a file. A print functions would probably break the display interface though.
faubiguy #6
Posted 12 October 2012 - 07:17 AM
Now with basic colors and mouse support when using 1.45 beta! It will still work fine on other versions.
ficolas #7
Posted 16 October 2012 - 06:53 PM
Awzum :D/>/>

Most of the calculators have +, -, *, and / and thats a crap :/
ficolas #8
Posted 17 October 2012 - 11:36 AM
Did u ask for permisson about including it in ur os? If not, you should :D/>/>
faubiguy #9
Posted 17 October 2012 - 02:35 PM
Did u ask for permisson about including it in ur os? If not, you should :D/>/>

I submitted it to CommuteOS,
Ditto8353 #10
Posted 17 October 2012 - 06:37 PM
You should add Reverse Polish Notation :D/>/>
It is quite simple if you run it off of a stack.
faubiguy #11
Posted 17 October 2012 - 06:56 PM
My program converts the infix to reverse polish internally before evaluating with the stack. As far as I can tell, it would require to be able to determine whether you're using RPN or infix, and evaluate it based on that. Another problem is that my program ignores whitespace, so it can't tell the difference between 3 5 + and 35+, but it probably wouldn't take much effort to add something to my tokenizing function to count numbers separated by whitespace as separate tokens.
Ditto8353 #12
Posted 17 October 2012 - 08:20 PM
I imagine that it would require a RPN mode and Infix mode, selected by the user.
It was really just a random thought I had, more than anything else.
faubiguy #13
Posted 17 October 2012 - 08:37 PM
I wouldn't really take too long to do, but I don't see much of a point in adding an RPN mode when people are more familiar with Infix anyway. What I want to do is add support for different bases (like binary or decimal or hexadecimal), but there would be a collision between letters in variable names and letters in hexadecimal digits (or other bases > 10). If there is a way around this (besides whitespace), I'd like to include it.
Ditto8353 #14
Posted 17 October 2012 - 08:53 PM
Some of the users might object to the change, but you could always change the variable syntax to something like 'var example = 10'
faubiguy #15
Posted 17 October 2012 - 09:02 PM
The problem isn't in the evaluating part, its when it converts the expression into a series of tokens. It would try to separate the hex number 4c13 in to 4, c, 13. I might be able to do it by examining context, but there's still the problem that it couldn't tell whether you were referencing the variable 'd' or the hex value d in the expression: var example = d. So it seems its not practical to include multiple bases. Maybe I'll just have a function: base(<number to convert>, <new base>).