gpt4 book ai didi

java - 如何使用 BigDecimal 递增 for 循环?

转载 作者:行者123 更新时间:2023-11-29 07:28:16 30 4
gpt4 key购买 nike

因此,在编码方面,我或多或少是个菜鸟。我正在为一门大学类(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 .

另请注意 BigDecimal0 预定义常量, 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com