Mathematical Operations

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Mathematical Operations

#1 Post by Cleptography » 16 Jun 2011 17:10

What mathematical operations can cmd process.
Examples please.
(+ - etc...) decimals...who uses decimals anymore :roll:
I know we could cheat a little and use debug doing something like

Code: Select all

C:\> debug
- H 4a44 90

Which of course print out the sum and difference of the two numbers...

Code: Select all

4AD4  49B4

...but this is cheating.
Ideas ideas.
Last edited by Cleptography on 16 Jun 2011 17:20, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Mathematical Operations

#2 Post by aGerman » 16 Jun 2011 17:19

CMD is not good in math :wink:

Numbers are limited to 32 bit integers (- 2^31 ... 2^31 - 1). No floating points (decimales are cutted).
All operators you will find in the help for SET.

Regards
aGerman

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Mathematical Operations

#3 Post by Cleptography » 16 Jun 2011 17:23

Yes cmd is limited to 32bits of precision but there are workarounds in the form of scripts.

Code: Select all

set /a 0xA + 01 + 1

but how could we workaround

Code: Select all

set /a 3000000000*2

or

Code: Select all

set /a 3/2*2

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Mathematical Operations

#4 Post by aGerman » 16 Jun 2011 17:33

Like you would do that with paper and pencil :wink:
(maybe not the best example for) division:

Code: Select all

@echo off &setlocal
set "Divident=863500000000000000000000000000000"
set "Divisor=864"
set "Decimal=2"
set "Separator=."

for /l %%i in (1,1,%Decimal%) do call set "Divident=%%Divident%%0"
set "n=0"
:loop
call set "Part=%%Divident:~%n%,1%%"
if "%Part%"=="" goto :ahead
set "Part=%Reminder%%Part%"
set /a Extension=Part/Divisor, Reminder=Part%%Divisor, n+=1
if %Reminder%==0 set "Reminder="
set "Quotient=%Quotient%%Extension%"
goto :loop

:ahead
if %Decimal% gtr 0 call set "Quotient=%%Quotient:~,-%Decimal%%%%Separator%%%Quotient:~-%Decimal%%%"
for /f "tokens=* delims=0" %%i in ("%Quotient%") do set "Quotient=%%i"
if not defined Quotient set "Quotient=0"
if "%Quotient:~,1%"=="%Separator%" set "Quotient=0%Quotient%"
echo %Quotient%
pause


Regards
aGerman

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Mathematical Operations

#5 Post by nitt » 21 Jun 2011 22:02

Well we could always handle our equations in another language. Such as 53/69 is 0.768115942028985 . But

Code: Select all

@echo off
set /a ans=53/69


Would actually return 0. So if you use

Code: Select all

@echo off
echo wscript.echo(eval("53/69")) > ~tmp.vbs
for /f "tokens=*" %%a in ('cscript ~tmp.vbs') do (set ans=%%a)
del ~tmp.vbs
echo %ans%
pause


You will actually get the correct answer. That's the best I can think of.

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Mathematical Operations

#6 Post by scienceguru1.bat » 24 Jun 2011 20:46

could you do something like:

Code: Select all

set /a a="5"
set /a b="2"
echo "%a%"+"%b%"

:?: :?: :?:

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Mathematical Operations

#7 Post by Acy Forsythe » 24 Jun 2011 21:33

That won't work, but set /a can do basic math. They are talking about complex math, floating points etc... Set /a won't do that.

This is what you were trying to do:

Code: Select all

@echo off
Set local

Set a=3
Set b=5
Set /a c=%a%*%b%
echo %a% * %b% = %c%

endlocal
exit/b


Or you can do this too for a quick commandline calculator:

Code: Select all

@echo off
Setlocal


set /a d=%1 %2 %3

echo.
echo %1 %2 %3 = %d%
echo.

endlocal
exit/b

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Mathematical Operations

#8 Post by Cleptography » 29 Jun 2011 07:01

Another batch / cmd exercise.
Convert degrees to radians
In mathmatics and physics, the radian is a unit of angle measurement. One radian is approximately 57.2957795130823 degrees. To figure out that number, use the formula ( 180 / pi )
So how can this be done using cmd?
Get the radian of 35

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Mathematical Operations

#9 Post by orange_batch » 04 Jul 2011 08:56

If the formula for degrees to radians is expressable using DOS's mathematical operations, it's possible.

Otherwise, make a call to VBScript and use one of it's many math functions.

Post Reply