3

I'm a bit confused about following behaviour of the int()-function in ColdFusion 10 (10,0,20,282462):

<cfset dummy = 100 - (5859 / (6510 / 100)) />
<cfoutput>
    dummy = #dummy#<br><br> <!--- 10 --->
    int(10) = #int(10)#<br> <!--- 10 --->
    int(dummy) = #int(dummy)# <!--- 9 --->
</cfoutput>

Can anybody explain me why int(dummy) returns 9 instead of 10?

Seybsen
  • 14,989
  • 4
  • 40
  • 73
  • 3
    dummy is actually a float. It's simply getting displayed as an integer. Output it with a number format to see the real value. – Dan Bracuk Jan 31 '17 at 15:54
  • 1
    To confirm that it a float, http://stackoverflow.com/questions/11208155/coldfusion-get-variable-type – James A Mohler Jan 31 '17 at 15:55
  • Maybe round and then use Javacast? This will return 10. – James Moberg Jan 31 '17 at 17:48
  • As with most numbers, CF stores the result of the division an approximate type: java.lang.Double and [decimal numbers that "look" round in base 10, are not exactly representable in base 2](http://stackoverflow.com/a/2002837/104223). In this case [the real value is ~ 9.999999999999986](http://trycf.com/gist/4e8e89c5799fd1e13c490dddaae70b82/acf?theme=monokai), which when rounded down equals 9, not 10. – Leigh Jan 31 '17 at 18:18

1 Answers1

0

int(dummy) returns 9 instead of 10 because it is essentially floor() in other languages, and your answer may become 9 because for performance, by default, they are treated as double.

Have you heard of PrecisionEvaluate() https://cfdocs.org/precisionevaluate

dummy = PrecisionEvaluate(100 - (5859 / (6510 / 100)));
writeOutput(dummy);

you'll get 10 as expected

Henry
  • 32,689
  • 19
  • 120
  • 221