简体   繁体   English

Java双变量问题

[英]Java- double variable issue

Here's my simple equations counter.Now it counts equations with two variables.It's based on trying many combinations (4 milion or 16 milion) of a and b variables.So the code works well and it counts exact.But since I tried to change variable b to double, things go wrong.I've expected line b=b+0.1 secures setting variable b each 10th time to 1.0.But almost imidiately after start way more decimal numbers appear for each b .So do I use wrong datatype? 这是我简单的方程式计数器,现在它计算带有两个变量的方程式,它基于尝试ab变量的许多组合(400万或16百万),因此代码运行良好且计数准确,但是由于我尝试更改变量b会翻倍,这会出错。我期望b=b+0.1行将第10次设置变量b确保为1.0。但是在启动之后几乎几乎是每个步骤b都会出现更多的十进制数,那么我使用错误的数据类型吗? Or should I raise variable b by different value?(I also tried changing all variables to double).Thank you for suggestions! 还是应该将变量b增加不同的值?(我还尝试将所有变量都更改为double)。谢谢您的建议!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Buffer{
static int a;
static double b;
static BufferedReader reader;
static String query;
static int range;
static int result;

public static void main(String[] args)throws IOException{
    reader=new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Choose speed of test(fast or slow):");
    query=reader.readLine();

    if(query.equals("fast"))
        range=2000;
    else
        range=4000;

     //START THE TEST//       
    while((a+b)!=26000){
        b=b+0.1;
        if(b==range+1){
            b=0;
            a=a+1;
        }
        if((a+b)!=26000)
            System.out.println("a- "+a+", "+"b- "+b+"=false.");

        if((a+b)==26000){
            System.out.println("a- "+a+", "+"b- "+b+"=true.");
            System.out.println("\n"+"Done.You can possibly solve this equation, if a= "+a+" and b= "+b+".");
        }
        if(a==range&&b==range&&(a+b)!=26000){
            System.out.println("\n"+"Tested "+a*b+" options.Solution not found.");
            System.exit(0);
        }
    }   
}
}

You can use BigDecimal instead of float/double primitives to avoid those issues. 您可以使用BigDecimal而不是float / double原语来避免这些问题。 Some of which are described here: Java Mistake 1 - Using float and double for monetary or financial calculation 这里描述了其中一些: Java错误1-使用浮点和双精度进行货币或财务计算

The issue you deal with is representation error . 您要处理的问题是制图表达错误 Type double can not represent some values. 类型double无法代表某些值。

Typical solution for this situation is usage of BigDecimal or Integer type. 这种情况的典型解决方案是使用BigDecimal或Integer类型。

Everyone here, did give you an alternative solution, but I think you should understand, why did the datatype double did not work for you. 这里的每个人都为您提供了替代解决方案,但我想您应该理解,为什么数据类型double不适用于您。

The problem lies with Binary representation . 问题在于Binary representation For decimals is not represented to the exact form. 对于小数,不以确切的形式表示。 Say for example: 例如说:

10 is represented as 1010 in binary but , 10 is represented as 1010 in binary 但是

0.10 is 0.00011001100110011001100110011001 and still going on.....

Hence the error occurs in double. 因此,该错误会发生两次。 As suggested by others, opt for BigDecimal. 正如其他人建议的那样,选择BigDecimal。

You can also learn more about the same, through these links; 您也可以通过这些链接了解更多有关此的信息。

Rounding Errors 舍入误差

Addition Of Doubles 双打加法

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM