整数 NEQ Int(Integer) 疯狂

发布于 2025-01-03 15:16:55 字数 2402 浏览 0 评论 0原文

我试图检测是否已将复分数的分子和分母相乘足够多次(乘以 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"

我做错了什么以及如何弥补?

I'm trying to detect whether I've multiplied the numerator and denominator of a complex fraction enough times (by 10/10) to get integers on both top and bottom. Often my algorithm works. Sometimes, though, the Int() function doesn't work as expected and I get huge numbers out of it. Here is a simplified version of what I'm running:

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

Most of the time this gets what I want, but sometimes it looks like this:

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"

Or even like this:

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"

What am I doing wrong and how do I compensate for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪眸﹌ 2025-01-10 15:16:55

实数 = "9.184E+015"

输出具有欺骗性。如果您使用#RealNumber.toString()#,实际值可能类似于9.183999999999998E15。 CF 对于大多数情况使用近似类型 Double数学运算。因此,您所看到的是浮点数的正常行为。如果您需要更高的准确度,请使用 PrecisionEvaluate。它使用 BigDecimals 进行算术运算(因为它们比 java.lang.Double 更精确)。

RealNumber = "9.184E+015"

The output is deceptive. If you use #RealNumber.toString()# the real value is probably something like 9.183999999999998E15. CF uses the approximate type Double for most mathematical operations. So what you are seeing is normal behavior for floating point numbers. If you need greater accuracy, use PrecisionEvaluate. It makes use of BigDecimals for arithmetic operations (because they are more precise than java.lang.Double).

玩心态 2025-01-10 15:16:55

尝试使用 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>

Try using javacast.. ColdFusion is trying to dynamically determine your variable type and is not doing a great job.. Javacast covers a multitude of sins.

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

昇り龍 2025-01-10 15:16:55

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>

Val() seems to correct it too:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文