gpt4 book ai didi

Java方法calculate ("2.002*1000")//返回2001.999999999999

转载 作者:行者123 更新时间:2023-12-01 06:35:24 25 4
gpt4 key购买 nike

我创建了一个方法calculate(String)来计算String类型的操作数,例如“2+3”。我已经尝试了很多操作数,并且该方法工作正常,除了:计算("2.002*1000)//返回2001.9999999999我很好奇这个错误怎么会发生。我把代码检查了很多遍,但我无法找出问题出在哪里。

代码如下:

private String calculate(String str)
{
String num = "";
List<String> list = new ArrayList<String>();
for(int i = 0; i < str.length(); i++) //Arrange str into list, elements are seperated by "+" or "-"
{
if(! checkPM(str.substring(i, i + 1)))
{
num = num + str.substring(i, i + 1);
} else
{
list.add(num);
list.add(str.substring(i, i + 1));
num = "";
}
}
list.add(num);//add the last num into list
if(checkSign(list.get(list.size() - 1)))//remove last element if it is an operator
{
list.remove(list.get(list.size() - 1));
}
String numlistele = ""; //Elements of numlist
List<String> numlist = new ArrayList<String>(); //List of numbers to be TD
List<String> TDlist = new ArrayList<String>(); //List of times or divide
for(int j = 0; j < list.size(); j++) //Check which of the elements of list contains "*" or "/"
{
String tdAns = ""; //Answer of numlistele timed or divided
if(checkTD(list.get(j))) // When the elements of list contains "*" or "/"
{
for(int k = 0; k < list.get(j).length(); k++) //
{
if(! checkSign(list.get(j).substring(k, k + 1)))
{
numlistele = numlistele + list.get(j).substring(k, k+1);
} else
{
numlist.add(numlistele);
TDlist.add(list.get(j).substring(k, k+1));
numlistele = "";
}
}
numlist.add(numlistele); //Adds the last number into numlist
numlistele = ""; //Restore numlistele to "", to be used in next loop
tdAns = numlist.get(0); //Answer of numlistele timed or divided, firstly it is equals to the first elements of numlist
for(int l = 0; l < TDlist.size(); l++)
{
if(TDlist.get(l).equals("×"))
{
double tempdou = Double.parseDouble(tdAns) * //temporary double used to save to tdANS
Double.parseDouble(numlist.get(l+1));
tdAns = String.valueOf(tempdou);
} else //when TDlist.get(l).equals("/") is true
{
double tempdou = Double.parseDouble(tdAns) /
Double.parseDouble(numlist.get(l+1));
tdAns = String.valueOf(tempdou);
}
}
list.set(j, tdAns);
}
numlist.clear(); //Clear numlist for next loop
TDlist.clear(); //Clear TDlist for next loop
}
String ans = list.get(0); //Will become final answer later, first it is assign to first element of list
for(int m = 0; m < list.size(); m++)
{
if(list.get(m).equals("+"))
{
double tempdou = Double.parseDouble(ans) + //Temporary double used to save to ans
Double.parseDouble(list.get(m + 1));
ans = String.valueOf(tempdou);
} else if(list.get(m).equals("-"))
{
double tempdou = Double.parseDouble(ans) -
Double.parseDouble(list.get(m + 1));
ans = String.valueOf(tempdou);
}
}
if(ans.length() > 2)
{
if(ans.substring(ans.length() - 2).equals(".0")) //To remove .0 of the answer
{
ans = ans.substring(0, ans.length() - 2);
}

}
return ans;
}

提前致谢。

最佳答案

浮点表示并不总是那么准确。当您解析字符串“2.002”时,Double.parseDouble 返回最接近的可能的double 表示形式。

当乘以1000时,浮点表示中的错误变得足够大,足以引起注意(2001.999999999999)。

这是一个good online article about floating point representation以及可能发生的不幸。

为了确保 float 的准确表示,可以使用Java内置的BigDecimal类(class)。它可能没有那么快,但它将保留您需要的所有精度。

关于Java方法calculate ("2.002*1000")//返回2001.999999999999,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555012/

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