Java 整数除法:如何产生双精度数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3144610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:49:54  来源:igfitidea点击:

Integer division: How do you produce a double?

javacastinginteger-division

提问by walnutmon

For this code block:

对于此代码块:

int num = 5;
int denom = 7;
double d = num / denom;

the value of dis 0.0. It can be forced to work by casting:

的值d就是0.0。它可以通过强制转换来强制工作:

double d = ((double) num) / denom;

But is there another way to get the correct doubleresult? I don't like casting primitives, who knows what may happen.

但是还有另一种方法可以获得正确的double结果吗?我不喜欢铸造原语,谁知道会发生什么。

采纳答案by Matthew Flaschen

double num = 5;

That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:

这避免了演员阵容。但是您会发现强制转换是明确定义的。您不必猜测,只需检查JLS。int 到 double 是一种扩大转换。从第5.1.2 节开始

Widening primitive conversions do not lose information about the overall magnitude of a numeric value.

[...]

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

扩展原始转换不会丢失有关数值整体大小的信息。

[...]

将 int 或 long 值转换为 float,或将 long 值转换为 double 可能会导致精度损失——也就是说,结果可能会丢失值的一些最低有效位。在这种情况下,生成的浮点值将是整数值的正确舍入版本,使用 IEEE 754 舍入到最近模式(第 4.2.4 节)

5 can be expressed exactly as a double.

5 可以精确地表示为双精度数。

回答by Paul Tomblin

What's wrong with casting primitives?

铸造原语有什么问题?

If you don't want to cast for some reason, you could do

如果你出于某种原因不想投射,你可以这样做

double d = num * 1.0 / denom;

回答by Jesper

I don't like casting primitives, who knows what may happen.

我不喜欢铸造原语,谁知道会发生什么。

Why do you have an irrational fear of casting primitives? Nothing bad will happen when you cast an intto a double. If you're just not sure of how it works, look it up in the Java Language Specification. Casting an intto doubleis a widening primitive conversion.

为什么你对铸造原语有一种非理性的恐惧?当您将 an 转换int为 a时,不会发生任何不好的事情double。如果您只是不确定它是如何工作的,请在Java Language Specification 中查找。强制转换inttodouble是一种扩大的原始转换

You can get rid of the extra pair of parentheses by casting the denominator instead of the numerator:

您可以通过转换分母而不是分子来摆脱额外的一对括号:

double d = num / (double) denom;

回答by alianos-

If you change the type of one the variables you have to remember to sneak in a double again if your formula changes, because if this variable stops being part of the calculation the result is messed up. I make a habit of casting within the calculation, and add a comment next to it.

如果你改变了一个变量的类型,你必须记住,如果你的公式发生变化,你必须记住再次偷偷加入一个双精度数,因为如果这个变量不再是计算的一部分,结果就会一团糟。我习惯在计算中进行转换,并在其旁边添加注释。

double d = 5 / (double) 20; //cast to double, to do floating point calculations

Note that casting the result won't do it

请注意,投射结果不会这样做

double d = (double)(5 / 20); //produces 0.0

回答by Nikhil Kumar

Cast one of the integers/both of the integer to float to force the operation to be done with floating point Math. Otherwise integer Math is always preferred. So:

将整数之一/两个整数转换为浮点数以强制使用浮点数学完成运算。否则整数数学总是首选。所以:

1. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;

Note that casting the result won't do it. Because first division is done as per precedence rule.

请注意,投射结果不会这样做。因为第一除法是按照优先规则完成的。

double d = (double)(5 / 20); //produces 0.0

I do not think there is any problem with casting as such you are thinking about.

我认为您正在考虑的铸造没有任何问题。

回答by ungalcrys

use something like:

使用类似的东西:

double step = 1d / 5;

(1d is a cast to double)

(1d 是加倍的演员表)

回答by Minhas Kamal

Type Casting Is The Only Way

类型转换是唯一的方法



Producing a doublefrom integer division- there is no other way without casting(may be you will not do it explicitly but it will happen).

double从整数除法产生一个 -没有其他方法没有强制转换(可能你不会明确地这样做,但它会发生)。

Now, there are several ways we can try to get precise doublevalue (where numand denomare inttype, and of-course with casting)-

现在,有几种方法可以尝试获得精确的double值(其中numdenomint类型,当然还有强制转换)-

  1. with explicit casting:

    • double d = (double) num / denom;
    • double d = ((double) num) / denom;
    • double d = num / (double) denom;
    • double d = (double) num / (double) denom;
  1. 使用显式转换:

    • double d = (double) num / denom;
    • double d = ((double) num) / denom;
    • double d = num / (double) denom;
    • double d = (double) num / (double) denom;

but not double d = (double) (num / denom);

但不是 double d = (double) (num / denom);

  1. with implicit casting:

    • double d = num * 1.0 / denom;
    • double d = num / 1d / denom;
    • double d = ( num + 0.0 ) / denom;
    • double d = num; d /= denom;
  1. 使用隐式转换:

    • double d = num * 1.0 / denom;
    • double d = num / 1d / denom;
    • double d = ( num + 0.0 ) / denom;
    • double d = num; d /= denom;

but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );

但不是double d = num / denom * 1.0;
也不是double d = 0.0 + ( num / denom );

回答by Ken Wayne VanderLinde

You might consider wrapping the operations. For example:

您可以考虑包装操作。例如:

class Utils
{
    public static double divide(int num, int denom) {
        return ((double) num) / denom;
    }
}

This allows you to look up (just once) whether the cast does exactly what you want. This method could also be subject to tests, to ensure that it continues to do what you want. It also doesn't matter what trick you use to cause the division (you could use any of the answers here), as long as it results in the correct result. Anywhere you need to divide two integers, you can now just call Utils::divideand trust that it does the right thing.

这使您可以(仅一次)查询演员表是否完全按照您的意愿行事。此方法也可能会接受测试,以确保它继续执行您想要的操作。您使用什么技巧来导致除法也无关紧要(您可以使用此处的任何答案),只要它产生正确的结果即可。在任何需要将两个整数相除的地方,您现在只需调用Utils::divide并相信它会做正确的事情。

回答by Mabood Ahmad

just use this.

就用这个。

int fxd=1;
double percent= (double)(fxd*40)/100;

回答by Alp Altunel

Best way to do this is

最好的方法是

int i = 3;
Double d = i * 1.0;

d is 3.0 now.