PHPã¨ã¯ãã£ã¦ããã¤ãã£ãã®æµ®åå°æ°ç¹æ°åããã®ã¾ã¾ä½¿ã£ã¦ãã®ã¯ééããªãã®ã§Cè¨èªã§åãäºãã£ã¦ã¿ããCè¨èªããããã¤ãåã®HEXãã³ãã追å ããã以ä¸ã®ãããªã³ã¼ããå®è¡è´ãã¾ãã
#include <vcl.h> #pragma hdrstop #include <tchar.h> #include <stdio.h> #include <stdlib.h> void test(double v) { unsigned long long *p = (unsigned long long *)&v; printf("%f\r\n", v); printf("%llx\r\n", *p); printf("%d\r\n", (int)v); } int _tmain(int argc, _TCHAR* argv[]) { puts("----"); test(0.201 * 1000); puts("----"); test(2.01 * 100); puts("----"); test(20.1 * 10); puts("----"); test(201); getchar(); return 0; }
以ä¸ã®ãããªçµæã«ãªãããã§ãã
---- 201.000000 4069200000000000 201 ---- 201.000000 40691fffffffffff 200 ---- 201.000000 4069200000000000 201 ---- 201.000000 4069200000000000 201
ãã¯ãã0.201 * 1000ãã¨ã2.01 * 100ãã®çµæã¯ãå é¨çã«ã¯ç°ãªãå¤ã«ãªã£ã¦ãããã ãæããããæããã俺å¯ããããã§ã¯ä½ã®è§£æ±ºã«ããªããã®ã§è¿½è©¦ãã¦ã¿ããã¤ãã§ã«floatã®å ´åã試ãã¦ã¿ããã¨ã«ããã
#include <vcl.h> #pragma hdrstop #include <tchar.h> #include <stdio.h> #include <stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { puts("----"); double x = 0.201 * 1000; double y = 2.01 * 100; unsigned long long *xp = (unsigned long long *)&x; unsigned long long *yp = (unsigned long long *)&y; printf("x=%f\r\n", x); printf("x=%llx\r\n", *xp); printf("y=%f\r\n", y); printf("y=%llx\r\n", *yp); if(x == y) { puts("x == y"); } else { puts("x != y"); } puts("----"); float x2 = 0.201 * 1000; float y2 = 2.01 * 100; unsigned long *xp2 = (unsigned long *)&x2; unsigned long *yp2 = (unsigned long *)&y2; printf("x2=%f\r\n", x2); printf("x2=%x\r\n", *xp2); printf("y2=%f\r\n", y2); printf("y2=%x\r\n", *yp2); if(x2 == y2) { puts("x2 == y2"); } else { puts("x2 != y2"); } getchar(); return 0; }
---- x=201.000000 x=4069200000000000 y=201.000000 y=40691fffffffffff x != y ---- x2=201.000000 x2=43490000 y2=201.000000 y2=43490000 x2 == y2
ãã¯ãã0.201 * 1000 != 2.01 * 100ããªã®ã ã£ãã10é²ã§ãããã°ããªã³ããã¦ãéãä½ãæªãã®ãæ°ã¥ããªãã®ã§ãããæµ®åå°æ°ç¹æ°æãï¼ï¼ãªãfloatã®å ´åã¯äººéçãªåä½ã¨ãªã£ããããã¡ããæ°å¤ã«ãã£ã¦ã¯doubleã®201ã¨åããã¨ãèµ·ããã¨èããããã
æµ®åå°æ°ç¹æ°æ¼ç®ã§èª¤å·®ãåºãã®ã¯å½ããåã ããæ¼ç®çµæããè¦ãæãåãã§ãããã¨ããç¹ã§ãã¡ãã£ã¨ããã¯ãã ããªã¼æã£ã次第ãããããè¯ããããã¿ã¼ã³ã§200.999999ã¨ãã«ãªã£ã¦ã¦ãããã°è©±ã¯æ©ãã®ã ãããã£ã±å¯ããï¼ãã