# [ANN] rcalc 2.0 (Ruby Calculator)

Discussion in 'Ruby' started by Josef 'Jupp' Schugt, Sep 5, 2003.

1. ### Josef 'Jupp' SchugtGuest

Saluton!

I am proud to announce version 2.0 of Ruby Calculator (rcalc for
short). Ruby calculator is a scientific RPN (Reverse Polish Notation)
calculator based on the extmath library. At present it only provides
a commandline interface but that may change in the future. Supported
functions are:

!, !=, #, *, **, +, -, ., /, <, <=, ==, >, >=, ?, ^, abs, acos,
acosh, acot, acoth, acsc, acsch, add, and, asec, asech, asin,
asinh, atan, atan2, atanh, beta, c, ceil, cos, cosh, cot, coth,
csc, csch, delta, div, drop, dup, e, epsilon, eq, eqv, erf, erfc,
exp, exp10, exp2, f, factorial, false, floor, frexp, gcd, ge, gt,
hypot, in, inv, lcm, ld, ldexp, le, lg, lgamma, linsolve, ln, log,
log10, log2, lt, mul, nand, ne, neqv, nor, not, or, over, p, pi,
pow, puts, read, rnd, root, rot, round, sec, sech, sign, sin, sinc,
sinh, sqr, sqrt, sqsolve, sub, swap, t, tan, tanh, tgamma, to_f,
to_i, to_s, true, x, xor

You want to input mass in kg and speed in m/s and need kinetic energy
in J? That's a one-liner:

rcalc 'mass [kg]?' in 0.5 x 'speed [m/s]?' in sqr x 'E_kin [J]' p

The interaction looks like this:

mass [kg]?
7
speed [m/s]?
3
E_kin [J]
31.5

Wonder why 'x' is used for multiplication? This is because of Unix
shells that interpret '*' as all files and subdirectories in the
current directory and '.' as the present directory. On systems that
don't do that (or if you escape these characters) they also result in
multiplication and so does 'mul'.

rcalc is self-documenting which means that you can ask for help on
specific functions. If alternative names of a function are supported
they are listed - try 'rcalc --help' or 'rcalc -h' for details.

rcalc's homepage is at

http://rcalc.rubyforge.org/

Note that rcalc requires extmath. extmath's homepage is at

http://extmath.rubyforge.org/

Presently only radian is supported but support for degree and gon is
the next issue I will address.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 5, 2003

2. ### mgarrissGuest

Wow, good work. That's really cool. I'll be trying it out soon.

Michael

mgarriss, Sep 6, 2003

3. ### Josef 'Jupp' SchugtGuest

Saluton!

* Josef 'Jupp' Schugt; 2003-09-05, 19:41 UTC:
Still holds.
Done.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 9, 2003
4. ### Harry OhlsenGuest

Have you or anyone else written a command-line front-end for the Engine? It would be great to have as a more capable equivalent of Unix's "dc", particularly given that it would run on Windows, too.

Harry O.

Harry Ohlsen, Sep 10, 2003
5. ### Josef 'Jupp' SchugtGuest

Saluton!

* Harry Ohlsen; 2003-09-10, 11:57 UTC:
I am a bit unsure what you are looking for but I will add an
interactive mode that allows you to directly enter commands - instead
of providing them as command line parameters.

Now that there is an explicit request for this I will add this first
and delay adding support for math-const library.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 10, 2003
6. ### Josef 'Jupp' SchugtGuest

Saluton!

* Harry Ohlsen; 2003-09-10, 11:57 UTC:
You are invited to give version 2.2 a try. Follows output of
'rcalc --help':

-------------------------------------------------------------------------------
AVAILABLE FUNCTIONS: !, !=, #, *, **, +, -, ., /, <, <=, ==, >, >=, ?, ^, abs,
acos, acosh, acot, acoth, acsc, acsch, add, and, asec, asech, asin, asinh,
atan, atan2, atanh, beta, c, ceil, cos, cosh, cot, coth, csc, csch, deg,
deg2gon, deg2rad, delta, div, drop, dup, e, epsilon, eq, eqv, erf, erfc, exp,
exp10, exp2, f, factorial, false, floor, frexp, gcd, ge, gon, gon2deg, gon2rad,
gt, hypot, in, inv, lcm, ld, ldexp, le, lg, lgamma, linsolve, ln, log, log10,
log2, lt, mul, nand, ne, neqv, nor, not, or, over, p, pi, pow, puts, rad,
sinh, sqr, sqrt, sqsolve, sub, swap, t, tan, tanh, tgamma, to_f, to_i, to_s,
true, x, xor
-------------------------------------------------------------------------------
General help: 'rcalc -h' or 'rcalc --help'
Specific function(s): 'rcalc -h <fn> ... ' or 'rcalc --help <fn> ...'
HTML help: 'rcalc -H' or 'rcalc --htmlhelp'
Run scripts: 'rcalc -f <file> ...' or 'rcalc --file <file> ...'
Interative usage: 'rcalc' (without any arguments)

Josef 'Jupp' Schugt, Sep 10, 2003
7. ### Harry OhlsenGuest

Great! I'll give it a try.

That was an incredibly quick turn around only one "sleep" for me .

Cheers,

Harry O.

Harry Ohlsen, Sep 11, 2003
8. ### Harry OhlsenGuest

You are invited to give version 2.2 a try. Follows output of
This is great!

My only suggestion would be that it would be nice if one could enter multiple commands on one line.

Ie, rather than just trying to execute the line as typed, you could split it into separate words using something like:

commands.split(/[\s;,]+/).each do |cmd|
# execute cmd
end

I've specified whitespace, commas and semicolons as potential command separators, simply because people are likely to guess one of them.

Just a thought.

Again, great work.

Cheers,

Harry O.

Harry Ohlsen, Sep 11, 2003
9. ### Ed BakerGuest

I tried installing this with the usual:
ruby install.rb config
ruby install.rb setup
ruby install.rb install

I'm on a Windows XP Pro system.
I tried running the install commands under a cygwin shell and a 'cmd' shell.
No Joy.
Any suggestions?

----------------------- here's my config.save file::::
bin-dir=\$prefix/bin
site-ruby=\$prefix/lib/ruby/site_ruby/1.8
prefix=c:/ruby
ruby-path=c:/ruby/bin/ruby
make-prog=make
rb-dir=\$site-ruby
without-ext=no
ruby-prog=c:/ruby/bin/ruby
site-ruby-common=\$prefix/lib/ruby/site_ruby
std-ruby=\$prefix/lib/ruby/1.8
data-dir=\$prefix/share
so-dir=\$prefix/lib/ruby/site_ruby/1.8/i386-msvcrt

Ed Baker, Sep 11, 2003
10. ### Josef 'Jupp' SchugtGuest

Saluton!

* Harry Ohlsen; 2003-09-11, 12:01 UTC:
present :-> I forgot to mention that you can enter several commands
on one line as long as they are separated by white space.

I don't want to support the use of a comma to avoid confusion. Most
people would read '3,142' as 'three thousand one hundred fourty-two'
or as 'drei komma eins vier zwei'. The latter is the German version
which in English means 'three dot one four two'. Few people would
read it as 'three followed by hundred fourty-two'.

As far as the use of semicolon is concerned: I want to use it as a
comment character (as it is in LISP).

BTW: I found a bug in interactive and script mode that is related to
splitting lines - strings in quotation signs are split, too. I will
look into that.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 11, 2003
11. ### Josef 'Jupp' SchugtGuest

Saluton!

* Ed Baker; 2003-09-11, 20:51 UTC:
I cannot help you because I don't run Windows. If you find a solution
please send it to me so that I can add the information to the INSTALL
instructions.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 12, 2003
12. ### Josef 'Jupp' SchugtGuest

Saluton!

* Josef 'Jupp' Schugt; 2003-09-11, 19:24 UTC:
This has been implemented in 2.3 now. Follows an example rcalc script
that illustrates usage of comments. Note that the handling of strings
has been enhanced. They are left intact and you can use escape
sequences. Supported sequences are \", \', \\, \a, \b, \e, \f, \n,
\r, \t, \v, \o, \oo, \ooo, \xh, and \xhh where 'o' is an octal digit

I did notice that there is a need for some command to flush the whole
stack. That command now exists and is called 'clear'.

You are irrited by the name of this function? Ask Matz why he did
choose it. 'clear' does nothing but call arr.clear - the Engine class
that is responsible for computations is a superclass of the Array
class.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Compute kinetic energy from mass and speed
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

'mass [kg]?' in ; input mass in kg (we call it m)
0.5 x ; TOS now is 0.5 * m
'speed [m/s]?' in ; input speed in m/s (we call it v)
sqr x ; TOS now is 0.5*m*v**2
'\nkinetic energy [J]' ; explanatory string is pushed
p ; stack is printed

; Let us suppose you enter 3 for m and 5 for v.
; After running the script you see this on your screen
; (Without the '; ' at the beginning of each line of course):

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 'mass [kg]?'
; 3
; 'speed [m/s]?'
; 5
;
; kinetic energy [J]
; 37.5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; You notice that you can use escape sequences, don't you?
This bug now is fixed.

Gis,

Josef 'Jupp' Schugt

Josef 'Jupp' Schugt, Sep 13, 2003