Java 使用双精度或浮点计算错误
Java miscalculation using either double or float
我正在尝试使用以下代码为 MVC 模式的模型部分创建一个 Web 应用程序:
public OddsCalcBrain(String _odds, String _personalodds, String _bankroll){
odds = Double.parseDouble(_odds);
personalodds = Double.parseDouble(_personalodds) / 100.0d;
personaloddslose = 1.0d-personalodds;
bankroll = Double.parseDouble(_bankroll);
result = bankroll * ((odds * personalodds - personaloddslose) / (2.0d * odds));
}
public String getResult(){
return Double.toString(result);
}
我在我的 servlet 中使用 RequestDispatcher 将结果转发到 JSP 页面,它看起来像这样:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String _personalodds = request.getParameter("personalodds");
String _odds = request.getParameter("odds");
String _bankroll = request.getParameter("bankroll");
OddsCalcBrain brain = new OddsCalcBrain(_personalodds, _odds, _bankroll);
request.setAttribute("result", brain.getResult());
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/oddscalc.jsp");
dispatcher.forward(request, response);
}
为了打印它,我使用了这个:
Result: ${result}
结果算错了。例如,如果:
odds = 3;
personalodds = 0.5; //(The user would enter a percentage, so 50 would be 0.5)
personaloddslose = 0.5;
bankroll = 120;
公式的结果应该是 20.0,如果我手动将它放入计算器中,它就会产生结果,但无论我对变量使用 float 还是 double,结果都会得到 0.636。为什么会这样,我怎样才能正确地做到这一点?
您来电:
OddsCalcBrain brain = new OddsCalcBrain(_personalodds, _odds, _bankroll);
//OddsCalcBrain("50", "3", "120");
而不是
OddsCalcBrain brain = new OddsCalcBrain(_odds, _personalodds, _bankroll);
//OddsCalcBrain("3", "50", "120");
我正在尝试使用以下代码为 MVC 模式的模型部分创建一个 Web 应用程序:
public OddsCalcBrain(String _odds, String _personalodds, String _bankroll){
odds = Double.parseDouble(_odds);
personalodds = Double.parseDouble(_personalodds) / 100.0d;
personaloddslose = 1.0d-personalodds;
bankroll = Double.parseDouble(_bankroll);
result = bankroll * ((odds * personalodds - personaloddslose) / (2.0d * odds));
}
public String getResult(){
return Double.toString(result);
}
我在我的 servlet 中使用 RequestDispatcher 将结果转发到 JSP 页面,它看起来像这样:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String _personalodds = request.getParameter("personalodds");
String _odds = request.getParameter("odds");
String _bankroll = request.getParameter("bankroll");
OddsCalcBrain brain = new OddsCalcBrain(_personalodds, _odds, _bankroll);
request.setAttribute("result", brain.getResult());
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/oddscalc.jsp");
dispatcher.forward(request, response);
}
为了打印它,我使用了这个:
Result: ${result}
结果算错了。例如,如果:
odds = 3;
personalodds = 0.5; //(The user would enter a percentage, so 50 would be 0.5)
personaloddslose = 0.5;
bankroll = 120;
公式的结果应该是 20.0,如果我手动将它放入计算器中,它就会产生结果,但无论我对变量使用 float 还是 double,结果都会得到 0.636。为什么会这样,我怎样才能正确地做到这一点?
您来电:
OddsCalcBrain brain = new OddsCalcBrain(_personalodds, _odds, _bankroll);
//OddsCalcBrain("50", "3", "120");
而不是
OddsCalcBrain brain = new OddsCalcBrain(_odds, _personalodds, _bankroll);
//OddsCalcBrain("3", "50", "120");