- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,该程序使用牛顿法计算用户提供的指定精度 (k) 的整数 (n) 的平方根。该程序运行良好,但我注意到我认为是一个舍入问题。有人可以帮我解决这个问题吗?
例如:
Scanner userInput = new Scanner(System.in);
//print instructions and scan user input
System.out.println("~ This program computes the square root of an integer ~");
System.out.print("\n" + "Enter a non-negative integer [not greater than 1 billion (10^9)] n: ");
int n = userInput.nextInt();
System.out.print("\n" + "Enter a non-negative integer [not greater than 10 thousand (10^4)] k: ");
int k = userInput.nextInt();
userInput.close();
// declaring and converting variables
int p = (k + 1);
BigDecimal num = new BigDecimal(n);
BigDecimal guess = new BigDecimal(n);
BigDecimal newGuess;
BigDecimal sqrt;
// calculating error using int p
BigDecimal error = BigDecimal.ONE.movePointRight(-p);
// calculating guess using int p
BigDecimal diff = BigDecimal.ONE.scaleByPowerOfTen(p);
// newton's loop
while (diff.compareTo(error) == 1) {
newGuess = guess.subtract(
((guess.multiply(guess)).subtract(num))
.divide(guess.add(guess), k, RoundingMode.DOWN));
diff = newGuess.subtract(guess);
if (diff.signum() == -1) {
diff = diff.abs();
}
guess = newGuess;
}
// printing sqrt to screen
sqrt = guess;
System.out.println("loop calculated: " + "\n" + sqrt);
最佳答案
在循环中,根据牛顿法迭代计算总和,由此每个被加数被四舍五入并导致误差。为了说明这一点,额外输出 guess
的初始值很有用。以及 diff
的所有值.例如 n = 81, k = 7
输出是:
guess: 81
diff: 40.0000000
diff: 19.5121951
diff: 8.8591124
diff: 3.1073634
diff: 0.5070567
diff: 0.0142611
diff: 0.0000112
diff: 0E-7
loop calculated:
9.0000001
diff
- 值从
guess
中减去,准确值为
9.0000001
,即个别舍入误差导致偏差
0.0000001
.要改变这种行为,可以增加比例,例如通过一,
k += 1
, 在循环之前,并使用
guess = guess.setScale(k - 1, RoundingMode.DOWN);
重置为实际值循环后。然后输出是:
guess: 81
diff: 40.00000000
diff: 19.51219512
diff: 8.85911242
diff: 3.10736339
diff: 0.50705669
diff: 0.01426108
diff: 0.00001129
diff: 0E-8
loop calculated:
9.0000000
n = 87026 , k = 11
验证更改。 ,其输出没有变化:
guess: 87026
diff: 43512.50000000000
diff: 21755.75001149068
diff: 10876.87510915519
diff: 5436.43840462864
diff: 2714.22604199009
diff: 1349.16761702433
diff: 659.01405739182
diff: 300.74946658011
diff: 107.35187252479
diff: 18.35523727653
diff: 0.56993647588
diff: 0.00055055104
diff: 5.1E-10
diff: 0E-11
loop calculated:
295.00169491039
guess: 87026
diff: 43512.500000000000
diff: 21755.750011490686
diff: 10876.875109155187
diff: 5436.438404628644
diff: 2714.226041990089
diff: 1349.167617024336
diff: 659.014057391816
diff: 300.749466580112
diff: 107.351872524786
diff: 18.355237276533
diff: 0.569936475871
diff: 0.000550551041
diff: 5.13E-10
diff: 0E-12
loop calculated:
295.00169491038
关于java - RoundingMode.DOWN 用于 BigDecimal 向上取整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899222/
我的 Java println 语句中有以下代码行: System.out.println("Iteration: " + counter + "\tRoot: " + new
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 5 年前。 所以我有这个代码 public String getRounded(doub
像这样制作我自己的格式化程序: enum Formatters { enum Number { static let moneyFormatter: NumberFormatter = {
请随意评论我。下面的程序出了什么问题。它给出了不同的回合结果。 public class Test { public static String round(double value, int
据我了解,RoundingMode.HALF_UP 使用我们传统的舍入方式。但是,它没有给我想要的结果: 我正在使用具有不同参数(#、#.#、...等)的 DecimalFormat() 并输入 do
我编写此代码是为了测试 BigDecimal 的运行情况,但发现 RoundingMode.UNNECESSARY 引发了异常。谁能解释一下为什么? public class TestRounding
我正在尝试编写一个程序,该程序使用牛顿法计算用户提供的指定精度 (k) 的整数 (n) 的平方根。该程序运行良好,但我注意到我认为是一个舍入问题。有人可以帮我解决这个问题吗? 例如: 对于输入:n =
基于this stackoverflow post ,我希望以下内容打印出 0.59 而不是 0.60。 import java.math.RoundingMode; import java.text
我认为它返回1.13,但freemarker返回1.12。 什么是正确的? ${(1.125)?string(",##0.00;roundingMode=HALF_UP")} freemarker 版
这个问题已经有答案了: Java BigDecimal setScale and rounding with half_even (1 个回答) 已关闭 6 年前。 我正在尝试调试 HALF_EVEN
为什么这个 DecimalFormat 没有按预期使用 RoundingMode.HALF_UP 进行舍入,应该怎么做才能获得预期的输出?可以用 DecimalFormat 来完成吗? Decimal
我在四舍五入时遇到问题。具体来说,在阅读了所有 javadoc 之后,我期待以下代码: int n = (integer between 0 and 9, included) new BigDecim
当我这样做时: let a = self.oddsNumberFormatter.stringFromNumber(2.601) // 2.60 let b = self.oddsNumberForm
当我用HALF_EVEN模式对双数取整时,发现一个问题,不知道是不是JDK的bug?请看下面的代码: public static void main(String[] args) { Roun
看来 HALF_EVEN 舍入模式在 Java DecimalFormat 中的工作方式与 BigDecimal 不同。有没有办法使 DecimalFormat 保持一致? // Using BigD
目前我正在编写一个程序来计算数学常数。但是,我在舍入 BigDecimal 时遇到问题。一旦达到非终止小数 (1.6),就会抛出错误。我认为问题在于舍入线,但它也将其设置为正确的小数位数,但没有舍入它
我有大约 400 个使用 RoundingMode.HALF_UP 的语句,并且根据应用的性质,我希望用户能够在舍入模式之间进行选择。但是,如果可能的话,我不知道如何将其表示为变量。由于语句数量令人难
每当有十进制值时,我都试图四舍五入一个 Double 数字。我正在尝试以下。 以下代码段按预期四舍五入所有其他值。 1.08 -> 2 9.5 -> 10 但是将 0.08 舍入为 0 而不是 1。我
如何在 BigDecimal 中的点后只保留 3 个值? 我找到了一个解决方案,但它需要一个 RoundingMode BigDecimal d = BigDecimal.valueOf(0.
RoundingMode.HALF_EVEN 的 javadoc 指出: This rounding mode is analogous to the rounding policy used for
我是一名优秀的程序员,十分优秀!