tracker issue : CF-4191694

select a category, or use search below
(searches all categories and all time range)
Title:

EQ fails on strange values

| View in Tracker

Status/Resolution/Reason: To Fix//

Reporter/Name(from Bugbase): Beau Gibson / Beau Gibson (Beau Gibson)

Created: 09/21/2016

Components: Language, Functions

Versions: 2016

Failure Type:

Found In Build/Fixed In Build: CF2016_Final /

Priority/Frequency: Major / Some users will encounter

Locale/System: ALL / Platforms All

Vote Count: 0

Problem Description:
The eq operator is not working as expected when comparing certain values.

Seems like this is could be a rounding error


Steps to Reproduce:
<cfset val1 = 14030 />
<cfset val2 = 140.30 * 100 />
#val1# eq #val2#? #val1 eq val2#


Actual Result:

14030 eq 14030? NO


Expected Result:

14030 eq 14030? YES


Any Workarounds:

Wrapping the (140.30 * 100) in a VAL() makes it work as expected.

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	4191694

External Customer Info:
External Company:  
External Customer Name: Beau Gibson
External Customer Email:  
External Test Config: My Hardware and Environment details:



Coldfusion 9 on windows Server 2008

Coldfusion 2016 on Microsoft Windows 10



Occurs on every version using www.trycf.com/scratch-pad

Attachments:

Comments:

Running this shows it occurs for alot of values (around 1/3) <cfloop from="100" to="20000" index="i"> <cfset val1 = (i / 100) * 100 /> <cfif i neq val1> #i# eq #val1#? #i eq val1#<br /> </cfif> </cfloop>
Comment by External U.
1756 | September 21, 2016 06:05:23 PM GMT
Its because its actually doing this: 14030 eq 14030.000000000002? false If you run the same thing in Lucee it shows a lot of the numbers have decimal places which means it doesnt match anymore. Coldfusion doesnt show these decimals. This is "Floating point arithmetic inaccuracy" Adam Cameron already wrote an article about this "issue" http://blog.adamcameron.me/2016/01/floating-point-arithmetic-with-decimals.html
Comment by External U.
1757 | September 23, 2016 08:44:53 AM GMT
Here the Number is double-precision 64-bit IEEE 754 (used by hardware) floating point numbers. Unless those are power of 2 or sums of powers of 2, they cannot be represented exactly, even if they have high precision. Some floating point operations will compound the round-off error present in these floating point numbers. For this reason, precision could change and shouldn't be rely upon. In any hardware that is the case. For precise arithmetic operations, you have to use PrecisionEvaluate function which uses BigDecimal format. <cfset val2 = PrecisionEvaluate(140.30 * 100) /> The documentation for this here- https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/precisionevaluate.html https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/using-coldfusion-variables/data-types-developing-guide.html
Comment by Krishna R.
1758 | September 30, 2016 03:40:08 AM GMT