These are the pitfalls of a dynamically typed language, and FLOAT/INT 
conversions.

Your number1 is a string, number2 is a Double, and your number 3 is a 
string:

<cfoutput>
number3 is #number3.getClass().getName()#<br />
number2 is #number2.getClass().getName()#<br />
number1 is #number1.getClass().getName()#<br />
</cfoutput>

If you do val() they will equal out:

<cfif val(number3) eq val(number2)>Number3 equals number2<BR>
<cfelse>Number3 does NOT equal number2<BR></cfif>

 From http://docs.sun.com/source/806-3568/ncg_goldberg.html#680, "the 
result of a floating-point calculation must often be rounded in order to 
fit back into its finite representation. This rounding error is the 
characteristic feature of floating-point computation."

Checkout this code:

<cfoutput>
         #(0.1 * 0.1) EQ 0.01# <!--- not equal! --->
         #0.1 * 0.1# <!--- 0.1 multiplied by 0.1 is actually 0.01, so 
why aren't they equal? --->
         #javacast("bigDecimal", 0.1 * 0.1)# <!--- results in 
0.010000000000000002 --->
</cfoutput>

If you need strict computation, stick to Doubles or bigDecimals all the 
time, and don't rely on implicit conversions.

Also this is not a CF thing or a Java thing, other languages plague this 
too i.e Python (for division, 3/4 is different from 3.0/4.0) and .NET 
(http://stefanoricciardi.com/2010/03/02/comparing-floating-point-numbers/), 
and PHP 
(http://it.slashdot.org/story/11/01/06/1820208/PHP-Floating-Point-Bug-Crashes-Servers
 
<http://it.slashdot.org/story/11/01/06/1820208/PHP-Floating-Point-Bug-Crashes-Servers?from=rss>
 
or just start typing in google instant, php floating... and you'll see 
all the suggestions hehe)

On 2/3/2011 12:56 PM, Jen McVicker wrote:
> number1 (evaluate(12.5 * 1.1)): 13.75
> number2 (12.5 * 1.1): 13.75
> number3 (13.75): 13.75
> 13.75 - 13.75: 0
> 13.75 - 13.75: -1.7763568394E-015
> Number3 equals number1
> Number3 does NOT equal number2
>
>
>
> Obviously number2 is set to a reference of the expression rather than the
> actual value that is returned.  But since the variable outputs as 13.75, why
> does it make a difference?  Can someone explain it to me in small words so
> that I will understand?  ;-)
>
>
>
> Jen Perkins McVicker
> Adobe Certified ColdFusion Developer
> Email: [email protected]
>
> Linked In: http://www.linkedin.com/in/jmcvicker
>
>
>
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:341853
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to