在java中将两个数字加倍 [英] Divide two number double in java

查看:93
本文介绍了在java中将两个数字加倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将两个数字分成9.6和3.2时。 double数据类型的第二个。但是,当分割结果(结果变量是类型double)是2.9999999999999996而不是3新右。有人能帮帮我吗?

when I divide two numbers 9.6 and 3.2. Number two on the double data type. However, when dividing the result (outcome variable is type double) is 2.9999999999999996 instead of 3 new right. can someone help me?

推荐答案

请看我对这个问题的评论。也许你所有的问题都是熟悉浮点数,作为实数这样的概念和数学抽象的近似计算机表示:

http://en.wikipedia.org/wiki/Floating-point_number [ ^ ],

http://en.wikipedia.org/wiki/Real_number [ ^ ]。



需要注意的是:作为微积分基础的实数理论是一个极其深刻和复杂的领域。数学。



如果使用定点运算,你可以在功能有限的情况下进行100%精确计算,在.NET中,可以根据整数类型建模:

http://en.wikipedia.org/wiki/Fixed-point_arithmetic [ ^ ]。



这个想法是这样的:比如,你想在你的例子中用两位数后计算数字。将您的数字乘以100,执行整数计算:9.60 / 3.20 = 960/320 = 3.



但是,定点运算确实有限。例如,你不能计算1/3这样的东西。对于这样的计算,你仍然需要使用浮点运算。



-SA
Please see my comment to the question. Perhaps all your problem is to get familiar with floating-point numbers, as an approximate computer representation of such concept and mathematical abstraction as real numbers:
http://en.wikipedia.org/wiki/Floating-point_number[^],
http://en.wikipedia.org/wiki/Real_number[^].

Just to note: the theory of real numbers, being a base of calculus, is an extremely deep and complex field of mathematics.

You can have 100% precise calculations with limited functionality, if you use fixed-point arithmetic, which, in .NET, can be modeled based on integer types:
http://en.wikipedia.org/wiki/Fixed-point_arithmetic[^].

The idea is this: say, you want to calculate the number in your example with two digits after point. Multiply your numbers by 100, perform integer calculations: 9.60/3.20 = 960/320 = 3.

However, fixed-point operations are really limited. You cannot calculate such thing as 1/3, for example. For such calculations, you still need to use floating-point arithmetic.

—SA


这篇关于在java中将两个数字加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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