Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IF GTR

190 views
Skip to first unread message

Joachim Hofmann

unread,
Aug 20, 2009, 9:02:52 AM8/20/09
to
Hello,

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]

John Gray

unread,
Aug 20, 2009, 9:46:08 AM8/20/09
to

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!

Pegasus

unread,
Aug 20, 2009, 10:55:35 AM8/20/09
to

"Joachim Hofmann" <spei...@freenet.de> wrote in message
news:7f4vrsF...@mid.individual.net...

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%


Pegasus

unread,
Aug 20, 2009, 12:04:11 PM8/20/09
to

"Joachim Hofmann" <spei...@freenet.de> wrote in message
news:7f4vrsF...@mid.individual.net...

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%


Tom Lavedas

unread,
Aug 20, 2009, 4:11:14 PM8/20/09
to
On Aug 20, 12:04 pm, "Pegasus" <I....@fly.com.oz> wrote:
> "Joachim Hofmann" <speic...@freenet.de> wrote in message

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

Pegasus

unread,
Aug 20, 2009, 6:21:44 PM8/20/09
to

"Tom Lavedas" <tglb...@cox.net> wrote in message
news:221137d3-f110-4c57...@m20g2000vbp.googlegroups.com...

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.


Todd Vargo

unread,
Aug 20, 2009, 9:19:10 PM8/20/09
to

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)

Pegasus

unread,
Aug 21, 2009, 4:06:57 AM8/21/09
to

"Tom Lavedas" <tglb...@cox.net> wrote in message
news:221137d3-f110-4c57...@m20g2000vbp.googlegroups.com...

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

Frank P. Westlake

unread,
Sep 1, 2009, 7:09:23 AM9/1/09
to
"Joachim Hofmann" <spei...@freenet.de> wrote in message news:7f4vrsF...@mid.individual.net...

> 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

Pegasus

unread,
Sep 1, 2009, 8:29:39 AM9/1/09
to

"Frank P. Westlake" <frank.w...@yahoo.com> wrote in message
news:h7ivd5$mr4$1...@news.albasani.net...

Frank

==========
Maybe - but try this:


if A54366127616 GTR A15591100416 echo greater

if A5436612761 GTR A15591100416 echo greater


foxidrive

unread,
Sep 1, 2009, 8:45:07 AM9/1/09
to
On Tue, 1 Sep 2009 14:29:39 +0200, "Pegasus" <I....@fly.com.oz> wrote:

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

Pegasus

unread,
Sep 1, 2009, 9:07:30 AM9/1/09
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:jn5q955v0u403coml...@4ax.com...

. . . which was all covered in previous replies in this thread.


foxidrive

unread,
Sep 1, 2009, 8:32:58 PM9/1/09
to

If it was already covered then why did you create an invalid compare string
and post it here? :)

Pegasus

unread,
Sep 2, 2009, 1:57:21 AM9/2/09
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:c7fr951uvd6bevpki...@4ax.com...

In reply to Frank Westlake's suggestion!


Frank P. Westlake

unread,
Sep 2, 2009, 7:10:02 AM9/2/09
to
"Pegasus" <I....@fly.com.oz> news:4a9e0941$0$13622$5402...@news.sunrise.ch...

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

Pegasus

unread,
Sep 2, 2009, 7:30:04 AM9/2/09
to

"Frank P. Westlake" <frank.w...@yahoo.com> wrote in message
news:h7ljqe$m03$1...@news.albasani.net...

Pegasus

unread,
Sep 2, 2009, 7:31:58 AM9/2/09
to

"Frank P. Westlake" <frank.w...@yahoo.com> wrote in message
news:h7ljqe$m03$1...@news.albasani.net...

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?


Frank P. Westlake

unread,
Sep 3, 2009, 7:12:36 AM9/3/09
to
"Pegasus" <I....@fly.com.oz> news:4a9e578d$0$145$fb62...@newsspool.solnet.ch...

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

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

Pegasus

unread,
Sep 3, 2009, 7:43:59 AM9/3/09
to

"Frank P. Westlake" <frank.w...@yahoo.com> wrote in message
news:h7o8b4$jpe$1...@news.albasani.net...

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.


Pegasus

unread,
Sep 3, 2009, 8:05:48 AM9/3/09
to

"Frank P. Westlake" <frank.w...@yahoo.com> wrote in message
news:h7o8b4$jpe$1...@news.albasani.net...

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.


Herbert Kleebauer

unread,
Sep 3, 2009, 10:52:20 AM9/3/09
to
Pegasus wrote:
> "Frank P. Westlake" <frank.w...@yahoo.com> wrote in message

>> > 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%?).

billious

unread,
Sep 3, 2009, 12:40:35 PM9/3/09
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:4A9FD824...@unibwm.de...

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


Pegasus

unread,
Sep 3, 2009, 1:41:40 PM9/3/09
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:4A9FD824...@unibwm.de...

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.


Esra Sdrawkcab

unread,
Sep 3, 2009, 1:53:13 PM9/3/09
to
On Thu, 03 Sep 2009 17:40:35 +0100, billious <billio...@hotmail.com>
wrote:

Perhaps we should ask the puzzlers?
(xpost added, but cunning batch snipped)

--
Siggy played guitar

billious

unread,
Sep 3, 2009, 2:30:40 PM9/3/09
to

"Pegasus" <I....@fly.com.oz> wrote in message
news:4a9fffd4$0$13615$5402...@news.sunrise.ch...

>
> "Herbert Kleebauer" <kl...@unibwm.de> wrote in message
> news:4A9FD824...@unibwm.de...
>
[much snippage]

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

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.


Frank P. Westlake

unread,
Sep 4, 2009, 7:19:28 AM9/4/09
to
"Pegasus" <I....@fly.com.oz> news:4a9fabdf$0$148$fb62...@newsspool.solnet.ch...

> OK, I agree. Your method works well when comparing numbers that have the
> same number of digits. It fails in ...

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

Frank P. Westlake

unread,
Sep 4, 2009, 7:57:01 AM9/4/09
to
"Pegasus" news:4a9fffd4$0$13615$5402...@news.sunrise.ch...

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

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

foxidrive

unread,
Sep 4, 2009, 11:57:36 AM9/4/09
to
To be fair to all - we often post batch code that has limitations and they
aren't always spelt out.

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?

Frank P. Westlake

unread,
Sep 5, 2009, 6:46:44 AM9/5/09
to
"foxidrive" news:5qd2a5127id2kkp9s...@4ax.com...

> How about we all drop this thread and do something constructive?

I vote for you for President. You get to drive.

Frank

0 new messages