tag:blogger.com,1999:blog-4434108347727659251.post93928496781254045..comments2022-06-29T08:53:03.580+02:00Comments on Compas Pascal: Floating point values are evilUnknownnoreply@blogger.comBlogger10125tag:blogger.com,1999:blog-4434108347727659251.post-88553243986029757702009-06-23T06:39:40.171+02:002009-06-23T06:39:40.171+02:00We have an external system where floating point va...We have an external system where floating point values are delivered using "single" accuracy in variants via a COM interface. However, since they are delivered as floating point variables, there is nothing in the API that specifies, what the actual precision is, so we need to use double to save the values, but when saving single values in double variables, without knowing that they are Lars Dhttps://www.blogger.com/profile/06217303713945608384noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-49438251855973001352009-06-22T23:38:17.874+02:002009-06-22T23:38:17.874+02:00All the other strategies seem to add work but not ...All the other strategies seem to add work but not value - BCD for example is unpleasant to work with excessive accuracy.<br /><br />I have found that using SameValue, IsZero and CompareValue as listed by Lars F normally gives me adequate accuracy. I suppose remembering to always use them is a fundamental thing like keeping in mind objects are held by reference or that events are not multicast andCobus Krugerhttp://sourcecodeadventures.wordpress.com/2009/06/22/whats-wrong-with-floating-point/noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-24343356774915174692009-06-21T23:07:29.783+02:002009-06-21T23:07:29.783+02:00w.r.t fractions, I have a fraction implementation ...w.r.t fractions, I have a fraction implementation that keeps numerator and denominator separate, exactly as you describe (among other things).<br /><br />Quite apart from anything else, you HAVE to do this if you wish to perform arithmetic on fractions with any semblance of accuracy, not least because floating point provides no representation for irrational numbers.<br /><br />Consider:<br /><br Jolyon Smithhttps://www.blogger.com/profile/12189834837787604468noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-65746684004816901302009-06-19T19:18:14.887+02:002009-06-19T19:18:14.887+02:00It might be better to write and regular use FloatC...It might be better to write and regular use FloatCompare functions that specify the degree of similarity (significant figures) to use when determining equality. <br /><br />if FloatCompareDigits(float1,float2,significantdecimals)= 0then<br /> ApproximatelyEqual.Warrenhttps://www.blogger.com/profile/04053407632823479165noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-82519732882721627142009-06-19T18:52:31.178+02:002009-06-19T18:52:31.178+02:00My point with "evil" was, that many prog...My point with "evil" was, that many programmers start to use floating point when they need to store fractional values, even though floating point variables are often not suited for overall goal.<br /><br />I had a good example , where we were doing a comparison of two floating point values, where one was a limit, and the other one was calculated. The only realistic scenario, where the Lars Dhttps://www.blogger.com/profile/06217303713945608384noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-33692074910585232222009-06-19T14:04:57.169+02:002009-06-19T14:04:57.169+02:00"* Instead of storing fractions like factor:=..."* Instead of storing fractions like factor:=height/width, keep storing the height and width in two integer values."<br /><br />This error exists in the .NET framework forms. Having made the same mistake myself, I recognize the odd behavior.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-21091939681881027402009-06-19T13:34:17.196+02:002009-06-19T13:34:17.196+02:00For comparison of floating number, the rtl (math u...For comparison of floating number, the rtl (math unit) comes with a set of helpful functions which allow you to define an epsilon within which two numbers are considered the same:<br /><br />function CompareValue(const A: Extended; const B: Extended; Epsilon: Extended = 0): TValueRelationship; overload;<br /><br />function SameValue(const A: Extended; const B: Extended; Epsilon: Extended = 0): Lars Frischehttps://www.blogger.com/profile/14899067253206576189noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-28347054768127877612009-06-19T13:09:19.140+02:002009-06-19T13:09:19.140+02:00Regarding the height/width example: I've got i...Regarding the height/width example: I've got into the habit of using MulDiv in such cases, e.g.<br />value:=MulDiv(xposition, height, width)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-88582190842948542152009-06-19T12:49:06.952+02:002009-06-19T12:49:06.952+02:00Basically the reason why TBCD and Currency exist ;...Basically the reason why TBCD and Currency exist ;-)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-88817148694409721872009-06-19T12:36:53.140+02:002009-06-19T12:36:53.140+02:00Come on, Floating point values aren't really e...Come on, Floating point values aren't really evil, they were just born that way :-)<br />I'd consider changing the title to "Floating point comparisons are tricky". Or what about: "All floating point values are equal, but some values are more equal than others"?Victornoreply@blogger.com