tracker issue : CF-3959342

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

Weird NumberFormat Bug

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/AsDesigned

Reporter/Name(from Bugbase): Dave Cordes / Dave Cordes (Dave Cordes)

Created: 03/25/2015

Components: Language

Versions: 11.0

Failure Type: Data Corruption

Found In Build/Fixed In Build: CF11_Final /

Priority/Frequency: Critical / All users will encounter

Locale/System: English / Mac 10.10 64 bit

Vote Count: 0

Problem Description:

I believe I just found a bug with the NumberFormat function but this only happens when you add 2 numbers together and the second number ends in .01 or .99. Yeah I know, really weird right?


Steps to Reproduce:

Say, for example, you have 2 numbers you want to add together the result of which comes to 19.94.

<cfset OrderSubtotal = 15.95>

<cfset OrderShipping = 3.99>

<cfset OrderTotal = OrderSubtotal + OrderShipping>

If you run the NumberFormat function on this number and then run a comparison, you get some strange results.

<cfset OrderTotalFormatted = Trim(NumberFormat(OrderTotal,"999999.99"))>

Order Total = #OrderTotal#

Order Total Formatted = #OrderTotalFormatted#

<cfif OrderTotal NEQ OrderTotalFormatted>
	#OrderTotal# - #OrderTotalFormatted# = #OrderTotal-OrderTotalFormatted#
</cfif>


Actual Result:

Order Total = 19.94

Order Total Formatted = 19.94

19.94 - 19.94 = -3.5527136788E-015


Expected Result:

Order Total = 19.94

Order Total Formatted = 19.94

19.94 - 19.94 = 0


Any Workarounds:

There are no workarounds that I know of at the moment.

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

Watson Bug ID:	3959342

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



System Information

Server Details

Server Product	ColdFusion

Version	11,0,02,291725

Tomcat Version	7.0.52.0

Edition	Enterprise  

Operating System	Windows Server 2008 R2  

OS Version	6.1  

Update Level	D:/ColdFusion11/cfusion/lib/updates/chf11000002.jar  

Adobe Driver Version	5.1.1 (Build 0001)  

JVM Details

Java Version	1.7.0_71  

Java Vendor	Oracle Corporation  

Java Vendor URL	http://java.oracle.com/

Java Home	D:\Java\jdk1.7.0_71\jre

Attachments:

Comments:

Hi Dave, Please try precisionEvaluate() around the floating point arithmetic. <cfset OrderTotal = precisionEvaluate(OrderSubtotal + OrderShipping)> Thanks!, -Aaron
Comment by External U.
7976 | May 14, 2015 07:18:16 PM GMT
Hi Aaron, Thanks for the workaround. It seems to be working. Do you know if this a bug I found or is this expected behavior?
Comment by External U.
7977 | May 15, 2015 11:32:43 AM GMT
Hi Dave, You're very welcome. Some floating point numbers (such as .01) can't be represented in binary, so precisionEvaluate() offers a higher-than-CF's-default level of precision when doing math on floating point numbers. Here is a blog entry related to this: http://www.bennadel.com/blog/2044-using-coldfusion-s-precisionevaluate-function-to-perform-big-integer-math.htm Thanks!, -Aaron
Comment by External U.
7978 | May 15, 2015 12:54:26 PM GMT
Hi Dave, This behavior is seen due to dependency on Java JDK, and is not an issue caused due to ColdFusion. Also as Aaron has pointed out, using the function precisionEvaluate() is a workaround for this. Hence this is an expected behavior. Please do mention if you have any other concerns. Thanks, Preethi
Comment by S P.
7979 | June 05, 2015 03:56:08 AM GMT
Hi Preethi, Thanks for your reply. Is this behavior fixed in Java 1.8?
Comment by External U.
7980 | June 05, 2015 10:35:26 AM GMT
There's nothing really to *fix* here. It's just the way floating point operations work. As a side note: you should only ever use numberFormat() as the last step as you output your number. You should not be performing more operations on the string that comes back from the formatting operation. Also what you're demonstrating is nothing to do with numberFormat(), given numberFormat() is explicitly intended for dealing with these floating point inaccuracies, you're basically demonstrating that it's doing exactly what it's supposed to do. You ought to read up on floating point inaccuracies. -- Adam
Comment by External U.
7981 | June 06, 2015 04:05:44 AM GMT
Closing the bug, since it is the expected behavior. Do let us know if you have any concerns regarding the same. Thanks!
Comment by S P.
7982 | August 25, 2015 01:33:40 AM GMT