Page 1 of 1

Mathematical Operations

Posted: 16 Jun 2011 17:10
by Cleptography
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.

Re: Mathematical Operations

Posted: 16 Jun 2011 17:19
by aGerman
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

Re: Mathematical Operations

Posted: 16 Jun 2011 17:23
by Cleptography
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

Re: Mathematical Operations

Posted: 16 Jun 2011 17:33
by aGerman
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

Re: Mathematical Operations

Posted: 21 Jun 2011 22:02
by nitt
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.

Re: Mathematical Operations

Posted: 24 Jun 2011 20:46
by scienceguru1.bat
could you do something like:

Code: Select all

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

:?: :?: :?:

Re: Mathematical Operations

Posted: 24 Jun 2011 21:33
by Acy Forsythe
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

Re: Mathematical Operations

Posted: 29 Jun 2011 07:01
by Cleptography
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

Re: Mathematical Operations

Posted: 04 Jul 2011 08:56
by orange_batch
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.