我正试图检测我是否将复数分数的分子和分母乘以足够的倍数(按10/10)以获得顶部和底部的整数。通常我的算法有效。但有时,Int()
函数不能按预期工作,我从中获得了大量数据。这是我正在运行的简化版本:
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
<cfloop condition="RealNumber NEQ Int(RealNumber)">
<cfset RealNumber = RealNumber * 10 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
</cfloop>
</cfoutput>
大部分时间这都是我想要的,但有时看起来像这样:
RealNumber = "9.184" Int(RealNumber) = "9"
RealNumber = "91.84" Int(RealNumber) = "91"
RealNumber = "918.4" Int(RealNumber) = "918"
RealNumber = "9184" Int(RealNumber) = "9183"
RealNumber = "91840" Int(RealNumber) = "91839"
RealNumber = "918400" Int(RealNumber) = "918399"
RealNumber = "9184000" Int(RealNumber) = "9183999"
RealNumber = "91840000" Int(RealNumber) = "91839999"
RealNumber = "918400000" Int(RealNumber) = "918399999"
RealNumber = "9184000000" Int(RealNumber) = "9183999999"
RealNumber = "91840000000" Int(RealNumber) = "91839999999"
RealNumber = "918400000000" Int(RealNumber) = "918399999999"
RealNumber = "9.184E+012" Int(RealNumber) = "9.184E+012"
RealNumber = "9.184E+013" Int(RealNumber) = "9.184E+013"
RealNumber = "9.184E+014" Int(RealNumber) = "9.184E+014"
RealNumber = "9.184E+015" Int(RealNumber) = "9.184E+015"
或者甚至喜欢这样:
RealNumber = "2.152" Int(RealNumber) = "2"
RealNumber = "21.52" Int(RealNumber) = "21"
RealNumber = "215.2" Int(RealNumber) = "215"
RealNumber = "2152" Int(RealNumber) = "2152"
RealNumber = "21520" Int(RealNumber) = "21520"
RealNumber = "215200" Int(RealNumber) = "215200"
RealNumber = "2152000" Int(RealNumber) = "2152000"
RealNumber = "21520000" Int(RealNumber) = "21520000"
RealNumber = "215200000" Int(RealNumber) = "215200000"
RealNumber = "2152000000" Int(RealNumber) = "2152000000"
RealNumber = "21520000000" Int(RealNumber) = "21520000000"
RealNumber = "215200000000" Int(RealNumber) = "215200000000"
RealNumber = "2.152E+012" Int(RealNumber) = "2.152E+012"
RealNumber = "2.152E+013" Int(RealNumber) = "2.152E+013"
RealNumber = "2.152E+014" Int(RealNumber) = "2.152E+014"
RealNumber = "2.152E+015" Int(RealNumber) = "2.152E+015"
RealNumber = "2.152E+016" Int(RealNumber) = "2.152E+016"
我做错了什么以及如何弥补这一点?
答案 0 :(得分:2)
RealNumber =“9.184E + 015”
输出具有欺骗性。如果您使用#RealNumber.toString()#
,则实际值可能类似于9.183999999999998E15
。 CF对大多数数学运算使用近似类型Double。所以你看到的是floating point numbers的正常行为。如果您需要更高的准确度,请使用PrecisionEvaluate。它使用BigDecimals进行算术运算(因为它们比java.lang.Double更精确)。
答案 1 :(得分:0)
尝试使用javacast .. ColdFusion正在尝试动态确定你的变量类型并且做得不好.Javacast涵盖了很多罪行。
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
<cfloop condition="RealNumber NEQ Int(RealNumber)">
<cfset RealNumber = RealNumber * 10 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#javaCast("int", RealNumber)#"<br />
</cfloop>
答案 2 :(得分:-1)
Val()似乎也纠正了它:
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
"#val(RealNumber)#" = INT: "#val(Int(RealNumber))#"<br />
<cfloop condition="val(RealNumber) NEQ Val(Int(RealNumber))">
<cfset RealNumber = RealNumber * 10 />
"#val(RealNumber)#" INT: "#val(Int(RealNumber))#"<br />
</cfloop>
</cfoutput>