0

Possible Duplicate:
Why does this subtraction not equal zero?

I just came across a weird situation. Why is below code not returning 0? Instead it's returning a very small negative number.

<cfset x = 5448.10-3311.23-2136.87>
<cfoutput>x=#x#</cfoutput>

Above code will output: x=4.54747350886E-013

I ran above code both in CF9 and CF10 with the same results.

Any help is greatly appreciated.

4

2 回答 2

2
<cfset x = PrecisionEvaluate(5448.10-3311.23-2136.87)>
<cfoutput>x=#x#</cfoutput>

Doc for PrecisionEvaluate(): http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fd9.html

于 2012-05-03T21:53:24.910 回答
0

As others have stated it is Floating point accuracy related. I just wanted to point out a resolution if you are trying to get a reasonable result

<cfset x1 = 5448.19-3311.23-2136.87 />
<cfset x2 = numberformat(x1, "9.99") />
<cfoutput>x1=#x1#<br />x2=#x2#</cfoutput>

Result

x1=0.0899999999997
x2=0.09

The numberformat function will round the number to the specified decimal place when given mask.

于 2012-05-03T18:36:42.670 回答