iwato Posted June 29, 2011 Share Posted June 29, 2011 BACKGROUND: The following function has been tested, perhaps not thoroughly, but certainly enough to believe that it is worthy of further use and consideration. This function reverses the operation of another function that takes a standard IP address and converts it into an easily stored and retrievable decimal whole number.REQUEST: I am hoping that someone can explain the reason for the alternative type-casting between float and integer values in order to perform the conversion. I simply do not understand the need. function dec2ip($dec) { if($dec<0) { $dec=(double) 4294967296+$dec; } if($dec>16777215) { $ip=$dec-(intval($dec/256)*256); $dec=(double) intval($dec/256); } else $ip="0"; if($dec>65535) { $ip=($dec-(intval($dec/256)*256)).".".$ip; $dec=(double) intval($dec/256); } else $ip="0.".$ip; if($dec>255) { $ip=($dec-(intval($dec/256)*256)).".".$ip; $dec=(double) intval($dec/256); } else $ip="0.".$ip; $ip=$dec.".".$ip; return (string) $ip; } STANDARD IP ADDRESS TO DECIMAL INTEGER CONVERSION FUNCTIONFor those unfamiliar with the procedure. function ip2dec($ip_addr) { $base=explode(".",$ip_addr); $decimal= (double) $base[0] * 16777216; $decimal += $base[1] * 65536; $decimal += $base[2] * 256; $decimal += $base[3]; if($decimal > 2147483647) { $decimal -= 4294967296; } return (int) $decimal; } Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted June 29, 2011 Share Posted June 29, 2011 Why exactly should you use these functions to begin with? There's ip2long() and long2ip() being built in.I'm not entirely sure about the particular code, but it appears they use it in order to force proper math basically. Actions between two integers produce an integer, not a double. And math between doubles can be off by the non integer part (the infamous floor((0.1+0.7)*10) for example). Perhaps it's not necesary, but they do it "just in case". Link to comment Share on other sites More sharing options...
justsomeguy Posted June 30, 2011 Share Posted June 30, 2011 I believe they do that for precision, since a floating-point value can be larger than an integer value. Link to comment Share on other sites More sharing options...
iwato Posted June 30, 2011 Author Share Posted June 30, 2011 And math between doubles can be off by the non integer part (the infamous floor((0.1+0.7)*10) for example).So, in PHP 8 is equal to 7.Truly amazing. Mathematicians, beware!Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted June 30, 2011 Share Posted June 30, 2011 That's not unique to PHP, that's how binary arithmetic works. 0.7 doesn't exactly translate perfectly into binary, so it gets approximated. If you leave out the call to floor you'll see the full value, which is just shy of 8, but not by much. PHP does have extensions to accurately deal with numbers like that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.