Page 1 of 1
SET /A overflow
Posted: 07 Apr 2014 14:38
by einstein1969
Hi to all,
There is a method for detect "programmatically" an overflow for simple expressions?
for example:
This is the complete code:
Code: Select all
@echo off & setlocal
rem Tested on windows 7 32bit
rem Tested on positive numbers
:start
Call :input A
Call :input B
Echo(
Echo Calculating %A%*%B%
set /a T=%A%*%B%
Echo Result: %T%
exit /b
:input
rem MAX 2147483647
set "Retry=& pause & goto :input"
set "Out=echo Number exceeds 2147483647 %Retry%"
set Num=
set /p "Num=Insert an integer number>"
if not defined Num set Num=%random%%random% & call echo Random number %%Num%%
(if errorlevel %Num% call ) >nul 2>&1||(Echo Not valid number! %Retry%)
if %Num% geq 2147483647 if not "%Num%"=="2147483647" %Out%
rem if there are other errors...
set /a %1=%Num% 2>nul
if %errorlevel% equ 1073750992 %Out%
set /a %1=Num
exit /b
einstein1969
Re: SET /A overflow
Posted: 07 Apr 2014 16:36
by penpen
This may help you:
Code: Select all
@echo off
set /A "A=-0x10000"
set /A "B= 0x8000"
set /A "A=-1"
set /A "B=-1"
:: needs A <= B
if NOT %A% EQU %B% if NOT %B% EQU 0x7FFFFFFF if %B% LSS %A% (
set "A=%B%"
set "B=%A%"
)
if 0 LSS %A% (set "signum_A=1") else set "signum_A=-1"
if 0 LSS %B% (set "signum_B=1") else set "signum_B=-1"
if %A% == -1 (
set /A "test=0x7FFFFFFF"
) else if NOT %signum_A% == %signum_B% (
set /A "test=0x80000000/A"
) else if %signum_A% == -1 (
set /A "test=0x80000000/A"
) else (
set /A "test=0x7FFFFFFF/A"
)
if %test% EQU 0x7FFFFFFF (
echo no overflow
) else if %B% GEQ %test% (
echo overflow
) else (
echo no overflow
)
goto :eof
penpen
Edit: Corrected the algorithm, because (0x80000000/-1) has no positive representation.
In addition worked around the bug: "if 2147483647 LSS -1 echo true" echoes "true" on windows xp home 32 bit (?!)
Re: SET /A overflow
Posted: 07 Apr 2014 18:20
by einstein1969
thanks! You are a genius!

I'm getting crazy with this:
Code: Select all
>set /a -2147483648
Numero non valido. I numeri possono avere una precisione massima di 32 bit.
>set /a 0x80000000/1
-2147483648
>set /a 0x80000000/-1
Numero non valido. I numeri possono avere una precisione massima di 32 bit.
when i check overflow for A=-1 and B=-1
einstein1969
Re: SET /A overflow
Posted: 08 Apr 2014 02:13
by penpen
Sorry my fault: I've not realized that -0x80000000/-1 has no positive representation.
I also realized that "if 2147483647 LSS -1 echo true" echoes "true", at least on my win xp home 32 bit ?!
I've changed the algorithm to fix both issues.
I hope it is bug free now.
penpen
Re: SET /A overflow
Posted: 08 Apr 2014 16:17
by einstein1969
... so ABS(-2147483648) do not exists?
einstein1969
Re: SET /A overflow
Posted: 09 Apr 2014 12:09
by penpen
It does exist, but it is a 32 bit cyclic subspace of the natural numbers in two's-complement, so --2147483648 == -2147483648
penpen
Re: SET /A overflow
Posted: 09 Apr 2014 16:36
by Aacini
einstein1969 wrote:... so ABS(-2147483648) do not exists?
einstein1969
Yes, that's right: +2147483648 does not exists
as a 32 bits number. We would need at least one bit more in order for the sign bit be zero.
Antonio
Re: SET /A overflow
Posted: 25 Nov 2024 10:58
by einstein1969
penpen wrote: ↑07 Apr 2014 16:36
...
In addition worked around the bug: "if 2147483647 LSS -1 echo true" echoes "true" on windows xp home 32 bit (?!)
but it's not a bug only for "-1" it does it for all the pairs that have the same representation and with the bit sign different. At least it seems to me. On win10
Code: Select all
if 2123153877 lss -24329771 (echo true) else echo false
this return "true"
