所以在编码时我或多或少都是一个菜鸟.我正在学习java的大学课程,我的一个练习要求我编写一个代码,使用左端点方法,中点方法和梯形方法来评估积分.
为此,我需要使用一个for循环,每次增加一步.我知道如何使用double来做到这一点,但问题是,当我使用double数据类型运行计算时,它会给我舍入错误.在阅读之后(主要是查看其他堆栈溢出问题),我想我应该使用BigDecimal来消除舍入错误.
但是使用BigDecimal会给我一大堆错误.据我所知,我不能使用常规运算符,如<,>,==等,因为BigDecimal不是原始数据类型.但后来我不知道如何做一个基本的for循环,在仍然使用BigDecimal的同时递增.我在这里看到的另一件事建议使用Commons-math?(第二个答案)
我没有最简单的想法如何使用它,据我所知,它与普通的旧的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));
}
}
Run Code Online (Sandbox Code Playgroud)
从0到19计数的正常循环是:
for (int i = 0; i < 20; i++)
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
完成相同的循环使用BigDecimal如下所示:
BigDecimal end = BigDecimal.valueOf(20);
for (BigDecimal i = BigDecimal.ZERO; i.compareTo(end) < 0; i = i.add(BigDecimal.ONE))
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
使用该方法<,>,==等用BigDecimal,是使用compareTo()方法.javadoc显示了如何执行此操作:
对于六个布尔比较运算符(<,==,>,> =,!=,<=)中的每一个,优先考虑该方法.建议执行这些比较的习惯用语是:
(x.compareTo(y)<op>0),其中<op>是六个比较运算符之一.
所以,i < end写成i.compareTo(end) < 0.
另请注意,BigDecimal具有for 0,1和10(BigDecimal.ZERO,BigDecimal.ONE和BigDecimal.TEN)的预定义常量,并且BigDecimal应该使用创建整数值BigDecimal.valueOf(long val),而不是new BigDecimal(String val).
| 归档时间: |
|
| 查看次数: |
2068 次 |
| 最近记录: |