开发者

Calculate percentage of 64 int

I have the following code:

typedef __int64 BIG_INT;
typedef double CUT_TYPE;

#define CUT_IT(amount, percent) (amount * percent)

void main()
{
    CUT_TYPE cut_percent = 1;

    BIG_INT bintOriginal = 0x1FFFFFFFFFFFFFF;
    BIG_INT bintAfter = CUT_IT(bintOriginal, cut_percent);
}
开发者_StackOverflow中文版

bintAfter's value after the calculation is 144115188075855872 instead of 144115188075855871 (see the "2" in the end, instead of "1"??).

On smaller values such as 0xFFFFFFFFFFFFF I get the correct result.

How do I get it to work, on 32bit app? What do I have to take in account?

My aim is to cut a certain percentage of a very big number.

I use VC++ 2008, Vista.


double has a 52 bit mantissa, you're losing precision when you try to load a 60+ bit value into it.


Floating point calculations aren't guaranteed to be perfectly accurate, and you've defined CUT_TYPE as double.

See this answer for more info: Dealing with accuracy problems in floating-point numbers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜