• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開(kāi)心的工作
            簡(jiǎn)單、開(kāi)放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            How to compare double or float in Cpp

            Posted on 2010-03-06 16:13 S.l.e!ep.¢% 閱讀(712) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): VC

            簡(jiǎn)單方便型:

            double i=90.02;
            if (? (i-90.02) >= 0.00?)

            How to compare double or float in Cpp

            From MetaSharp

            Jump to: navigation, search

            Article Author(s): Audric Thevenet
            All Rights Reserved.


            language?: english definition of the double type

            The following code is the best way I could come up with to work correctly with doubles. The float version is not provided yet, but after reading a bit the code, you should manage to easily extend it to floats. The most important functions here are:

            • GetExpoBase2() extracts the base2 exponent from the double (putting aside the sign bit and the mantissa)
            • Equals() gets the exponents (e1,e2) of both doubles (d1,d2) and the exponent (e3) of their difference (d1-d2), then the 2 doubles (d1,d2) are considered being equal only if their difference is close enough to 0. This is done comparing the exponents. Example: e3=-50 (meaning 2^-50) e1=-1 e2=-1 then e3-e2=-49 and e3-e1=-49 both less than -48. It means that e1 and e2 are equal because their difference significant bits are located 2^48 lower... hard to tell if it's clear enough, I'll edit later if I come up with a clearer explaination ^^

            Download Double comparison Sample for VC2005

            compare.cpp

            // compare.cpp
            #include "compare.h"
            
            namespace metasharp
            {
            	inline int GetExpoBase2(double d)
            	{
            		int i = 0;
            		((short *)(&i))[0] = (((short *)(&d))[3] & (short)32752); // _123456789ab____ & 0111111111110000
            		return (i >> 4) - 1023;
            	}
            
            	bool	Equals(double d1, double d2)
            	{
            		if (d1 == d2)
            			return true;
            		int e1 = GetExpoBase2(d1);
            		int e2 = GetExpoBase2(d2);
            		int e3 = GetExpoBase2(d1 - d2);
            		if ((e3 - e2 < -48) && (e3 - e1 < -48))
            			return true;
            		return false;
            	}
            
            	int 	Compare(double d1, double d2)
            	{
            		if (Equals(d1, d2) == true)
            			return 0;
            		if (d1 > d2)
            			return 1;
            		return -1;
            	}
            
            	bool	Greater(double d1, double d2)
            	{
            		if (Equals(d1, d2) == true)
            			return false;
            		if (d1 > d2)
            			return true;
            		return false;
            	}
            
            	bool	GreaterOrEqual(double d1, double d2)
            	{
            		if (Equals(d1, d2) == true)
            			return true;
            		if (d1 > d2)
            			return true;
            		return false;
            	}
            
            	bool	Less(double d1, double d2)
            	{
            		if (Equals(d1, d2) == true)
            			return false;
            		if (d1 < d2)
            			return true;
            		return false;
            	}
            
            	bool	LessOrEqual(double d1, double d2)
            	{
            		if (Equals(d1, d2) == true)
            			return true;
            		if (d1 < d2)
            			return true;
            		return false;
            	}
            }
            

            compare.h

            // compare.h
            #ifndef COMPARE_H
            #define COMPARE_H
            
            namespace metasharp
            {
            	int 	Compare(double d1, double d2);
            	bool	Equals(double d1, double d2);
            	bool	Greater(double d1, double d2);
            	bool	GreaterOrEqual(double d1, double d2);
            	bool	Less(double d1, double d2);
            	bool	LessOrEqual(double d1, double d2);
            }
            
            #endif
            
            Retrieved from "
            久久香蕉国产线看观看猫咪?v| 久久国产成人精品麻豆| 国产精品18久久久久久vr| 久久精品国产只有精品2020| 久久这里的只有是精品23| 激情五月综合综合久久69| 国产精品久久久久久久久鸭 | 久久久久亚洲AV片无码下载蜜桃| 99久久成人18免费网站| 国产91色综合久久免费分享| 99精品国产在热久久无毒不卡| 久久婷婷五月综合97色| 人妻少妇久久中文字幕一区二区| 色婷婷综合久久久久中文| 久久久久久九九99精品| 久久se精品一区精品二区| 国产精品成人99久久久久91gav| 国产成人精品久久综合| 天天做夜夜做久久做狠狠| 欧美精品九九99久久在观看| 无码AV中文字幕久久专区| 狠色狠色狠狠色综合久久| 久久久亚洲精品蜜桃臀| 久久久久久国产精品美女| 国内精品久久久久影院优| 亚洲狠狠婷婷综合久久久久 | 日日狠狠久久偷偷色综合0| 久久国产免费直播| 日本三级久久网| 久久亚洲国产成人影院| 99久久99这里只有免费的精品| 99久久亚洲综合精品成人| 国产精品美女久久福利网站| 久久精品国产亚洲AV麻豆网站| 久久精品成人| 久久99精品久久只有精品| 亚洲AV伊人久久青青草原| 久久精品9988| 亚洲va久久久噜噜噜久久狠狠| 青草影院天堂男人久久| 色综合久久久久无码专区|