I have a table with a point data type that stores the latitude and longitude. I plan to insert from a function, and the input argument is defined as a float(18)
[which is where I suspect the problem comes from].
Let's say the following values are used:
48.277274, -116.5547745
If I insert these with from the command line the database shows those values. But if I insert them from the function the values in the database are:
48.2772750854492, -116.55477142334
I'm not doing anything to modify the values, as I mention above I believe bringing them in as float is causing the problem, but I've tried various other types and only get other issues.
Would greatly appreciate any help.
CREATE OR REPLACE FUNCTION myfun(
float(18), -- lat
float(18)) -- lon
RETURNS integer AS
$BODY$
DECLARE
postcnt integer;
BEGIN
INSERT INTO user(usergeo)
VALUES (point($1,$2))
RETURN 1;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Best Answer
When executing this:
the two decimal constants are implicitly casted to double precision numbers, since it corresponds to the closest matching types for the
point()
function:On the other hand, when executing this:
the two decimal constants are implicitly casted to
real
as the type, because float(n) is equivalent to real when 1<=n<=24, as explained in the documentation on datatypes:This downcast comes with a loss of precision, because, from the same doc page:
Conclusion: use
double precision
instead offloat(18)
, which in fact is a synonym for the less-precisereal
.