This does NOT echo "greater"
if 54366127616 GTR 15591100416 echo greater
The numbers seems to be too big, with lower numbers it works.
Is there any size restriction?
What can I do?
Thank You
Joachim
Windows Server Enterprise SP 1 / Microsoft Windows [Version 6.0.6001]
The magic (and maximum) positive integer is 2147483647.
Try running
@echo off
set n=2147483645
for /l %%z in (1,1,6) do call :add
goto :eof
:add
set /a n+=1
echo %n%
goto :eof
What you do with larger numbers depends on their closeness. If they
are very different you could drop the final 'n' digits from each and
compare. If they are very close (e.g. 54366127616 and 54366127619)
that approach won't work. There must be better ways, but curiously
TSCMD doesn't seem to mention the topic!
To go beyond the magic number quoted by John, you could use the approach
below. It will deal with considerably larger numbers.
@echo off
set a=54366127616
set b=15591100416
set Scr=c:\TempVBS.vbs
set VB=echo^>^>%Scr%
cd 1>nul 2>%Scr%
%VB% If Not IsNumeric(%a%) then WScript.Quit 9
%VB% If Not IsNumeric(%b%) then WScript.Quit 9
%VB% If %a% = %b% then WScript.Quit 0
%VB% If %a% ^> %b% then WScript.Quit 1
%VB% If %a% ^< %b% then WScript.Quit 2
cscript //nologo %Scr%
echo ErrorLevel = %ErrorLevel%
del %Scr%
You can compare any two numbers, regardless of size, with the batch file
below. Just make sure to set %len% to a length that exceeds the maximum
number of digits you expect to process, and %pad% to a string of at least
%pad% zeroes.
@echo off
set a=123456789012347
set b=123456789012346
set len=20
set pad=0000000000000000000000000000000000000000000000000000000000
set x=%pad%%a%&set y=%pad%%b%
set x=%x:~-20%&set y=%y:~-20%
:Loop
if not %x:~0,1%==%y:~0,1% goto :Label1
if %x% equ %y% set res=equal to
if %x% lss %y% set res=smaller than
if %x% gtr %y% set res=greater than
set x=%x:~1%& set y=%y:~1%
if "%x%"=="" set comp=smaller than& goto :Label2
if "%y%"=="" set comp=greater than& goto :Label2
goto Loop
:Label1
if %x:~0,1% GTR %y:~0,1% (
echo %a% is greater than %b%
) else (
echo %a% is smaller than %b%
)
goto :eof
:Label2
if "%x%%y%"=="" set comp=%res%
echo %a% is %comp% %b%
Way back when (before the added comparison operators of NT), I used to
use the SORT utility to instrument 'greater than/less than'
comparisons. Here it is retreaded in NT for this long number
problem ...
@echo off
set a=123456789012347
set b=123456789012346
:: set len=20 strictly informational, I think
set pad=0000000000000000000000000000000000000000000000000000000000
set "x=%pad%%a%" & set "y=%pad%%b%"
set "x=%x:~-20%" & set "y=%y:~-20%"
if %x%==%y% (
echo a equals b
) else (
for /f "tokens=1-2 delims=, skip=1" %%a in (
'^(echo a,b,%x% ^& echo b,a,%y%^) ^|sort /+5') do (
echo %%a is larger than %%b)
)
Tom Lavedas
***********
set pad=0000000000000000000000000000000000000000000000000000000000
===============
Think again. %Len% should be set at or slightly above the expected number of
digits. If you make it too small then the batch file will fail. If you make
it too large or if you make %pad% too long then the batch file becomes
inefficient because it nees to process too many leading zeroes.
Even easier is to just quote the numbers being compared.
@echo off
set a=123456789012346
set b=123456789012347
set pad=00000000000000000000000000000000000000000000000000
set "x=%pad%%a%" & set "y=%pad%%b%"
set "x=%x:~-50%" & set "y=%y:~-50%"
if "%x%" equ "%y%" (
echo a equals b
) else (
if "%x%" gtr "%y%" (
echo a is larger than b
) else (
echo b is larger than a
)
)
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
Tom Lavedas
***********
Oops, you were right, in the copy/paste process I omitted a key line from my
code. Here is what it should have been:
@echo off
set a=1234567890123400
set b=123456789012346
set len=20
set pad=0000000000000000000000000000000000000000000000000000000000
call set pad=%%pad:~0,%len%%%
> if 54366127616 GTR 15591100416 echo greater
> Is there any size restriction?
Run IF /? and read about it. You might need to convert the number to a string:
if A54366127616 GTR A15591100416 echo greater
Frank
Frank
==========
Maybe - but try this:
if A54366127616 GTR A15591100416 echo greater
if A5436612761 GTR A15591100416 echo greater
>> if 54366127616 GTR 15591100416 echo greater
>
>> Is there any size restriction?
>
>Run IF /? and read about it. You might need to convert the number to a
>string:
>
> if A54366127616 GTR A15591100416 echo greater
>
>Frank
>
>==========
>Maybe - but try this:
>if A54366127616 GTR A15591100416 echo greater
>if A5436612761 GTR A15591100416 echo greater
In such a case it is doing an ascii compare, not numerical, so you have to
arrange the string properly...
. . . which was all covered in previous replies in this thread.
If it was already covered then why did you create an invalid compare string
and post it here? :)
In reply to Frank Westlake's suggestion!
I somehow got an incomplete thread -- I thought I was replying to the first and only article in it.
My reply wasn't intended to be a complete spoonfeeding, as many of you enjoy doing, it was intended to guide toward a solution.
Frank
Frank
=============
It cannot be a guide towards a solution because it produces a wrong result,
e.g. here:
if A5 GTR A15 echo greater
Earlier in the thread there were a number of solutions that generate the
correct result - why chip in with a suggestion that is obviously flawed?
That isn't the example which was given. In the example which was given both strings were the same length.
> Earlier in the thread there were a number of solutions that generate the
> correct result - why chip in with a suggestion that is obviously flawed?
As I said, I only saw the one article. My reply was not flawed, it works with the example given and was intended as a guide to what might need to be done for other solutions. I try not to spoonfeed.
Frank
Frank
===========
OK, I agree. Your method works well when comparing numbers that have the
same number of digits. It fails in 50% of all cases where the number of
digits is different.
Frank
==========
I'll revise my estimate. Your method of comparing two numbers having a
random number of digits will yield the correct result in about 75% of all
cases and the wrong result in about 25% of all cases. If you're happy with
this performance then your method is the way to go.
>> > It cannot be a guide towards a solution because it produces a wrong
>> > result, e.g. here:
>> >
>> > if A5 GTR A15 echo greater
>>
>> That isn't the example which was given. In the example which was given both
>> strings were the same length.
>>
>> > Earlier in the thread there were a number of solutions that generate the
>> > correct result - why chip in with a suggestion that is obviously flawed?
The original question was:
This does NOT echo "greater"
if 54366127616 GTR 15591100416 echo greater
Frank's suggestion gives the correct answer:
if A54366127616 GTR A15591100416 echo greater
Why isn't this a "guide towards a solution"?
Now, if the OP want's to compare numbers of different
size, then he has to adopt this suggestion, maybe like:
@echo off
set x=12345678901234567890
set y=1234567890123456789
setlocal
set a=%x%
set b=%y%
for %%i in (1 2 3 4 5 6 7 8 9) do call set a=%%a:%%i=0%%
for %%i in (1 2 3 4 5 6 7 8 9) do call set b=%%b:%%i=0%%
endlocal & set x=%b%%x% & set y=%a%%y%
if a%x% gtr a%y% echo %x% is greater than %y%
if a%x% leq a%y% echo %x% is not greater than %y%
> I'll revise my estimate. Your method of comparing two numbers having a
> random number of digits will yield the correct result in about 75% of all
> cases and the wrong result in about 25% of all cases.
That revision you have to explain (what's wrong with 50%?).
Personally, I believe the correct answer here is "marginally greater than
50% success."
The alphabetical-comparison method would return a result based on the
earliest mismatched character in the string, so this is a 50-50 chance of a
correct result.
BUT where the two strings match for the length of the shorter string, the
the result will ALWAYS be correct, biasing the success proportion slightly.
The exact proportion would depend on the length of the strings allowed, and
as a logician rather than a mathematician, I'll not pursue that argument
further.
As for a reliable routine to compare the magnitude of two arbitrary positive
leading-zero-suppressed numbers:
This solution developed using XP
It may work for NT4/2K
----- batch begins -------
[1]@echo off
[2]set first=%1
[3]set second=%2
[4]set greater=N
[5]:loop
[6]if %first%==%second% goto :eof
[7]set finalf=%first:~-1%&set first=%first:~,-1%&set finals=%second:~-1%&set
second=%second:~,-1%
[8]if %finalf%==%finalsd% goto nochg
[9]if %finalf% gtr %finals% (set greater=Y) else (set greater=N)
[10]:nochg
[11]if defined first (if defined second (goto loop) else (set greater=Y) )
else (if defined second (set greater=N))
[12]for %%i in (finalf finals) do (set %%i=)
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed
The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof
If Frank Westlake wants his suggestion to be a dependable guide to a
solution then he must state that it can only be used for numbers of equal
length. Omitting this essential restriction is IMHO misleading. It's like
saying "the squares of all integers end in 6" without adding the restriction
"provided that the integers themselves end in 4 or 6".
I may be wrong with my probability estimates but my reasoning went as
follows:
1. if length(Number1) > length(Number2) then Frank's method will always
yield the correct result.
2. if length(Number1) < length(Number2) and Number1 > Number2 then Frank's
method will always yield the correct result.
3. if length(Number1) < length(Number2) and Number1 < Number2 then Frank's
method will always yield the wrong result.
4. if length(Number1) = length(Number2) then Frank's method will always
yield the correct result.
About 50% of all random integers belong to Class 1.
About 25% of all random integers belong to Class 2.
About 25% of all random integers belong to Class 3.
A small percentage of all random integers belong to Class 4.
Hence my estimate of about 75% right, 25% wrong.
Lastly, since there were several responses with unconditional solutions,
Frank's solution appeared to be a backwards step.
Perhaps we should ask the puzzlers?
(xpost added, but cunning batch snipped)
--
Siggy played guitar
I believe that there's a problem with your assertion about case 1, 2 and 3 -
especially 1.
Since the solution relies on prefixing the numeric-string with an alphabetic
so that the comparison is performed on two alphabetic strings,
if string1 gtr string2
will be FALSE if string1=A1234 and string2=A987 as the "1" is LESS than the
"9" but 1234 >987
The longer string will only be classed as greater if the strings match for
the length of the shorter string.
You're still assuming that I was spoonfeeding an all inclusive solution. As I've answered twice now, I was not spoonfeeding. I was giving an example of how to create a solution by giving one for the example given. I also refered the person to 'IF /?" for further guidance.
Your comments seen to indicate that you believe this forum is an answer forum, where people submit problems and others give complete solutions to those problems. It it not.
Frank
I did not omit it. Here's my statement:
"Frank P. Westlake" news:h7ivd5$mr4$1...@news.albasani.net...
> Run IF /? and read about it. You might need to convert the number to a string:
>
> if A54366127616 GTR A15591100416 echo greater
"Pegasus" news:4a9fffd4$0$13615$5402...@news.sunrise.ch...
> It's like saying "the squares of all integers end in 6" without adding
> the restriction "provided that the integers themselves end in 4 or 6".
No, it's not like saying that. For it to be like saying that I should've said something like "the complete answer to all related problems is...." But as you can see above I didn't even imply that I was giving a complete solution to the example given.
All of your replies to my messages in this thread indicate to me that you are just simply attacking and are inventing reasons for the attack.
Frank
Pegasus could have commented on the string comparison in reasonable detail
rather than post a line of code.
Frank could have spelt out that there were limitations when comparing ascii
strings.
I'm not immune to posting when a little hot headed...
How about we all drop this thread and do something constructive?
I vote for you for President. You get to drive.
Frank