Jump to content

QuickCalc [Updated Apr 17]


Recommended Posts

An alternative calculator for Alfred 2, supporting custom functions and variables, automatic parentheses matching, and percentages. It also supports k, m, and b (or thousand, million, and billion) as suffixes on a number.

It's essentially a wrapper around bc with a few extras added in.
quickcalc.png
Installation

Simply download the workflow and open the file. You'll need to have the Powerpack to install workflows.

Default functions

I've tried to add support for most of the functions from Alfred's advanced calculator.

Supported functions: sin, cos, tan, log, log2, ln, exp, abs, sqrt, asin, acos, atan, sinh, cosh,tanh, asinh, acosh, atanh, ceil, floor, round, trunc, rint, near, dtor, rtod, pow, logx, mod, min, max.

Most of these should be similar to the implementation of Alfred's advanced calculator, but there are a few additions:

  • pow(x, y) can be used to raise x to the power of y, without the integer limitations of the ^ operator in bc.
  • logx(base, x) lets you get the log of a number with a defined base (log calculates with a base of 10 and log2 with a base of 2).
  • mod(x, y) performs the modulo operation.
  • dtor(d) and rtod® converts between degrees and radians (this is part of Alfred's calculator, but I'm documenting them here since googling them doesn't turn up useful results).

Defining custom functions and variables

After using the workflow at least once, you can find the custom functions/variables file at "~/Library/Application Support/Alfred 2/Workflow Data/com.clintonstrong.QuickCalc/custom.txt"

Here's an example custom.txt file:

define f2c(q) { return (q - 32) / 1.8}define c2f(q) { return 1.8 * q + 32}tax = 8.25%vat = 20%

In this case, I just defined some functions to convert between celsius and fahrenheit, and set up some variables to use in calculations. It uses the syntax of GNU bc.

Known bugs and limitations

When using functions that take multiple arguments, use both a comma and a space to separate the arugments. This is necessary since commas and spaces can be used as thousands separators. For example, use min(5, 10) instead of min(5,10).

Because percent signs % are used for percentages, you'll need to use the mod function for modulo. For example: mod(10, 3)evaluates to 1.

Due to limitations with bc, the exponentiation operator (^ or **) doesn't allow numbers to be raised to the power of a float (a number with digits after the decimal place). To get around this, you can use the pow function. For example, pow(2, 2.5) evaluates to 5.6568.

Percentages don't work as expected within functions. They still get converted (by dividing by 100), but it doesn't work correctly with addition and subtraction (100 + 10% evaluates to 100.10 rather than 110). Percentages should still work fine outside of functions.

The workflow currently doesn't have support for detecting locales. It expects a period . to be used as a decimal point, with commas, underscores, or spaces as optional thousands separators.

More info

 

Changelog

 

  • Apr 17, 2013: Added 'x' for multiplication. Bugfixes.
  • Fix for Ruby 2.0.0/Mavericks.
Edited by Clinton Strong
Link to comment

Very cool. I was actually going to make something like this myself, but I'm glad I don't have to now!

 

Some cool things I'd like to see added (although not sure how feasible they are in Alfred):

 

1. Matrix manipulation

2. Calculus solver

 

Also, how come you didn't make its keyword '=' since it's basically replacing the regular calculator?

Link to comment

Very cool. I was actually going to make something like this myself, but I'm glad I don't have to now!

 

Some cool things I'd like to see added (although not sure how feasible they are in Alfred):

 

1. Matrix manipulation

2. Calculus solver

 

Also, how come you didn't make its keyword '=' since it's basically replacing the regular calculator?

 

I personally use '=', but I didn't think it was a good default. I thought it might confusing if you typed '=' and got both the built-in calculator and the workflow's calculator at the same time. You can always change it though.

 

Since it's essentially just a frontend for bc, I doubt it'll get too many advanced features. I'm still looking in to using (or perhaps creating) another parser, so it could be a possibility in the future. For now I'd probably just go with WolframAlpha for those kind of calculations.

Link to comment
  • 1 month later...
  • 5 months later...

Thanks for the prompt response! this is a great workflow, thanks for the work you've put in!

 

Do you know whats causing workflows that worked in the past to present this issue lately?

 

Mavericks ships with an updated version of Ruby, which has some compatibility issues with older versions. The main issues are with the load path (you have to either manually add the workflow directory to the load path or explicitly use the full path of a file to load it), and with character encoding.
 
If you want a quick fix, you could probably get some of the workflows to run in 1.8.7, the version Mountain Lion was using. It's located at "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/". If you need any help with that, just send me a message.
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...