Integer division: How do you produce a double?

2018-12-31 00:43发布

For this code block:

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

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

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

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

10条回答
怪性笑人.
2楼-- · 2018-12-31 01:22

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;
查看更多
像晚风撩人
3楼-- · 2018-12-31 01:31

Type Casting Is The Only Way


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

Now, there are several ways we can try to get precise double value (where num and denom are int type, and of-course with casting)-

  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;

but not 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;

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

查看更多
刘海飞了
4楼-- · 2018-12-31 01:34

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::divide and trust that it does the right thing.

查看更多
梦该遗忘
5楼-- · 2018-12-31 01:38

use something like:

double step = 1d / 5;

(1d is a cast to double)

查看更多
登录 后发表回答