numbers in variables
Moderator: DosItHelp
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
numbers in variables
i want to save some number in variables. for example: 326598745 or 32.659
how to do it?
how to do it?
Re: numbers in variables
You can save numbers easy save with the set command:
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 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
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
Re: numbers in variables
why it does not work?
Code: Select all
set /a nom=2.335
Re: numbers in variables
Because the cmd knows only integer values. There are no floating points allowed.
My output on Win7:
I remember the behaviour on XP was slightly different if one tried to define a number greater or less than the limit.
Regards
aGerman
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
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
Re: numbers in variables
i want to add float numbers to variables. how? is it possible?
Re: numbers in variables
only if you add them as string
c:\>set pi=3.14159
c:\>clc 2 * %pi%
6.28318
c:\>clc %pi% ^^ 2
9.8695877281
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
Re: numbers in variables
it does not work for me:
Code: Select all
clc 2 * %pi%
Re: numbers in variables
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
CLC is a third party application. You can download it somewhere in the internet.
Regards
aGerman
Re: numbers in variables
really?Mohammad_Dos wrote:it does not work for me:Code: Select all
clc 2 * %pi%
viewtopic.php?p=5672#p5672
-
- Posts: 84
- Joined: 08 Sep 2010 10:25
- Location: Iran,Kashan
- Contact:
Re: numbers in variables
wow! you have very powerful memory!
is there another way without download any extra file? with dos's own commands
is there another way without download any extra file? with dos's own commands
Re: numbers in variables
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
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....
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
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
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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: numbers in variables
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...
On a similar topic, you can split 32-bit integers to surpass the limit of 2,147,483,647.
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
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.
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.