numbers in variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

numbers in variables

#1 Post by Mohammad_Dos » 02 Nov 2011 05:00

i want to save some number in variables. for example: 326598745 or 32.659
how to do it?

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: numbers in variables

#2 Post by trebor68 » 02 Nov 2011 10:13

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

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: numbers in variables

#3 Post by Mohammad_Dos » 02 Nov 2011 12:02

why it does not work?

Code: Select all

set /a nom=2.335

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

Re: numbers in variables

#4 Post by aGerman » 02 Nov 2011 12:41

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

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: numbers in variables

#5 Post by Mohammad_Dos » 03 Nov 2011 00:54

i want to add float numbers to variables. how? is it possible?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: numbers in variables

#6 Post by !k » 03 Nov 2011 07:38

only if you add them as string
c:\>set pi=3.14159

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

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: numbers in variables

#7 Post by Mohammad_Dos » 04 Nov 2011 03:45

it does not work for me:

Code: Select all

clc 2 * %pi%

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

Re: numbers in variables

#8 Post by aGerman » 04 Nov 2011 04:43

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: numbers in variables

#9 Post by !k » 04 Nov 2011 04:51

Mohammad_Dos wrote:it does not work for me:

Code: Select all

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

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: numbers in variables

#10 Post by Mohammad_Dos » 04 Nov 2011 06:31

wow! you have very powerful memory! :shock:

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: numbers in variables

#11 Post by !k » 04 Nov 2011 08:01

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

Judago
Posts: 15
Joined: 04 Nov 2011 07:59

Re: numbers in variables

#12 Post by Judago » 04 Nov 2011 08:11

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....

jeb
Expert
Posts: 1043
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: numbers in variables

#13 Post by jeb » 02 Dec 2011 16:17

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

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

Re: numbers in variables

#14 Post by orange_batch » 02 Dec 2011 18:32

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.

Judago
Posts: 15
Joined: 04 Nov 2011 07:59

Re: numbers in variables

#15 Post by Judago » 02 Dec 2011 21:47

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.

Post Reply