wbednarz Posted October 24, 2010 Share Posted October 24, 2010 I am very new to PHP and programming in general. About 3 weeks into this. I have this simple expression and some echo in my code. $x1 = $lat_b + $lat_a1; echo "<br>lat b: ".$lat_b; echo "<br>lat a1: ".$lat_a1; echo "<br>x1: ".$x1; $lat_b and $lat_a1 are derived from some xml extractions from a yahoo geocode api (that part works fine). The results of the expression and echos above are as follows: lat b: 40.402866 lat a1: 40.252590 x1: 80 Why is $x1 rounding automatically on me? Later, I substituted the expression above with $x1 = 40.402866 + 40.252590 ; The new result (shown below) makes sense to me. lat b: 40.402866 lat a1: 40.252590 x1: 80.655456 Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/ Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 Hard to say what's happening there. When I paste your original code block in and run it locally, it produces the correct result. Did you check the html source to make sure something isn't getting mangled somehow? lat b: 40.402866 lat a1: 40.25259 x1: 80.655456 Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1125958 Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 It works fine for me... But I do remember having this problem years ago! One, try using sprintf to format the output (http://uk3.php.net/manual/en/function.sprintf.php) Otherwise try initialising $c to a float. $c=0.0; Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1125961 Share on other sites More sharing options...
wbednarz Posted October 25, 2010 Author Share Posted October 25, 2010 The datatypes of $lat_b and $lat_a1 were set to object. This was from the xml extraction (see example below). I added the (float) type casting attribute to the expression to fix the problem. Thanks for the help. $xml_a1=simplexml_load_file("http://where.yahooapis.com/geocode?location={$score_capabilities_z1['si_cmp_zip']}=myapiid"); $lat_a1= (float) $xml_a1->Result->latitude; Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126160 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 Well done, so this is solved then! Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126167 Share on other sites More sharing options...
wbednarz Posted October 25, 2010 Author Share Posted October 25, 2010 The next expression, which is more complicated, has a similar issue. The (float) attribute in the expression does not seem to solve the problem this time. It looks like rounding is occurning within the expression. $score_capabilities_d1= (float) ((69.1*($lat_b-$lat_a1))^2+(53*($lon_b-$lon_a1)*cos($lat_a1/57.3))^2)^0.5; The result of this variable should be 10.4238... Instead, I just get 10. I am taking geocode data (latitude and longitudes from a zip code) of two locations and trying to get the distance between them. Well done, so this is solved then! Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126170 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 you'll need to add 'float' in a few more places then... Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126174 Share on other sites More sharing options...
wbednarz Posted October 25, 2010 Author Share Posted October 25, 2010 I figured I should post the solution to this problem. When I used a carat to square (and square root) a portion of the expression, the data type would change to integer. Not sure why this is the case. I used the pow(x,y) and sqrt() functions to fix this. $score_capabilities_d1=round(sqrt(pow(69.1*($lat_b-$lat_a1),2)+pow(53*($lon_b-$lon_a1)*cos($lat_a1/57.3),2)),1); The next expression, which is more complicated, has a similar issue. The (float) attribute in the expression does not seem to solve the problem this time. It looks like rounding is occurning within the expression. $score_capabilities_d1= (float) ((69.1*($lat_b-$lat_a1))^2+(53*($lon_b-$lon_a1)*cos($lat_a1/57.3))^2)^0.5; The result of this variable should be 10.4238... Instead, I just get 10. I am taking geocode data (latitude and longitudes from a zip code) of two locations and trying to get the distance between them. Well done, so this is solved then! Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126192 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2010 Share Posted October 25, 2010 ^ is an exclusive or bitwise operator, not a mathematical operator. Quote Link to comment https://forums.phpfreaks.com/topic/216729-why-is-this-expression-automatically-rounding-on-me/#findComment-1126198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.