[fpc-pascal] floating point an FPC

Jerry lanceboyle at qwest.net
Mon Mar 1 23:38:04 CET 2010


On Mar 1, 2010, at 2:32 PM, ik wrote:

>
> http://ik.homelinux.org/
>
>
> On Mon, Mar 1, 2010 at 23:20, Jerry <lanceboyle at qwest.net> wrote:
> Don't compare floating points. Ever. Now you see why.
>
> So if I need for example to compare currency or something that is  
> floating point, how do I do that ?
>
>
> Also, don't write loops that calculate a floating point number which  
> is then tested for equality to exit the loop. Also, it is a bad idea  
> to test for > or < for exiting unless you know in advance an  
> accidental equality result will not yield surprising results. In  
> other other words, don't do that (test for > or <) either.
>
> As I understand, there is an old way to calculate floating point  
> that is very popular and newer ways for that.
> The old way uses binary calculation and the newer I do not  
> understand them :)

The older way that you are thinking of might be binary coded decimal,  
BCD, in which each decimal digit is represented by its own bit string,  
usually four bits, to represent the digits 0–9. It is rarely used any  
more. I'm not sure but maybe Hewlett-Packard calculators use BCD. (HP  
calculators are known for their excellent numerical accuracy. The  
academic who did a lot of their work was also involved in the IEEE 754  
standard, I think.)

The newer way that you are thinking of is probably IEEE 754 floating  
point which is implemented widely on modern microprocessors. Its whole  
numbers (1.0, 2.0, etc.) are supposedly exact equivalents to integer  
counterparts up to a very large number, but you had _really_ better  
know what you are doing if you depend on that for comparisons. And  
expect portability problems when moving to non-IEEE 754 platforms.

BTW, the way to write a loop that increments a floating point number  
properly is this (Please pardon if any Ada syntax slips in here):

for i := 0 .. 100 begin
	a_float := 1.23 * i;
end;


whereas this is a bad idea:

a := 0.0;
while a <= 100.0 do
	a_float := 1.23 * i;
	a := a + 1.0;
end;


as is this:


a := 0.0;
while a <= 123.0 do
	a := a + 1.23;
end;

Jerry
>
>
>
> Jerry
>
> On Feb 26, 2010, at 3:37 AM, ik wrote:
>
>> Hello,
>>
>> I've made a small test:
>>
>> if (0.1+0.2) = 0.3 then
>>
>> The only compiler/interpator that actually tells that it equal is  
>> FPC.
>>
>> I've tested it using Javascript (in Firefox), Ruby, Python, C (gcc)  
>> and Perl.
>>
>> Except FPC, everyone tells that 0.1+0.2 = 0.30000000000000004
>>
>> Can someone explain how FPC see the result and why others does not  
>> see it like that ?
>>
>>
>> Thanks,
>> Ido
>>
>> http://ik.homelinux.org/
>> _______________________________________________
>> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>
> Ido
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20100301/51b1fa38/attachment.html>


More information about the fpc-pascal mailing list