我的价值不断出现在NaN身上

时间:2014-01-26 19:07:08

标签: java variables nan

基本上我的delta值给了我一些麻烦,当我将所有变量打印到控制台窗口时,一切都是正确的,除了delta之外,其值一直显示为NaN。我不能为我的生活找出原因。我是大学的初级CIS学生,这真让我头疼。

import java.util.Scanner;


public class ReimannSumCalculator {
    static double a,b,c,start,end;
    static double partitions;
    static double delta=end - start / partitions;

    private static double Quadratic(double a,double b,double c,double x) {
        double Quadratic = a*x*x + b*x + c;
        return Quadratic;
    }


    public static void leftReiman(double a,double b,double c,double delta,double start,double end) {
        double leftReiman = 0;

        for(double x = start; x<end; x+=delta) {
            leftReiman = delta * Quadratic(a,b,c,x) + leftReiman;
        }

        System.out.println("Your left Reiman sum is " + leftReiman);
    }

    public static void rightReiman(double a,double b,double c,double delta,double start,double end) {
        double rightReiman = 0;

        for(double x = start+delta; x<=end; x+=delta) {
            rightReiman = delta* Quadratic(a,b,c,x) + rightReiman;
        }

        System.out.println("Your right Reiman sum is " + rightReiman);
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter some values for a b and c.");

        a = keyboard.nextDouble();
        b = keyboard.nextDouble();
        c = keyboard.nextDouble();

        System.out.println("Please enter a start and end point.");

        start = keyboard.nextDouble();
        end = keyboard.nextDouble();

        System.out.println("Now enter the amount of partitions.");

        partitions = keyboard.nextInt();

        leftReiman(a,b,c,start,end,delta);
        rightReiman(a,b,c,start,end,delta);

        System.out.println("The delta is: " + delta);
        System.out.println("The amount of partitions are: " + partitions);
        System.out.println("a is: "+ a);
        System.out.println("b is: "+ b);
        System.out.println("c is: " + c);
        System.out.println("The start is: " + start);
        System.out.println("The end is: " + end);
    }
}

3 个答案:

答案 0 :(得分:4)

在初始化其中一个变量之前,您正在执行start / partitions

所以你基本上计算0.0 / 0.0,等于NaN

答案 1 :(得分:3)

Java并不擅长。它不会自动计算符号表达式的操作顺序,也不会在输入更改时重新计算表达式。

正确的操作顺序,以及在输入变得陈旧时重新计算值的负担在程序员身上。

static double a,b,c,start,end;

static double partitions;

static double delta=end - start / partitions;

相当于

static double a,b,c,start = 0.0d, end = 0.0d;

static double partitions = 0.0d;

static double delta=end - start / partitions;

因为所有字段的类型都为零值,相当于

static double a,b,c,start = 0.0d, end = 0.0d;

static double partitions = 0.0d;

static double delta=0.0d - 0.0d / 0.0d;   // division by zero -> NaN

因为字段按声明的顺序初始化。

然后您无法重新计算delta正文中的main,因此您最终将delta NaN传递给leftReimannrightReimann。< / p>


使用整数循环来确保处理正确数量的分区而不是将double x与双边界进行比较可能是有意义的。浮点运算是有损的,因此在循环条件下检查double时可能会遇到边界条件。

public static double leftReimann(double a,double b,double c,int partitions,double start,double end) {
    assert partitions > 0;

    double leftReimann = 0;

    double delta = (end - start) / partitions;

    for(int i = 0; i < partitions; ++i) {
        double x = start + delta * i;
        leftReimann += delta * Quadratic(a,b,c,x);
    }

    System.out.println("Your left Reimann sum is " + leftReimann);
    return leftReimann;
}

public static double rightReimann(double a,double b,double c,int partitions,double start,double end) {
    assert partitions > 0;

    double rightReimann = 0;

    double delta = (start - end) / partitions;  // negative

    for(int i = 0; i < partitions; ++i) {
        double x = end + delta * i;
        rightReimann += delta * Quadratic(a,b,c,x);
    }

    System.out.println("Your right Reimann sum is " + rightReimann);
    return rightReimann;
}

答案 2 :(得分:0)

有两种选择,要么使delta成为一种功能

private double getDelta() { return (end - start) / partitions;

或者,您可以在设置结束,开始或分区后重新计算增量。 (在这种情况下,就在您调用Reiman函数之前,您应该说:

delta = (end - start) / partitions;

所以它使用了end,start和partitions的新值。

正如其他人所说,在初始化delta时,分区为零,因此除以零是NaN。