- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想制作一个使用 Heron 算法计算平方根的 Java 应用程序。但是当我输入 9 时,它会在屏幕上打印 2.777777910232544 。当我输入 1 时,它会打印 1。现在我不知道是否我写错了代码,或者我不了解 Java 中的 float 。
这是我的代码:
public class MainActivity extends AppCompatActivity {
float length1;
float width1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mainOutput = (TextView) findViewById(R.id.mainOutput);
final EditText mainInput = (EditText) findViewById(R.id.mainInput);
final Button wurzel2 = (Button) findViewById(R.id.wurzel2);
assert wurzel2 != null;
wurzel2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < 20; i++) {
float inputNumber = Integer.parseInt(mainInput.getText().toString());
length1 = 1;
width1 = inputNumber / length1;
float length2 = (length1 + width1) / 2;
float width2 = inputNumber / length2;
length1 = length2;
width1 = width2;
}
double wurzel = length1 / width1;
mainOutput.setText(String.valueOf(wurzel));
}
});
}
}
最佳答案
我编写了 Heron 算法的非 Android Java 实现,该实现源自 https://en.wikipedia.org/wiki/Methods_of_computing_square_roots 中显示的算法公式。
public class MyClass {
public static void main(String[] args) {
float x = 9;
System.out.println(heron(x));
}
static float heron(float s) {
float x = (float) 1.0; // initial approximation of result
for (int i = 0; i < 20; i++) {
float sDivX = s / x;
x = (x + sDivX) / 2;
// remove this line in production, this is just to watch progress
System.out.println(String.valueOf(x));
}
return x;
}
}
你的代码在循环内有length1=1(你的length1相当于我的x),所以从一个迭代到另一个迭代,它没有取得任何进展。
x = s/(float)2
可能是比 1 更好的初始估计,特别是对于较大的值。对于较小的输入值,20 次迭代可能有点过大。
关于java - Android 平方根计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858346/
出于某种原因,我的平方根程序得到的答案与大多数输入应得到的答案略有不同。我不确定这是为什么。只有某些输入是错误的。在给出答案后的最后我也遇到了段错误,我不确定为什么会这样。 #include #inc
我目前正在实现一个可以处理与物理单位相关的数字数据的类。 我想实现一种计算实例平方根的方法。假设您有一个具有属性值和名称的类的实例: from math import sqrt class Foo:
我正在使用 HTML + CSS + AngularJS 制作一个简单的计算器。一切正常,但我想添加一个 SquareRoot 函数。这是一些代码: function _solve(){ switc
问题是关于在通用数值接口(interface)中定义平方根算法的问题的策略方法。我知道存在解决不同条件下问题的算法。我对以下算法感兴趣: 仅使用选定的函数解决问题; 不关心操作的对象是整数、 floa
好吧,我已经研究了一段时间了,我知道我的逻辑是正确的,但是,我似乎无法生成正数的正确底平方根。 public int mySqrt(int x) { if(x 0) uppe
我找不到让这个函数 Math.sqrt(value) 工作的方法。 (╯°□°)╯︵┻━┻我不确定,但问题似乎出在运营商。我还尝试在 const calculation 中添加该函数,但它也不起作用。
我发现了这段获得平方根的代码,令我惊讶的是它的工作方式,使用 union 和位移这是代码: float sqrt3(const float x) { union { int i;
在 python 中使用 sqrt 函数时,我遇到了“distance ValueError: math domain error”问题。 这是我的代码: from math import sqrt
我一直在做一些研究,寻找一种对大整数进行运算的相对快速的平方根算法。我在这里找到了几个例程。第一个(下面)是用 C 语言编写的... int isqrt(int n) { int b = 0;
好吧,我想知道 math.h 平方根与其中包含神奇数字的那个(因 Quake 而出名,但由 SGI 制作)相比有多快。 但这对我来说是一个受伤的世界。 我首先在 Mac 上尝试了此操作,其中 math
有谁知道如何解决这个复发? 大定理在这里不起作用。 最佳答案 这在 O(1) 中似乎很明显,因为 T(n) = T(n - sqrt(n)) = T(m) with 0 < m < n 通过归纳,你得
我是一名优秀的程序员,十分优秀!