• <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.¢%

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

            How to compare double or float in Cpp

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

            簡單方便型:

            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 "
            国产精品一区二区久久不卡 | 激情伊人五月天久久综合| 久久精品成人欧美大片| 久久久久无码精品国产不卡| 精品久久久久久久国产潘金莲| 欧美激情精品久久久久久久| 亚洲精品蜜桃久久久久久| 久久综合给合久久狠狠狠97色| 狠狠色丁香婷综合久久| 无码乱码观看精品久久| 97视频久久久| 色婷婷久久综合中文久久蜜桃av| 成人资源影音先锋久久资源网| 深夜久久AAAAA级毛片免费看| 蜜桃麻豆WWW久久囤产精品| 久久精品国产亚洲AV高清热 | 久久强奷乱码老熟女网站| 久久精品国产精品亚洲精品 | 久久综合给合久久狠狠狠97色69| 久久综合给久久狠狠97色 | 欧美伊人久久大香线蕉综合| 久久精品国产亚洲AV电影| 国产伊人久久| 99麻豆久久久国产精品免费| 国产精品久久久99| 久久久国产精品亚洲一区| 久久人人超碰精品CAOPOREN| 久久精品人人做人人爽97| 亚洲国产成人精品女人久久久 | 国内精品久久久久影院日本| 亚洲国产成人精品女人久久久 | 久久99精品久久久久久齐齐| 久久亚洲春色中文字幕久久久| 久久亚洲中文字幕精品一区| 免费观看久久精彩视频| 久久av无码专区亚洲av桃花岛| 久久久99精品成人片中文字幕 | 少妇熟女久久综合网色欲| 国产精品伊人久久伊人电影| 国产成人精品久久一区二区三区| 综合久久国产九一剧情麻豆|