SICP寻找平方根的方法

huangapple go评论68阅读模式
英文:

SICP method to find squareRoot

问题

public class Newton {
    public static double sqrt(double x) {
        return sqrtIter(1, x);
    }

    public static double sqrtIter(double guess, double x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must not be negative.");
        }
        while (true) {
            if (goodEnough(guess, x)) {
                return guess;
            } else {
                guess = improve(guess, x);
            }
        }
    }

    public static double improve(double guess, double x) {
        return average(guess, x / guess);
    }

    public static double average(double x, double y) {
        return (x + y) / 2;
    }

    public static boolean goodEnough(double guess, double x) {
        return Math.abs(guess * guess - x) < 0.0001;
    }
}

请注意,我已经将代码中的错误进行了修正。如果你有任何其他问题,都可以随时问我。

英文:

Suppose such a program to find square-root:

def sqrt(x):
    return sqrt_iter(1, x)
def sqrt_iter(guess, x):
    if x &lt; 0: raise ValueError(&quot;n must not be negative.&quot;)
    while True:
        if good_enough_p(guess, x):
            return guess
        else:
            guess = improve(guess, x) def improve(guess, x):
    return average(guess, x/guess)
def good_enough_p(guess, x):
    return abs(guess**2 - x) &lt; 0.00001
def average(x, y):
    return (x + y) / 2

I tried to re-write it in java as:

public class Newton {
    public static double sqrt(double x) {
        return sqrtIter(1, x);
    }
    public double sqrtIter(double guess, double x) {
        if (x &lt; 0) {
            throw new IllegalArgumentException(&quot;x must not be negative.&quot;);
        }
        while (true) {
            if (goodEnough(double guess, double x)) {
                return guess;
            } else {
                guess = improve(guess, x);
            }
        }
        public double improve(double guess, double x) {
            return average(guess, x/guess);
        }
        public double average(double x, double y) {
            return (x + y) / 2;
        }
        public boolean goodEnough(double guess, double x) {
            return Math.abs(guess*guess -x) &lt; 0.0001;
        }
    }
}

However, it report 6 errors:

Newton.java:10: error: &#39;.class&#39; expected
            if (goodEnough(double guess, double x)) {
                                  ^
Newton.java:10: error: &#39;;&#39; expected
            if (goodEnough(double guess, double x)) {
                                       ^
Newton.java:10: error: &#39;;&#39; expected
            if (goodEnough(double guess, double x)) {
                                                 ^
Newton.java:12: error: &#39;else&#39; without &#39;if&#39;
            } else {
              ^
Newton.java:16: error: illegal start of expression
        public double improve(double guess, double x) {
        ^
Newton.java:26: error: class, interface, or enum expected
}
^
6 errors

I' afraid that have to finish reading a book such as Core-Java-Volumn before could correct them.

Could you please give some hints to correct the program?

答案1

得分: 3

总体而言,你的 Java 翻译几乎是正确的。
问题在于在调用方法时需要一个实例,并且需要去除类型。

public class Newton {
    public static void main(String args[]) {
        Newton instance = new Newton();
        System.out.println(instance.sqrt(4.33));
    }

    public double sqrt(double x) {
        return sqrtIter(1, x);
    }

    public double sqrtIter(double guess, double x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must not be negative.");
        }
        while (true) {
            if (goodEnough(guess, x)) {
                return guess;
            } else {
                guess = improve(guess, x);
            }
        }
    }

    public double improve(double guess, double x) {
        return average(guess, x / guess);
    }

    public double average(double x, double y) {
        return (x + y) / 2;
    }

    public boolean goodEnough(double guess, double x) {
        return Math.abs(guess * guess - x) < 0.0001;
    }
}

输出结果:

2.080865423875971
英文:

Overall your translation with java was almost fine.
The issues were that you need an instance and remove types when call a method.

public class Newton {
	public static void main(String args[]) {
		Newton instance = new Newton();
		System.out.println(instance.sqrt(4.33));
	}

	public double sqrt(double x) {
		return sqrtIter(1, x);
	}

	public double sqrtIter(double guess, double x) {
		if (x &lt; 0) {
			throw new IllegalArgumentException(&quot;x must not be negative.&quot;);
		}
		while (true) {
			if (goodEnough(guess, x)) {
				return guess;
			} else {
				guess = improve(guess, x);
			}
		}
	}

	public double improve(double guess, double x) {
		return average(guess, x / guess);
	}

	public double average(double x, double y) {
		return (x + y) / 2;
	}

	public boolean goodEnough(double guess, double x) {
		return Math.abs(guess * guess - x) &lt; 0.0001;
	}

}

Output

2.080865423875971

答案2

得分: 1

在Java中,静态上下文只能从静态上下文中调用。

尝试

public class Newton {
    public static double sqrt(double x) {
        return sqrtIter(1, x);
    }
    public static double sqrtIter(double guess, double x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must not be negative.");
        }
        while (true) {
            if (goodEnough(guess, x)) {
                return guess;
            } else {
                guess = improve(guess, x);
            }
        }
    }
    static public double improve(double guess, double x) {
        return average(guess, x / guess);
    }
    static public double average(double x, double y) {
        return (x + y) / 2;
    }
    static public boolean goodEnough(double guess, double x) {
        return Math.abs(guess * guess - x) < 0.0001;
    }
}
英文:

in java static context can be called from static context only .

try

public class Newton {
    public static double sqrt(double x) {
        return sqrtIter(1, x);
    }
    public static double sqrtIter(double guess, double x) {
        if (x &lt; 0) {
            throw new IllegalArgumentException(&quot;x must not be negative.&quot;);
        }
        while (true) {
            if (goodEnough( guess,  x)) {
                return guess;
            } else {
                guess = improve(guess, x);
            }
        }
        
    }
    static public double  improve(double guess, double x) {
        return average(guess, x/guess);
    }
    static public double   average(double x, double y) {
        return (x + y) / 2;
    }
    static public boolean  goodEnough(double guess, double x) {
        return Math.abs(guess*guess -x) &lt; 0.0001;
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 16:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63807368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定