[fpc-pascal] Freepascal Floating Point Issue

James Richters james at productionautomation.net
Thu Aug 24 14:00:15 CEST 2017


Thanks everyone for the explanation and solutions.  I can work around it well enough now that I understand what’s happening, but I’m still curious if there is a way to define fixed point variables?  In a C++ Project I was helping with recently, they had a way of defining a variable as having 2 places before the decimal and 30 places after?  Since the decimal could never move (float) It ended up being much better defined.   It’s like always moving the decimal over 30 places to the right, using integer math, then moving it back.  I’m not sure if this was something the compiler did, or if maybe they had their own functions to do this… I had a difficult time following how things worked, but I do remember the fixed point variables.  Anyway I thought I would just ask the question in case there is a way to define this in FreePascal

 

It seems to me that if you know you never need more than a fixed number of places before and after the decimal, then moving the decimal over the maximum number of places, performing integer math, which would round to the nearest integer(and cut off the useless garbage), then moving the decimal back would mean a fixed amount of precision with the benefit that one could do things like exact conditionals and get the expected result

 

James

 

From: fpc-pascal [mailto:fpc-pascal-bounces at lists.freepascal.org] On Behalf Of Sven Barth via fpc-pascal
Sent: Tuesday, August 22, 2017 1:55 AM
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
Cc: Sven Barth <pascaldragon at googlemail.com>
Subject: Re: [fpc-pascal] Freepascal Floating Point Issue

 

Am 22.08.2017 00:02 schrieb "James Richters" <james at productionautomation.net <mailto:james at productionautomation.net> >:

The others already wrote about floating point precision, so I won't repeat that.

> The reason I noticed this is because I have some conditional statements like
> If Draw_GX_Min<>99.999 then
>     Something....

*DON'T* compare floating point numbers like that. Instead use SameValue() from the Math unit which has an additional epsilon value (with a default value) that defines a lower and upper bound from your desired value to compare to.

Regards,
Sven

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170824/42df5799/attachment.html>


More information about the fpc-pascal mailing list