Page 1 of 1

A Critique on DOS limitation with maths and its workaround

Posted: 05 Apr 2011 07:46
by ghostmachine4
Source1: Floating Point Maths
Source2: set /A

An example of normal maths with decimal values on DOS

Code: Select all

C:\work>set /a x=20
20
C:\work>set /a y=3*x
60
C:\work>echo %y%
60

C:\work>set /a y=3.3*x
Missing operator.


Oops! Batch/DOS does not support floating point maths for newbies to batch who does not know this fact yet. What do you do? You can switch to vbscript or some better programming language if you can afford to. However, for those stuck with just the tools you have, here's an implementation in vbscript to workaround this annoying limitation. Note: vbscript comes installed by default on most Windows distribution. There is no reason one could not get to know it and harness its capabilities (same with powershell).

Code: Select all

WScript.Echo Eval (WScript.Arguments(0))


Code: Select all

C:\work>cscript //nologo test1.vbs "1 * 19"
19

C:\work>cscript //nologo test1.vbs 1*19
19

C:\work>cscript //nologo test1.vbs 20/2.3
8.69565217391304

C:\work>cscript //nologo test1.vbs 1+2
3



There, your problem is solved.

DosItNotHelp