首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在计算之前找出未知的“正确”值

在计算之前找出未知的“正确”值
EN

Stack Overflow用户
提问于 2013-10-29 12:25:11
回答 4查看 154关注 0票数 0

问题是,我有一个大的或小的数字(可以是其中的一个),我需要调整这个数字并进行计算。考虑到计算的结果,它至少要在小数点5的时候算出一定的值。

所以我需要做一个方法,取这个起始值,在得到正确的结果之前,根据当前的结果,尝试增加或减少它。我犯了一些错误,但没有成功。

这里有一个根本不讨好的例子,但它暗示了我的意思.(这只是一个小规模的测试用例)

代码语言:javascript
复制
   public class Test {
        public static void main(String[]args)
        {
            double ran = 100 + (int)(Math.random() * 100000.999999999);
            int count = 0;              
            double tmpPay = 3666.545;
            double top = tmpPay;
            double low = 0;             

            while ( tmpPay != ran )
            {
                if ( tmpPay > ran)
                {
                    if( low == 0)
                    {
                            tmpPay = top / 2;
                            top = tmpPay;
                    }
                    else
                    {
                        tmpPay = tmpPay + ((top - low) / 2);
                        top = tmpPay;
                    }        
                }           

                if (tmpPay  < ran)
                {
                    tmpPay = top * 1.5;
                    low = top;
                    top = tmpPay;                   
                }
            }
            System.out.println(" VAlue of RAN: " +ran + "----VALUE OF tmpPay: " + tmpPay + "---------- COUNTER: " + count);         
}

示例2 mabey有一个更清晰的描述。这是我现在的解决方案..。

代码语言:javascript
复制
guessingValue = firstImput;

while (amortization > tmpPV)
{
    guessingValue -= (decimal)1;
    //guessingVlue -- > blackbox
    amortization = blackboxResults;
}
 while (amortization < tmpPV)
{
    guessingValue += (decimal)0.00001;
    //guessingVlue -- > blackbox
    amortization = blackboxResults;
}

}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-10-29 13:32:54

正如我在上面的评论中已经提到的,您不应该使用内置操作符来比较加倍。这是您的代码无法工作的主要原因。第二种是tmpPay = tmpPay +((顶-低) /2 ),而不是tmpPay = tmpPay -((顶-低) /2);

完整的固定代码如下:

代码语言:javascript
复制
public class Test {
    private static final double EPSILON = 0.00001;
    public static boolean isEqual( double a, double b){

        return (Math.abs(a - b) < EPSILON);

    }


    public static void main(String[]args)
    {

        double ran = 100 + (int)(Math.random() * 100000.999999999);
        int count = 0;              
        double tmpPay = 3666.545;
        double top = tmpPay;
        double low = 0;             

        while ( !isEqual(tmpPay, ran))
        {
            if ( tmpPay > ran)
            {
                if( isEqual(low, 0.0))
                {
                        tmpPay = top / 2;
                        top = tmpPay;
                }
                else
                {
                    tmpPay = tmpPay - ((top - low) / 2);
                    top = tmpPay;
                }        
            }           

            if (tmpPay  < ran)
            {
                tmpPay = top * 1.5;
                low = top;
                top = tmpPay;                   
            }
            System.out.println("RAN:"+ran+" tmpPay:"+tmpPay+" top:"+top+" low:"+low+" counter:"+count);
            count++;
        }
        System.out.println(" VAlue of RAN: " +ran + "----VALUE OF tmpPay: " + tmpPay + "---------- COUNTER: " + count);




    }
}
票数 1
EN

Stack Overflow用户

发布于 2013-10-29 12:43:48

一种方法是将您的问题定义为局部优化任务,并使用本地优化器(例如Brent的方法或来自阿帕奇公域的Nelder )。

这里的目标函数是期望的值和从黑匣子中得到的值之间的距离。

票数 1
EN

Stack Overflow用户

发布于 2013-10-29 12:45:50

如果我正确理解,您有一个函数g(x)和一个值K,您想要找到x0,使g(x0) = K,这相当于找到函数f(x) = g(x) -K的根,因为f(x0) == f(x0) -K == K == 0。

一个简单的算法是牛顿法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19658162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档