Wow! I made an interesting discovery today

but first a very brief introduction:
In assembly language the multiplication of two 32-bits registers give a 64-bits wide result in two registers; for example, 123 times 100:
Code: Select all
mov eax,123 ;EAX = 123
mov ebx,100 ;EBX = 100
mul ebx ;EAX*EBX -> EDX:EAX = 12300
High part of the result goes into EDX and low part in EAX (EDX:EAX notation). The division is exactly the opposite procedure: divide EDX:EAX 64-bits number by a 32-bits divisor, result is EAX=quotient and EDX=remainder. This way, before the division of two 32-bits numbers you must clear the high part of the dividend:
Code: Select all
mov edx,0 ;clear high part of EDX:EAX
mov eax,12345 ;EDX:EAX = 12345
div ebx ;EAX = 123, EDX = 45
When I evaluate a large expression I used to clear EDX just once. I can preserve some precision when a multiplication is followed by a division:
Code: Select all
mov eax,1999999999 ;EAX = 1999999999
mov ebx,1000000000 ;EBX = 1000000000
mul ebx ;EDX:EAX = 1999999999000000000
add eax,ebx ;EDX:EAX = 2000000000000000000
div ebx ;EAX = 2000000000
This way if I carefully choose the order of operations and use the right factors, I can get a correct result of an expression with partial results that exceed a 32-bits number.
Well, it seems that SET /A command
ACHIEVE THE SAME THING!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
if "%1" neq "" cd /D %1
for /F "tokens=3" %%a in ('dir ^| find "free"') do set freeSpace=%%a
set freeSpace=%freeSpace:,=%
set highPart=%freeSpace:~0,-6%
set lowPart=%freeSpace:~-6%
for /L %%a in (1,1,5) do if "!lowPart:~0,1!" equ "0" set lowPart=!lowPart:~1!
set /A GB=(highPart*1000000+lowPart)/1073741824, HH=((highPart*1000000+lowPart)%%1073741824/1000000+5)/10
echo %GB%.%HH% GB of free space
Interesting! Isn't it?

This feature may be used in several other instances...
Antonio
EDIT: Although my description about assembly trick is correct, my conclusion about SET /A command is not...
However, I posted below a new method that correctly works.
