Java for-loop问题 [英] java for-loop problem

查看:199
本文介绍了Java for-loop问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java程序来计算辛普森的积分规则。这是我有的代码。请注意count == 4,9,10,11的输出值中的第二列数字。他们不是我需要的数字,他们不遵循这个模式。我需要这些数字是准确的。这是怎么回事,我该如何解决它?

  public static void main(String [] args)
{
double totalS = 0.0;
int count = 0;

(double i = 0; i <4; i + = 0.4)
{
count ++;
totalS + = Sfunction(i,count);
System.out.println(count ++ i ++ totalS);



public static double Sfunction(double f1,int count)
{
double value;

if(f1 == 0.0 || f1 == 4.0)
value = Math.cos(Math.sqrt(f1));
else if((count count 2)== 1)
value = 2 * Math.cos(Math.sqrt(f1));
else
value = 4 * Math.cos(Math.sqrt(f1));

返回值;

$ / code $ / pre
$ b $我得到的输出:

  1 0.0 1.0 
2 0.4 4.226313639540303
3 0.8 5.478244888601832
4 1.2000000000000002 7.30884788480188
5 1.6 7.911122809972827
6 2.0 8.534897589034324
7 2.4 8.578100205110182
8 2.8 8.168723348285942
9 3.1999999999999997 7.736055200662704
10 3.5999999999999996 6.452869366954546
11 3.9999999999999996 5.620575693860261
pre

解决方案

每次循环时,都会在0.4的不精确加法中加入错误。 p>

取而代之的是,使用循环计数器的整数值,并缩放以获得更好的近似值:

<$ (int count = 0; count <10; ++ count){
final double i = 0.4 * count; p $ p>
System.out.println((count + 1)++ i);



$ b

这不会消除浮点错误,但这意味着它不是在每个迭代增加。要从输出中删除错误,请将输出格式化为合理的小数位数:

  for(int count = 0; count <10; ++ count){
final double i = 0.4 * count;
System.out.printf(%2d%.1f\\\
,(count + 1),i);
}


I am making a Java program to calculate Simpson's rule for integrals. Here is the code I have. Notice the second column of numbers in the output values of count == 4,9,10,11. They are not numbers that I need, they do not follow the pattern. I need these numbers to be accurate. What is going on and how can I fix it?

public static void main(String[] args) 
{
   double totalS = 0.0;
   int count = 0;

   for(double i=0; i< 4; i += 0.4 )
   {
          count++;
          totalS += Sfunction(i, count);
          System.out.println(count + " " + i + " " + totalS);
   }
}

public static double Sfunction(double f1, int count)
{
    double value;

    if (f1 == 0.0 || f1 == 4.0)
        value = Math.cos(Math.sqrt(f1));
    else if ((count % 2) == 1)
        value = 2 * Math.cos(Math.sqrt(f1));
    else
        value = 4 * Math.cos(Math.sqrt(f1));

    return value;
}

I get the output of:

1    0.0    1.0 
2    0.4    4.226313639540303
3    0.8    5.478244888601832
4    1.2000000000000002    7.30884788480188
5    1.6    7.911122809972827
6    2.0    8.534897589034324
7    2.4    8.578100205110182
8    2.8    8.168723348285942
9    3.1999999999999997    7.736055200662704
10   3.5999999999999996    6.452869366954546
11   3.9999999999999996    5.620575693860261

解决方案

Each time you go round your loop, you are compounding the error in the inexact addition of 0.4 to i.

Instead, use an integral value for the loop counter, and scale that to get a better approximation to the values:

    for ( int count = 0; count < 10; ++count ) {
        final double i = 0.4 * count;
        System.out.println ( ( count + 1 ) + " " + i );
    }

This will not eliminate the floating point error, but it will mean it is not increasing at each iteration. To remove the error from the output, format the output to a reasonable number of decimal places:

    for ( int count = 0; count < 10; ++count ) {
        final double i = 0.4 * count;
        System.out.printf ( "%2d %.1f\n", ( count + 1 ), i );
    }

这篇关于Java for-loop问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆