[Gambas-user] Confusion in Data Types
Doriano Blengino
doriano.blengino at ...1909...
Tue Jun 9 06:49:09 CEST 2009
nando ha scritto:
> Float and Double is not as simple an explaination as the Int and others.
> ...
>
> Floating point numbers have two parts.
> Simplistically: One part is an integer which represents the actual digits in binary.
> The other is the value of the exponent (binary point and not decimal point).
>
>
>> ' Single (4-Byte C Float) 1.2e-38 to 3.4e38
>> mySingle = 3.4e+38
>> PRINT "Max value in Single " & mySingle
>> mySingle += 1 '1e+36 ' Issues
>> PRINT "Min value in Single " & mySingle
>>
>> ' Float (4-Byte C Double) 2.2e-308 to 1.8e308
>> 'myFloat = 1.8e+308 BUG: Cant hold this value
>> myFloat = 1.79e+308
>> PRINT "Max value in Float " & myFloat
>> myFloat += 1 ' BUG: DoesNOT Wrap
>> PRINT "Min value in Float " & myFloat
>>
>> END
>>
>> ----------------------------
>>
>> Program Output:
>>
>> ----------------------------
>>
>> RANGES
>> Max value in Single 3.399999952144E+38
>> Min value in Single 3.399999952144E+38
>> Max value in Float 1.79E+308
>> Min value in Float 1E+2147483647
>>
>> ----------------------------
>>
Nando you are right but, anyway, there is something strange here. I
think that the "single" behaviour is correct - if you add 1 to a very
large number, this small added quantity should be so small to be ignored
(rounded away), and this is what the "single" part does.
But the "float" behaviour scares me... floating point numbers should not
overflow via an addition, I think. You can expect overflows and NaN's
when more complex operations are involved, such as dividing by small
numbers, or calculating tangents or other trascendentals (is this right
english?). And, "1E+2147483647" is a strange number...
At this point, just for curiosity, I tried this program in freepascal:
program test;
var
valsingle : single;
valdouble : double;
begin
valsingle := 3.4e+38;
writeln('MAX single: ', valsingle);
valsingle := valsingle+1;
writeln('MIN single: ', valsingle);
valdouble := 1.79e+308;
writeln('MAX double: ', valdouble);
valdouble := valdouble+1;
writeln('MIN double: ', valdouble);
end.
Assigning 1.8e+308 to valdouble does not and give error, but prints out
as "+Inf", so I resorted to use 1.79e+308; the result is:
MAX single: 3.399999952E+38
MIN single: 3.399999952E+38
MAX double: 1.79000000000000E+308
MIN double: 1.79000000000000E+308
Which confirms that there is something strange in KhurramM experiment.
By the way, KhurramM: a double takes 8 bytes, not four.
Regards,
--
Doriano Blengino
"Listen twice before you speak.
This is why we have two ears, but only one mouth."
More information about the User
mailing list