3

I have the following code in my batch file:

if %bakfilesize% geq %freespacet% echo baksize is larger
if %bakfilesize% leq %freespacet% echo baksize is smaller   

When I run it, I get the following output on screen:

if 399502220288 GEQ 463777075200 echo baksize is larger
baksize is larger

if 399502220288 LEQ 463777075200 echo baksize is smaller
baksize is smaller

Can anyone tell me what is going on here? I have tried a simple version with the numbers 1 and 2, and windows can confirm that 1 is indeed less than 2, so why is it failing in this instance?

I only really need the geq statement to work. The other one is in to demonstrate the oddness.

BeanFrog
  • 2,297
  • 12
  • 26
  • See [Windows batch file IF failure - How can 30000000000000 equal 40000000000?](http://stackoverflow.com/q/9116365/1012053) for an explanation and a work-around. – dbenham Mar 05 '16 at 04:34
  • Also, you are drawing the wrong conclusion from your tests. If both GEQ and LEQ are true, then IF thinks the values are equal. Not greater than, and not less than. You will also get true with EQU. But GTR and LSS will both evaluate to false. – dbenham Nov 30 '19 at 22:30

2 Answers2

1

Both the numbers you have provided in your sample are too big to fit in 32 bit integers. I don't have a reference at hand to confirm it, but it would not be surprising at all if CMD.EXE is using 32-bit signed integer arithmetic.

463777075200 is 0x6BFB449000
399502220288 is 0x5D04301C00

If those values are truncated to 32 bits by CMD, then the bigger value would appear to be a negative number (0xFB449000 is -79392768) and is indeed less than the smaller value which is still positive when truncated (0x04301C00 is 70261760).

In the original question for it was edited to correct the obvious error you had swapped the variables as well as the comparison operator. Try:

if %bakfilesize% geq %freespacet% echo baksize is larger
if %bakfilesize% leq %freespacet% echo baksize is smaller

You might also want to think about what your code will do (and should do) when they are equal... but I'll leave that consideration as an exercise for the reader.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
1

OK, in this case, the problem is quite simple. Try this as it is in your example:

set /a bakfilesize=399502220288

You will get this error:

Invalid number. Numbers are limited to 32-bits of precision.

So this means that the numbers you are handling are too big and DOS won't be able to compare them propperly.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 1
    huh - I was sure, `if` would swich to string comparison if there is an invalid number. Obviously, it doesn't. – Stephan Mar 03 '16 at 09:28