Page 1 of 2

numbers in variables

Posted: 02 Nov 2011 05:00
by Mohammad_Dos
i want to save some number in variables. for example: 326598745 or 32.659
how to do it?

Re: numbers in variables

Posted: 02 Nov 2011 10:13
by trebor68
You can save numbers easy save with the set command:

Code: Select all

set var=12345

The number will also working as a string.

But if you will working with mathematic operations ("+", "-", "*", "/") only with numbers less 2^31 or negative value.

Code: Select all

set /a var2=%var%/125

Re: numbers in variables

Posted: 02 Nov 2011 12:02
by Mohammad_Dos
why it does not work?

Code: Select all

set /a nom=2.335

Re: numbers in variables

Posted: 02 Nov 2011 12:41
by aGerman
Because the cmd knows only integer values. There are no floating points allowed.

Code: Select all

@echo off &setlocal
echo the limit is exceeded
set /a var1=2147483648
echo %var1%
echo(
echo that's the upper bound
set /a var2=2147483647
echo %var2%
echo(
echo that's the lower bound
set /a var3=-2147483647
echo %var3%
echo(
echo that's below the limit
set /a var4=-2147483648
echo %var4%
echo(
echo there are no floating points
set /a var5=3.14159
echo %var5%
echo(
echo there are no thousand separators
set /a var6=1,000,000
echo %var6%
echo(
pause


My output on Win7:
the limit is exceeded
Ungültige Zahl. Zahlen sind begrenzt auf eine Genauigkeit von 32 Bits.
ECHO ist ausgeschaltet (OFF).

that's the upper bound
2147483647

that's the lower bound
-2147483647

that's below the limit
Ungültige Zahl. Zahlen sind begrenzt auf eine Genauigkeit von 32 Bits.
ECHO ist ausgeschaltet (OFF).

there are no floating points
Fehlender Operator
3

there are no thousand separators
1

Drücken Sie eine beliebige Taste . . .

I remember the behaviour on XP was slightly different if one tried to define a number greater or less than the limit.

Regards
aGerman

Re: numbers in variables

Posted: 03 Nov 2011 00:54
by Mohammad_Dos
i want to add float numbers to variables. how? is it possible?

Re: numbers in variables

Posted: 03 Nov 2011 07:38
by !k
only if you add them as string
c:\>set pi=3.14159

c:\>clc 2 * %pi%
6.28318
c:\>clc %pi% ^^ 2
9.8695877281

Re: numbers in variables

Posted: 04 Nov 2011 03:45
by Mohammad_Dos
it does not work for me:

Code: Select all

clc 2 * %pi%

Re: numbers in variables

Posted: 04 Nov 2011 04:43
by aGerman
Of course it will not work. As I explained before you cannot calculate floats by default, only integers are supported.
CLC is a third party application. You can download it somewhere in the internet.

Regards
aGerman

Re: numbers in variables

Posted: 04 Nov 2011 04:51
by !k
Mohammad_Dos wrote:it does not work for me:

Code: Select all

clc 2 * %pi%
really?
viewtopic.php?p=5672#p5672

Re: numbers in variables

Posted: 04 Nov 2011 06:31
by Mohammad_Dos
wow! you have very powerful memory! :shock:

is there another way without download any extra file? with dos's own commands

Re: numbers in variables

Posted: 04 Nov 2011 08:01
by !k
vbs?

Code: Select all

@echo off
>calc.vbs echo WScript.Echo Eval(WScript.Arguments(0))

set pi=3.14159
call:calc 2 * %pi%
call:calc %pi% ^^ 2
del /q calc.vbs
goto:eof

:calc
cscript //nologo calc.vbs "%*"
goto:eof

Re: numbers in variables

Posted: 04 Nov 2011 08:11
by Judago
I wrote a floating point/large number batch script recently(mostly just for the fun of it). Just addition, subtraction, multiplication and division(can be slow).

If your interested here it is, I can't guarantee accuracy and it's probably too much code to be really useful. Either way just thought I'd post it....

Re: numbers in variables

Posted: 02 Dec 2011 16:17
by jeb
Judago wrote:I wrote a floating point/large number batch script recently(mostly just for the fun of it).


I tried it, very nice work :D

Judago wrote:I can't guarantee accuracy and it's probably too much code to be really useful.

It seems to be correct, and I see many larger scripts here, which are not so useful. :)

jeb

Re: numbers in variables

Posted: 02 Dec 2011 18:32
by orange_batch
Integer multiplication/division of floating point numbers in DOS.

Only accurate up to the last digit of your number (floored), could be improved to support full-length numbers, floating point operands, addition/subtraction...

Code: Select all

@echo off&setlocal enabledelayedexpansion

:: Multiply.
set num=200.333
set operation=*2
call :calcfloat %num% %operation%

:: Divide.
set num=500.222
set operation=/2
call :calcfloat %num% %operation%

pause
exit

:: Float Math
:calcfloat
for /f "delims=. tokens=1*" %%a in ("%1") do (
call :getlength %%b
set pad=
for /l %%c in (1,1,!length!) do set pad=0!pad!
set /a result=%%a!pad!%2+%%b%2
)
set "result=!result:~,-%length%!.!result:~-%length%!"
echo: ^> Result: %result%
exit/b

:: Length, max 2048 chars.
:getlength
set "lengthstr=x%~1"
set length=0
for /l %%a in (10,-1,0) do (
set /a "length|=1<<%%a"
for /f "delims=" %%b in ("!length!") do if "!lengthstr:~%%b,1!"=="" set /a "length&=~1<<%%a"
)
exit/b

On a similar topic, you can split 32-bit integers to surpass the limit of 2,147,483,647.

Re: numbers in variables

Posted: 02 Dec 2011 21:47
by Judago
Thanks jeb!

orange_batch, I like the simplified process, I did it in a similar way just a little more long winded. I wouldn't mind seeing the integer splitting technique you mention.