[fpc-pascal] Freepascal Floating Point Issue

Ralf Quint freedos.la at gmail.com
Wed Aug 23 00:56:31 CEST 2017


On 8/22/2017 1:39 PM, Mark Morgan Lloyd wrote:
> On 21/08/17 22:15, Ralf Quint wrote:
>> On 8/21/2017 3:02 PM, James Richters wrote:> I am having an issue
>> with a simple floating point application.  I am setting a variable to
>> a specific value and immediately after I set it,  it is not exactly
>> what I set it to.  Here's an example>>    Draw_GX_Min:=999.999;>   
>> Writeln(Draw_GX_Min:3:30);>> The writeln results in
>> 999.999000000000020000000000000000  >> Why is it not 
>> 999.999000000000000000000000000000  where did 0.00000000000002 come
>> from?>Out of thin air... Well, kind of. Double floating point means
>> 16 digitsof precision, so when you force a 30 digit precision output,
>> anythingpast 16 digits is random garbage, at best...
>
> And in any event, that's probably much more precision than the GPU is
> using to generate the final image :-)
>
Well, older GPUs (at least NVidia, pretty sure similar restrictions
apply to AMD) use(d) only 32bit "single" floats, giving a 7 digit
precision, though newer ones can also handle 64bit doubles. But there
are quite a few differences in how certain FP operations are handled on
those GPUs, which result in even doubles only having 14 (instead of 16)
digits of precision at best. So while NVidia keeps mentioning IEEE754,
their GPUs are in practice not 100% compliant.

As I mentioned before, if someone needs to work a lot with floating
point arithmetic, it really helps to get yourself acquainted to the way
those works and all the possible pitfalls.

Btw, anyone know about a BCD math implementation for Free Pascal, like
it used to be implemented in DR CBASIC? (those were the days... ;-) )

Ralf



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus




More information about the fpc-pascal mailing list