note 103209 added to language.types.float

From: Date: Thu, 31 Mar 2011 21:02:11 +0000
Subject: note 103209 added to language.types.float
Groups: php.notes 
Request: Send a blank email to php-notes+get-178393@lists.php.net to get a copy of this message
<?php $binarydata32 = pack('H*','00000000'); $float32 = unpack("f", $binarydata32); // 0.0 $binarydata64 = pack('H*','0000000000000000'); $float64 = unpack("d", $binarydata64); // 0.0 ?> I get 0 both for 32-bit and 64-bit numbers. But, please don't use your own "functions" to "convert" from float to binary and vice versa. Looping performance in PHP is horrible. Using pack/unpack you use processor's encoding, which is always correct. In C++ you can access the same 32/64 data as either float/double or 32/64 bit integer. No "conversions". To get binary encoding: <?php $float32 = pack("f", 5300231); $binarydata32 =unpack('H*',$float32); //"0EC0A14A" $float64 = pack("d", 5300231); $binarydata64 =unpack('H*',$float64); //"000000C001385441" ?> And my example from half a year ago: <?php $binarydata32 = pack('H*','0EC0A14A'); $float32 = unpack("f", $binarydata32); // 5300231 $binarydata64 = pack('H*','000000C001385441'); $float64 = unpack("d", $binarydata64); // 5300231 ?> And please mind the Big and Little endian boys... ---- Server IP: 69.147.83.197 Probable Submitter: 95.176.201.69 ---- Manual Page -- http://www.php.net/manual/en/language.types.float.php Edit -- https://master.php.net/note/edit/103209 Del: integrated -- https://master.php.net/note/delete/103209/integrated Del: useless -- https://master.php.net/note/delete/103209/useless Del: bad code -- https://master.php.net/note/delete/103209/bad+code Del: spam -- https://master.php.net/note/delete/103209/spam Del: non-english -- https://master.php.net/note/delete/103209/non-english Del: in docs -- https://master.php.net/note/delete/103209/in+docs Del: other reasons-- https://master.php.net/note/delete/103209 Reject -- https://master.php.net/note/reject/103209 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178393) next »