longlongとdouble

消えると困るのでコピペ

ネタ元

Microsoft Visual C++にはLONGLONGという、 64ビットの整数があります。 その使い方をまとめてみました。 結論からいうと、intやdoubleなど普通の型と同様に扱うことができそうです。 使い方は以下のプログラムを見ればわかるでしょう。 (これはマイクロソフト独自の仕様です)
#include <windows.h>  // 必要
#include <stdio.h>

// 定数の宣言の仕方。longで"L"を付けるように、末尾に"i64"を付けます。
LONGLONG man = 10000i64;		// 64-bit integer-suffix
LONGLONG oku = 100000000i64;
LONGLONG tyo = 1000000000000i64;
LONGLONG kei = 10000000000000000i64;

int	main()
{
	LONGLONG a=1;

	for(int i=0; i<64; i++) {
		// %dの代わりに%I64dにします。同様に%I64X等も可能。
		printf("2^%-2d = %20I64d (%016I64Xh)\n",i,a,a);
		a *= 2;  //四則演算OK
	}

	// MAXLONGLONGは 922京3372兆0368億5477万5807 です。
	printf("MAXLONGLONG =  %I64d\n",MAXLONGLONG);

	// 実数間のキャストもOK
	printf("floatに変換    %f\n",(float)MAXLONGLONG);
	printf("再びLONGLONGに %I64d\n",(LONGLONG)(float)MAXLONGLONG);
	printf("doubleに変換   %f\n",(double)MAXLONGLONG);
	printf("再びLONGLONGに %I64d\n",(LONGLONG)(double)MAXLONGLONG);
	return 0;
}

出力結果
2^0  =                    1 (0000000000000001h)
2^1  =                    2 (0000000000000002h)
2^2  =                    4 (0000000000000004h)
2^3  =                    8 (0000000000000008h)
2^4  =                   16 (0000000000000010h)
2^5  =                   32 (0000000000000020h)
2^6  =                   64 (0000000000000040h)
2^7  =                  128 (0000000000000080h)
2^8  =                  256 (0000000000000100h)
2^9  =                  512 (0000000000000200h)
2^10 =                 1024 (0000000000000400h)
2^11 =                 2048 (0000000000000800h)
2^12 =                 4096 (0000000000001000h)
2^13 =                 8192 (0000000000002000h)
2^14 =                16384 (0000000000004000h)
2^15 =                32768 (0000000000008000h)
2^16 =                65536 (0000000000010000h)
2^17 =               131072 (0000000000020000h)
2^18 =               262144 (0000000000040000h)
2^19 =               524288 (0000000000080000h)
2^20 =              1048576 (0000000000100000h)
2^21 =              2097152 (0000000000200000h)
2^22 =              4194304 (0000000000400000h)
2^23 =              8388608 (0000000000800000h)
2^24 =             16777216 (0000000001000000h)
2^25 =             33554432 (0000000002000000h)
2^26 =             67108864 (0000000004000000h)
2^27 =            134217728 (0000000008000000h)
2^28 =            268435456 (0000000010000000h)
2^29 =            536870912 (0000000020000000h)
2^30 =           1073741824 (0000000040000000h)
2^31 =           2147483648 (0000000080000000h)
2^32 =           4294967296 (0000000100000000h)
2^33 =           8589934592 (0000000200000000h)
2^34 =          17179869184 (0000000400000000h)
2^35 =          34359738368 (0000000800000000h)
2^36 =          68719476736 (0000001000000000h)
2^37 =         137438953472 (0000002000000000h)
2^38 =         274877906944 (0000004000000000h)
2^39 =         549755813888 (0000008000000000h)
2^40 =        1099511627776 (0000010000000000h)
2^41 =        2199023255552 (0000020000000000h)
2^42 =        4398046511104 (0000040000000000h)
2^43 =        8796093022208 (0000080000000000h)
2^44 =       17592186044416 (0000100000000000h)
2^45 =       35184372088832 (0000200000000000h)
2^46 =       70368744177664 (0000400000000000h)
2^47 =      140737488355328 (0000800000000000h)
2^48 =      281474976710656 (0001000000000000h)
2^49 =      562949953421312 (0002000000000000h)
2^50 =     1125899906842624 (0004000000000000h)
2^51 =     2251799813685248 (0008000000000000h)
2^52 =     4503599627370496 (0010000000000000h)
2^53 =     9007199254740992 (0020000000000000h)
2^54 =    18014398509481984 (0040000000000000h)
2^55 =    36028797018963968 (0080000000000000h)
2^56 =    72057594037927936 (0100000000000000h)
2^57 =   144115188075855872 (0200000000000000h)
2^58 =   288230376151711744 (0400000000000000h)
2^59 =   576460752303423488 (0800000000000000h)
2^60 =  1152921504606846976 (1000000000000000h)
2^61 =  2305843009213693952 (2000000000000000h)
2^62 =  4611686018427387904 (4000000000000000h)
2^63 = -9223372036854775808 (8000000000000000h)
MAXLONGLONG =  9223372036854775807
floatに変換    9223372036854775800.000000
再びLONGLONGに 9223372036854775800
doubleに変換   9223372036854775800.000000
再びLONGLONGに 9223372036854775800
64ビットですから、doubleと同じbit幅を持ちます。精度はLONGLONGの方が上なので、 doubleでケタ落ちが起こるのはやむを得ないでしょう。

long で足らないことがたまにあるので、独自のクラスを作ろうかと思っていましたが、 その必要はなさそうです。