Jump to content

Why is this expression automatically rounding on me?


wbednarz

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.