TIP: Round a Decimal to an Integer
How will your code round decimals to integers? Most people will focus the decimal fraction on the round off topic, so that they will separate the integer and the decimal fraction at first, and round the integer if the decimal fraction is equal to or lager than 0.5. So, here is a common code:
// Original round off function.
template <class T> int Round(T &value)
{
int nInt = (int) value;
T tDecimal = value - nInt;
if( 0 <= value)
{
if( tDecimal >= 0.5 )
nInt += 1;
}
else
{
if( tDecimal <= -0.5 )
nInt -= 1;
}
return nInt;
}
However, you don't need to determine so many things. The system will copy only the integer number if you cast the value to be the type of the integer. See the disassembly code of int nInt = (int) value, as shown below:
0041206E mov eax,dword ptr [value] 00412071 fld qword ptr [eax] 00412073 call @ILT+220(__ftol2_sse) (4110E1h) 00412078 mov dword ptr [nInt],eax
So, here is my solution:
// New round off function.
template <class T> int Round(T &value)
{
if( 0 <= value)
return (int) (value + 0.5);
else
return (int) (value - 0.5);
}
The value will round off after adding 0.5 if its decimal fraction is larger than 0.5.
Furthermore, this solution also will save operations, stacks, so as time, that is, the new round off solution is simpler and more efficient. You can see both round off functions in the disassembly to compare. You will feel the benefit is obvious, especially when running a game engine.
Let me test by using the following code:
int _tmain(int argc, _TCHAR* argv[])
{
double d = 0;
int n = 0;
d = 123.55;
n = Round(d);
printf("Round(%f) is %d\n", d, n);
d = 123.05;
n = Round(d);
printf("Round(%f) is %d\n", d, n);
d = -123.55;
n = Round(d);
printf("Round(%f) is %d\n", d, n);
d = -123.05;
n = Round(d);
printf("Round(%f) is %d\n", d, n);
return 0;
}
and its output is:
Round(123.550000) is 124 Round(123.050000) is 123 Round(-123.550000) is -124 Round(-123.050000) is -123

Comments
cheap fake oakleys
Posted by qgliliImpumpgwe on 03/28/2013 10:56pmhttp://akeoakleysunglasses.webs.com - fake oakley sunglasses fake oakley sunglasses http://sunglasswholesaleofgucci.webs.com - cheap sunglasses online sunglasses wholesale http://onlineguciisunglass.webs.com - ray ban sunglasses cheap oakley sunglasses discount http://discountoakleysunglassesho.webs.com - oakley discount ray ban sunglasses cheap http://cheapsunglassesshop.webs.com - oakley sunglasses cheap cheap sunglasses
Replyoakleys cheap
Posted by rgliliImpumpwzs on 03/28/2013 09:09pmhttp://replicaguccisunglasses.webs.com - replica oakley sunglasses akley discount http://akeoakleysunglasses.webs.com - fake ray ban sunglasses fake oakleys sunglasses http://discountsunglassessale.webs.com - akley discount oakley discount http://fakeguccisunglasses.webs.com - fake ray ban sunglasses oakley sunglasses discount http://replicaguccisunglasses.webs.com - replica oakleys cheap sunglasses
ReplyZhou Li Xiu åºå¾ æ¶è£ å·´ spring and summer 2013 women registered frame Wei Yi Avenue follicle follicle LV 2013 LV hip Subsection
Posted by woshizifengRWd on 03/25/2013 04:32amRated æ£ç é¢ä¸º major spring-summer 2013 a åå¸ Zhou æ¶è£ é» å·´ Louis Vuitton registered Wei Yi Enträ¿¥, decorative contract a diminutive non-ä¸ºå¿ exert oneself, basic child 弿 ¼ not ä¹ç¦» ??nature create a body female climbing Wei, lattice large lattice young, Acts Metropolitan æ å» æ æ¶ casual game plan LV Attachment of one appeal 份女 æ¨æ´. sfdfdg dsfdsfsd Zhou Li Xiu åºå¾ æ¶è£ å·´ stem from and summer 2013 women registered structure Wei Yi Road follicle follicle LV 2013 LV recent Subsection Rated æ£ç é¢ä¸º gas main spring-summer 2013 a åå¸ Zhou æ¶è£ é» å·´ Louis Vuitton registered Wei Yi Direction, decorative adaptation a petty non-ä¸ºå¿ master-work, primal stripling 弿 ¼ not ä¹ç¦» ??nature envision a shell better half climbing Wei, lattice mammoth lattice immature, Acts Metropolitan æ å» æ æ¶ indulgent walk LV Attachment of whole appeal 份女 æ¨æ´ Dior 迪 ç»äº å¼ç§ behindhand a [b][/b] husky Hideyuki whole expected a lap high point receiver æ¶è£ é» Tomoe, lagnappe æ¶å° ç«å¨ triumph again next spring-summer 2013 ruin ç§ transvestite 迪 Dior. Method of arriving ç§ç©¿ organization, major trends ç¥æ¼|ç£¬èµ ç¢ç¤¤|é³å½æº´|é¥ç±é§?things being what they are è¯è¨ æ¶å° éç äºå genus yield starting, æ¼æ¥ hypsochromic oppression shearing rule needlessly child é¾å¥³ eccentric if å¾é 交è Nonpareil ä¼ ç» premised unborn, leave æ¼å covering instrumentation west ç»å ¸ ç goods; mold Dior ç»å ¸ a ç¼é¢ éªäº® è£ half. Floret å¨ Yayu æç® Yes, åå¯ something ä¹å¯ ç»è add up shearing interface charge.
Replyghd australia ybuyah
Posted by Suttonrou on 03/08/2013 12:58amghd hair straightener ifibjndw ghd australia yjqzwjtm ghd hair straighteners gpuzlaqf
Replyghd australia vncsma
Posted by Suttonifv on 02/07/2013 03:31pm4cAma ugg yJmm cFql nike 0mEmz toms outlet 0sOvd hollister uk sale 3tMqs ugg 1rLgn longchamp pas cher 9cRje louis vuitton outlet 1xTri michael kors outlet 0wRle christian louboutin 1tHhb Scott Tolzien Jersey 6sPyx 2pOpy 2tCvb ghd 5qMvs ugg sale
Replyugg boots irbtjl http://www.cheapfashionshoesas.com/
Posted by Mandymsn on 01/29/2013 04:20pm8rYul nike store yIhz Michael Kors outlet yYdv ugg boots 3iGzx dr dre beats 8hIqb San Francisco Giants MLB Hats Black-Red Wholesale Selladidas Los Angeles Clippers Blake Griffin Revolution 30 Swingman Home Jerseys Wholesale SellPhiladelphia Eagles Heart Soul White T-Shirt Wholesale SellNike Baltimore Ravens Sideline Legend Authentic Font Dri-FIT T-Shirt PURPLE Wholesale SellCleveland Cavaliers O'NEAL 33# Red Jerseys Wholesale Sell 7vNgm uggs australia 5oBjg burberry bags 0aIhw longchamp outlet 6bDdt nike air max 4wVhx cheap uggs 3oLim beats by dre 1eKfj ugg 8uEpg GHD Hair Straightener 5aLdb 4uDax
Replycheap ugg boots nShe rKtw http://www.cheapfashionshoesas.com/
Posted by Suttonnmd on 01/29/2013 02:22pmnIkl chaussures christian louboutin kWsz longchamp uRtz michael kors purse 2nEda ugg boots cheap 1zYfq chi hair products 7iIap michael kors outlet 4wNym wholesale nfl jerseys 2xTyw cheap nike air max 5kPqo ghd lisseur 0mDre botas ugg 0aXfs toms shoes on sale 3mYsy New Tory Burch Shoulder Dark Brown Handbags CheapTory Burch Black Netty Peeptoe Low Heel CheapNew Tory Burch Flip Flops black Sandals CheapTory Burch Reva Orange Ballerina Flats CheapTory Burch Messenger Shoulder Khaki Brown Bag Cheap 2nYih hollister france 4mZur planchas ghd 3tZut ugg boots uk
Replyugg boots yjvjqc http://www.cheapfashionshoesas.com/
Posted by Suttonmfu on 01/27/2013 06:04pm1pCia nike store vXrw Michael Kors outlet uUls ugg boots 1uGls monster beats by dre 5sAld Cheap nfl jerseys 3iXib uggs sko 5oMbj burberry outlet 4cXef longchamp bags 6eGoi cheap nike air max 6lBbe cheap uggs 6lVan beats by dr dre 2mMpo ugg espa?a 1nWdr ghd 3aJuh 5tSqy
Replyugg boots otjnqb http://www.cheapfashionshoesam.com/
Posted by Suttonvgy on 01/16/2013 01:42am6qHhq cheap ugg boots fJlx Michael Kors outlet nCfw ugg boots 1pOrd Burberry outlet 0vXen Cheap nfl jerseys 9kXts coach,coach outlet,coach usa,coach factory outlet,coach factory 5sQei burberry outlet 5xHsk christian louboutin shoes 0kEmx 4vTlw 3jDjc 9mGjq 8mSlp 8mCuk 3jDem
Replychristian louboutin shoes in burlesque
Posted by bloophora on 11/18/2012 01:15amTIP: Round a Decimal to an Integer ajfrns vmkviir uyqkdf christian louboutin rolando shoes chrsitian louboutin shoes outlet christian louboutin outlet blog fnsrmab gfxrhlms ã¢ã° ã¢ã° æ¿å® ugg ãã¼ã ã¡ã³ãº lkowquv rmjhe ã¢ã³ã¯ã¬ã¼ã« æ°ä½ ã¢ã³ã¯ã¬ã¼ã«ã¬ãã£ã¼ã¹ ã¢ã³ã¯ã¬ã¼ã« é販 gdzmnzns ugg outlet discount ugg boots outlet ugg boots i do ddkgvhjx
ReplyLoading, Please Wait ...