I have two fields for storing geolocation data, defined as doubles in my MySQL database:
`address_geo_latitude` float(10,6) NOT NULL,
`address_geo_longitude` float(10,6) NOT NULL
And I'm using Yii2's double
validator over values passed by user:
public function rules()
{
return [
[['address_geo_latitude', 'address_geo_longitude'], 'double', 'min'=>0, 'max'=>360]
];
}
(though my tests seems to be proving, that this issue has nothing to do with Yii2 validators)
During tests I've observed strange (?) changes of values, i.e.:
359.90
becomes359.899994
(0,000006
difference),359.80
becomes359.799988
(0,000012
difference),311.11
becomes311.109985
(0,000015
difference),255.55
becomes255.550003
(-0,000003
difference),205.205
becomes205.205002
(-0,000002
difference),105.105
becomes105.105003
(-0,000003
difference).
but:
359.899994
remains359.899994
,311.109985
remains311.109985
,311
remains311
,255
remains255
,200
remains200
,75.75
remains75.75
,11.11
remains11.11
.
What am I missing? I can't see any pattern or logic behind these.
Is this, because I have an incorrect MySQL's field declaration for this kind of data? If yes, then what is the correct one? Few different answers:
- Database/SQL: How to store longitude/latitude data?
- What datatype to use when storing latitude and longitude data in SQL databases?
- What is the ideal data type to use when storing latitude / longitudes in a MySQL database?
suggests, that using float(10,6)
is the best option, if not using MySQL's spatial extensions.
My tests seems to be proving, that this issue has nothing to do with Yii2 validators, because value remains correct until re-read from database:
print_r(Yii::$app->request->post()); //Correct!
print_r($lab->address_geo_latitude); //Correct!
if ($lab->load(Yii::$app->request->post(), 'Lab') && $lab->save()) {
print_r($lab->address_geo_latitude); //Correct!
$lab2 = $this->findModel($lab->id);
print_r($lab2->address_geo_latitude); //<-- HERE! Incorrect!
}
My question is on contrary to this one. My numbers gains, not looses, accuracy! And only for certain numbers, not always.