- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,在编码方面,我或多或少是个菜鸟。我正在为一门大学类(class)学习 Java,我的一个练习要求我编写一段代码,使用左端点法、中点法和梯形法计算积分。
为此,我需要使用每次递增一步的 for 循环。我知道如何使用 double 来做到这一点,但问题是,当我使用 double 数据类型运行计算时,它会给我舍入错误。阅读完(主要是查看其他堆栈溢出问题)后,我认为我应该使用 BigDecimal 来消除舍入错误。
但是使用 BigDecimal 给我带来了一大堆错误。据我所知,我不能使用 <、>、== 等普通运算符,因为 BigDecimal 不是原始数据类型。但是后来我不知道如何在仍然使用 BigDecimal 的同时进行递增的基本 for 循环。我在这里看到的另一件事建议使用 Commons-math? (第二个答案向下)
https://stackoverflow.com/questions/16707397/whats-wrong-with-this-simple-double-calculation#=
我完全不知道如何使用它,因为据我所知,它与普通的旧 import java.lang.Math 不相似。
所以我想我的问题是,我怎样才能让我的代码在 for 循环中使用 BigDecimal,以及通常一起比较数字,或者我怎样才能使用 Common-Math 来简单地完全避免舍入错误?
这是我的代码:
import java.util.Scanner;
import java.lang.Math;
import java.math.BigDecimal;
public class IntegrationMethods{
//calculates the value of the function f(x)=x^2
public static BigDecimal f(BigDecimal x){
return x.multiply(x);
}
//uses a, b and N as arguments, calculates sum of integral using left
endpoint rule
public static BigDecimal leftEndpoint(BigDecimal a, BigDecimal b,
BigDecimal n){
BigDecimal h = (b.subtract(a)).divide(n);
sum = new BigDecimal("0");
i = new BigDecimal("0");
for(i.compareTo(n)<=0;){
sum = sum.add(h.multiply(f(a.add(h.multiply(i-1)))));
i = i.add(new BigDecimal(1));
}
return sum;
}
public static BigDecimal midpoint(BigDecimal a, BigDecimal b, BigDecimal
n){
BigDecimal h = (b.subtract(a)).divide(n);
BigDecimal sum = 0.0;
for(BigDecimal i=0.0; i<n; i++){
BigDecimal x1 = a.add(h.multiply(i));
BigDecimal x2 = a.add(h.multiply(i+1));
sum = sum.add(h.multiply((f(x1).add(f(x2))).divide(2)));
}
return sum;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the lower limit of integration a.");
BigDecimal a = scanner.nextBigDecimal();
System.out.println("Please enter the upper limit of integration b.");
BigDecimal b = scanner.nextBigDecimal();
//swaps a and b so that the bigger of the two is always b.
//this prevents a negative popping up later on.
if(b<a){
BigDecimal r = a;
BigDecimal m = b;
a = m;
b = r;
}
System.out.println("Please enter the number of sampling points N.");
BigDecimal n = scanner.nextBigDecimal();
//checks if n is greater than or equal to 1, and asks for a new value if it isn't.
while (n<1.0){
System.out.println("Please enter a new value for N.");
n = scanner.nextBigDecimal();
}
System.out.println("For " + n + " intervals, the integral of x^2 using the left endpoint rule is: " + leftEndpoint(a,b,n));
System.out.println("Using the midpoint rule, the integral of x^2 is: " + midpoint(a,b,n));
}
}
最佳答案
从 0 到 19 计数的正常循环是:
for (int i = 0; i < 20; i++)
System.out.println(i);
使用 BigDecimal
完成的相同循环看起来像这样:
BigDecimal end = BigDecimal.valueOf(20);
for (BigDecimal i = BigDecimal.ZERO; i.compareTo(end) < 0; i = i.add(BigDecimal.ONE))
System.out.println(i);
使用方法<
, >
, ==
等 BigDecimal
, 是使用 compareTo()
方法。 javadoc 显示了如何执行此操作:
This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is:
(x.compareTo(y)
<op>0)
, where <op> is one of the six comparison operators.
所以,i < end
写成 i.compareTo(end) < 0
.
另请注意 BigDecimal
为 0
预定义常量, 1
, 和 10
( BigDecimal.ZERO
、 BigDecimal.ONE
和 BigDecimal.TEN
),并创建 BigDecimal
整数值应使用 BigDecimal.valueOf(long val)
, 而不是 new BigDecimal(String val)
.
关于java - 如何使用 BigDecimal 递增 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47185616/
当你做这样的事情 BigDecimal bigDecimal = BigDecimal.ONE; 为什么bigDecimal成为一个新对象? 最佳答案 在这种情况下,变量(或字段)bigDecimal
我正在使用 BigDecimal 计算一些大实数。虽然我尝试了两种方法:BigDecimal.toString() 或 BigDecimal.stripTrailingZeros().toString
我广泛使用 BigDecimals。我经常需要进行长时间的计算,然后比较结果。由于这些漫长的计算,答案不是 1而是0.9999999... 。这就是为什么我无法精确比较 BigDecimals,而只能
我有一段代码有两个 BigDecimal 变量。这两个变量都被发送到方法格式(工作正常)然后打印。变量“d”被发送到格式方法,然后直接打印而不分配它(因为格式方法返回一个字符串)。问题出现在变量 a
我遇到过 java.math.BigDecimal 和 android.icu.math.BigDecimal 因为我需要在项目中使用 BigDecimal . 我意识到 Android BigDec
我在整个应用程序中使用 BigDecimal 来处理金钱 和百分比。但是,我需要一种方法来区分两者之间的用法(为了呈现它们,即在 JTable 中)。因此,我最初的想法是编写两个行为与 BigDeci
这应该很简单,但它正在爆炸。有什么想法吗? d = BigDecimal.new("2.0") YAML::load({:a => d}.to_yaml) TypeError: BigDecimal
我有两个 BigDecimal,我想根据它们的 significant digits 确定它们是否接近。 . 例如,请考虑以下内容: BigDecimal million = new BigDecim
我需要一种方法将其表达为有效的java代码:我有2个BigDecimals,我想知道较小的BigDecimal是否可以(当添加到较大的BigDecimals时) BigDecimal 一次)更改较大
我正在争论是使用 BigDecimal 和 BigInteger 还是仅使用 BigDecimal 来让我的生活更轻松,减少来回转换。在资源方面仅使用 BigDecimal 有缺点吗? 仅使用原始数据
我有以下代码: Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45). scala> v
我有一个 java 客户端,它通过 GRPC 调用 Clojure 服务。问题是,当我们从他们的服务收到响应时,它会在大十进制的末尾添加字母。当我们尝试在 java 中将其转换为大十进制时,我们会收到
我仍在学习 Java,并且一直在阅读多个站点上的文章。我在 Java Code Geeks 找到了一篇文章我有一个问题。该文章正在解释开放/封闭原则。本文以对公司产品应用折扣的场景为例。第一部分代码如
我正在尝试对 List 中的多个 BigDecimals 求和.目前,我正在使用两个流,但如果可能的话,我希望只有一个流。我不确定如何以高效的方式重写下面的内容。 BigDecimal totalCh
下面两行代码有什么区别? BigDecimal one = new BigDecimal("1"); BigDecimal two = BigDecimal.ONE; 两条线是否相同? 谢谢! 最佳答
这个问题在这里已经有了答案: BigDecimal from Double incorrect value? (4 个答案) Convert double to BigDecimal and set
在 Java 中,来自另一个 bigDecimal.toString() 的新 BigDecimal 是否总是等于?例如 BigDecimal a = new BigDecimal("1.23
对于新变量的比较或初始化,您使用其中的哪一个会有所不同吗? 我知道 BigDecimal.ZERO 是 1.5 的功能,所以这是一个问题,但假设我使用的是 1.5,这有关系吗? 谢谢。 最佳答案 Bi
情况: public static double pi(int a) { return (BigDecimal.valueOf(53360*sqrt(640320))).divide(co
我正在尝试创建一个可以在 diesel 中用于插入的结构。具体来说,我正在使结构可插入。编译时出现此错误。 我有一个结构,我正试图通过 derive 属性使 Insertable 成为可能。我有一个名
我是一名优秀的程序员,十分优秀!