[Gambas-user] Variable overflow questions

Doriano Blengino doriano.blengino at ...1909...
Wed Nov 3 10:26:23 CET 2010


user ha scritto:
> Hi i check the overflow of variables and there is something i dont
> understand.
>
> My system is
>
> user at ...2493...:~$ cat /etc/*release*
> DISTRIB_ID=Ubuntu
> DISTRIB_RELEASE=10.04
> DISTRIB_CODENAME=lucid
> DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"
>
> Linux ubuntu-desktop 2.6.32-25-generic #45-Ubuntu SMP Sat Oct 16
> 19:52:42 UTC 2010 x86_64 GNU/Linux
>
>
>
> I have this small code
> -----------------------------------
> ' Gambas class file
> PUBLIC SUB Form_Open()
>
> DIM bVar AS Boolean = TRUE
> DIM iNum AS Byte = 255
> DIM iA AS Short = 32767
> DIM iRoads AS Integer = 2147483647
> DIM iBig AS Long = 9223372036854775807
> DIM iArea AS Single = 1.7014118E+38
> DIM fPi AS Float = 8.98846567431105E+307
>
> bVar += 1
> iNum += 1
> iA += 1
> iRoads += 1
> iBig += 1
> iArea += 1
> fPi += 1
>
> PRINT bVar
> PRINT iNum
> PRINT iA
> PRINT iRoads
> PRINT iBig
> PRINT iArea
> PRINT fPi
>
> END
> -----------------------------------
>
> And i get the following result
>
> False
> 0
> -32768
> -2147483648
> -9223372036854775808
> 1.701411834605E+38
> 8.988465674311E+307
>
>
> My question is why the Single and Float do not overflow?
>    
Because they should not overflow, I think. "Real" numbers (in the IT 
sense: "float" if you prefer ) are not discrete numbers, they have a 
limited precision; a single bit pattern in a float corresponds to 
infinite numbers in the real world. When you add a very small quantity 
to a float (and "1", compared to 1E+38, is very small), you simply tell 
it to store a different number, which has the same bit representation. 
If a float would overflow, it would cease its main reason to exist: in 
most situations you *do* want it to behave so. You could add a bigger 
number to make it overflow: you should add a number big enough to fall 
in the precision bits of the mantissa. A normal float has perhaps 6/7 
digits of precision; to choose a comparable a number, you should choose 
something like 1E+31 (this "1" falls in the digits of precision of 
1E+38). But! Even in that case, the number should not overflow, but 
raise an error (overflow error). This last thing depends on 
implementation of the floating point by the host computer.

Regards,
Doriano






More information about the User mailing list