Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

command line calculator?

404 views
Skip to first unread message

Tim Baker

unread,
Mar 22, 2004, 9:28:40 AM3/22/04
to
Does anyone know of a command line calculator that I could use with my batch
files in Windows? I would like to be able to give it a command such as "calc
5 * 2" or "calc sqrt 15" and then redirect the output to a file, where I can
pick up the result later.

thanks,
Tim


Timo Salmi

unread,
Mar 22, 2004, 10:35:28 AM3/22/04
to
Tim Baker <tim.bak...@NOSPAMsomersetcare.co.uk> wrote:
> Does anyone know of a command line calculator that I could use with my batch
> files in Windows? I would like to be able to give it a command such as "calc

It is reasobaly easy to build it up with QBASIC or WBS. More on the
former option e.g. in

243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi

Alternatively, e.g.

ftp://garbo.uwasa.fi/pc/ts/tsfunc17.zip Plot/calculate functions T.Salmi
Filename Comment Date Time
-------- -------------------------------- ---- ----
FILE_ID.DIZ Brief characterization of tsfunc Oct-13-2001 09:59:50
FN.EXE Calculator (evaluates functions) Oct-13-2001 17:18:14 <----
FN.ICO Windows icon for Fn Feb-20-1998 19:08:38
FN.PIF Windows 3.1x PIF for Fn Feb-17-1998 08:06:28
FNP.EXE Plots any function Feb-20-1998 19:28:32
FNP.ICO Windows icon for Fnp Feb-21-1998 08:16:56
FNP.PIF Windows 3.1x PIF for Fnp Feb-21-1998 08:18:58
FNT.EXE Tabulates user's function Feb-20-1998 20:28:24
FNT.ICO Windows icon for Fnt Feb-20-1998 23:12:14
FNT.PIF Windows 3.1x PIF for Fnt Feb-21-1998 08:10:46
TSFUNC.INF Document and a readme Oct-13-2001 10:07:08
TSFUNC.NWS News announcements about tsfunc Oct-13-2001 10:15:42
TSPROG.INF List of programs from Timo Salmi Mar-26-2000 18:03:18
VAASA.INF Info: Finland, Vaasa, U of Vaasa Oct-18-1997 13:18:46
---- ------ ------ -----
0014 167304

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Charles Dye

unread,
Mar 22, 2004, 12:16:50 PM3/22/04
to

CMD.EXE has a simple expression evaluator in SET /A. It supports basic
arithmetic functions, but little more.

set /a result=5 * 2
echo Five times two is %result% > outfile.txt

CMD.EXE is the CLI which ships with Windows NT/2000/XP. It's the most-
used command shell for these operating systems, but not the only one nor
the best. 4NT, a commercial replacement from JP Software, has a better
expression evaluator in the @EVAL function. Since this is a function,
you don't need a separate command to do math. You just put your
calculations inline, rather like a programming language:

echo The square root of 15 is %@eval[15 ** 0.5] > outfile.txt

Rolling the 4NT function into a command is trivial; use an alias:

alias calc=`echo %@eval[%$]`
calc 15 ** 0.5 > outfile.txt

--
Charles Dye ras...@highfiber.com

Phil Robyn

unread,
Mar 22, 2004, 2:26:19 PM3/22/04
to
Tim Baker wrote:

C:\cmd>type C:\cmd\demo\WSHCalc.cmd
@echo off
echo>~tmp.vbs WScript.Echo Eval("%*")
for /f "tokens=*" %%a in ('cscript //nologo ~tmp.vbs') do echo %%a
del ~tmp.vbs


C:\cmd>demo\WSHCalc (123.45 * 17.91)/8.72
253.553841743119

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Tim Baker

unread,
Mar 23, 2004, 4:00:26 AM3/23/04
to
Thanks for all the the ideas guys!

Tim


Timo Salmi

unread,
Mar 23, 2004, 6:55:53 AM3/23/04
to
DRAFT:

61} How can one devise a command line calculator?

There are several options. A fairly trivial, but severely limited
option is using the SET /A command. For example
@echo off & setlocal enableextensions
set /a x=4+7
echo %x%
endlocal & goto :EOF
The output would be
D:\TEST>cmdfaq
11

A second option is using G(nu)AWK. For example
@echo off & setlocal enableextensions
gawk 'BEGIN{printf"%%s\n",4/7}'
endlocal & goto :EOF
The output would be
D:\TEST>cmdfaq
0.571429
This option could be used to perform quite complicated calculations.

A third option is using a third-party program such as FN.EXE from
ftp://garbo.uwasa.fi/pc/ts/tsfunc17.zip (or whatever is the current
version number). An example
D:\TEST>fn sin(45*3.1415926536/180) /b
0.7071067812

A fourth option is using QBASIC or GWBASIC, if available. For more
see
FU.BAT Poor man's function evaluator
and the MS-DOS+Win../95/98/ME FAQ ftp://garbo.uwasa.fi/pc/link/tsbat.zip
item "141. Can I calculate factorials in a batch? Do I need
recursion?" which is sufficiently compatible with NT/2000/XP.

A fifth option is letting the CMD.EXE script build a Visual Basic
Script (VBScript)
@echo off & setlocal enableextensions
::
:: Usage
if [%1]==[] (
echo.+----------------------------------------------------+
echo ^| VCALC.CMD to perform Visual Basic calculations ^|
echo ^| By Prof. Timo Salmi, Last modified Tue 23-Mar-2004 ^|
echo +----------------------------------------------------+
echo.
echo Usage: %~f0 Formula
echo.
echo Examples: VCALC 4 / 7 * 2.5
echo VCALC Round^(Sqr^(2^),3^)
echo VCALC 2^^^^4 [Note the need of double carets ^^]
echo VCALC 103 mod 10
echo VCALC 103 \ 10
echo VCALC Exp^(1^)
echo VCALC Log^(10^)
echo VCALC sin^(45*3.1415926536/180^)
echo VCALC CDbl(rnd)
goto :EOF)
::
:: Make a temporary directory
if not exist c:\mytemp mkdir c:\mytemp
::
:: Build a Visual Basic Script
echo Randomize>c:\mytemp\vbscalc.vbs
echo WScript.Echo Eval("%*")>>c:\mytemp\vbscalc.vbs
::
:: Run it with Microsoft Windows Script Host Version 5.6
cscript //nologo c:\mytemp\vbscalc.vbs>c:\mytemp\result.txt
::
:: The result from the optional output file
type c:\mytemp\result.txt
::
:: Clean up (leave c:\mytemp\result.txt if need be)
for %%f in (c:\mytemp\vbscalc.vbs c:\mytemp\result.txt) do (
if exist %%f del %%f)
rmdir c:\mytemp
endlocal & goto :EOF

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Tim Baker

unread,
Mar 23, 2004, 8:58:16 AM3/23/04
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:c3p8k9$8...@poiju.uwasa.fi...
> DRAFT:

>
> A third option is using a third-party program such as FN.EXE from
> ftp://garbo.uwasa.fi/pc/ts/tsfunc17.zip (or whatever is the current
> version number). An example
> D:\TEST>fn sin(45*3.1415926536/180) /b
> 0.7071067812
>

Timo, thanks for that program, it does exactly what I need, and keeps the
readability of my batch files a lot better than the other solutions. I will
definately be looking through your FTP site for any other useful little
apps!
Thanks,

Tim


Dr John Stockton

unread,
Mar 23, 2004, 3:14:11 PM3/23/04
to
JRS: In article <c3p8k9$8...@poiju.uwasa.fi>, seen in
news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Tue, 23 Mar
2004 13:55:53 :-

>A fifth option is letting the CMD.EXE script build a Visual Basic
>Script (VBScript)


You should be able to add "or javascript"; there are examples at
<URL:http://www.merlyn.demon.co.uk/batfiles.htm#WSH> (tested only in
Win98, AFAIK) which show javascript doing things that VBScript cannot
[so readily]? do.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

0 new messages