2012-02-06 52 views
0

我試圖檢測我是否已經乘以複數分數的分子和分母足夠多的時間(到10/10),以獲得整數在頂部和底部。通常我的算法有效。但有時候,Int()函數不能按預期工作,我從中得到大量數字。下面是我跑什麼的簡化版本:整數NEQ Int(整數)Craziness

<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" 

我在做什麼錯,我該如何彌補?

回答

-1

纈氨酸()似乎糾正它太:

<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> 
+0

''解決問題並獲得最少的循環時間。 – 2012-02-07 13:32:11

+0

@Dave - 我對此有點懷疑;)PrecisionEvaluate通常會導致更少的迭代,因爲它不使用像9.183999999這樣的近似數字。因此,評估9.184需要四(4)次迭代,而不是~13(13)'val'。也就是說,除非您處理數百或數千個數字,否則這並不是一個顯着的差異。 – Leigh 2012-02-07 17:58:27

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> 

+0

This:'「仍然獲得巨大的數字。 – 2012-02-07 13:29:48

2

實數= 「9.184E + 015」

輸出是欺騙性的。如果您使用#RealNumber.toString()#,則實際值可能類似於9.183999999999998E15。 CF對大多數數學運算使用近似類型Double。所以你看到的是floating point numbers的正常行爲。如果您需要更高的準確度,請使用PrecisionEvaluate。它使用BigDecimals進行算術運算(因爲它們比java.lang.Double更精確)。

+0

'>仍然獲得巨大的數字。 – 2012-02-07 13:31:08

+0

這是因爲你必須在兩個值上使用它,例如'precisionEvaluate(RealNumber * 10)'。否則當你執行數學運算時,CF會回退到使用浮點數。 – Leigh 2012-02-07 17:36:30