dbTalk Databases Forums  

[BUGS] BUG #2037: user function call unexpected "input out of range"

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #2037: user function call unexpected "input out of range" in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tom
 
Posts: n/a

Default [BUGS] BUG #2037: user function call unexpected "input out of range" - 11-12-2005 , 10:30 AM







The following bug has been logged online:

Bug reference: 2037
Logged by: Tom
Email address: lackey (AT) ltu (DOT) edu
PostgreSQL version: 8.1-beta4
Operating system: Windows XP
Description: user function call unexpected "input out of range"
Details:

--No database needs to be selected.
--Just cut and paste into a file.
--All remarks are in comments.
--
--From the psql prompt use "\i fileName".
--
create function find (numeric,numeric,numeric,numeric) returns double
precision
as
$$

--Calculate distance
select acos
(
sin($1*pi()/180)*sin($3*pi()/180)
+
cos($1*pi()/180)*cos($3*pi()/180)
*cos(($2-$4)*pi()/180)
)*60*1.1515*180/pi();



$$
language SQL;

--Number 1.
select find(42.3202,-83.2687,42.3202,-83.2687);
--Number 2.
select find(42.320,-83.2687,42.3202,-83.2687);
--Number 3.
select find(42.3338,-83.1818,42.3338,-83.1818);

\df+ find;
drop function find(numeric,numeric,numeric,numeric);


/* This surfaced when calculating the distances between zip codes using
latidtudes and longitudes. When the latitudes and longitudes are the same
the distance should be zero. Number 1 gives the error. Number 2 differs by a
digit in the last decimal place and works. Number 3 is another test with
identical latitudes and longitudes and does calculate zero.

The "DROP FUNCTION" was only put in so I could repeatly re-execute the
script.


The ".msi" was used for the install accepting all of the defaults.

Thanks, Tom

*/

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] BUG #2037: user function call unexpected "input out of range" - 11-12-2005 , 11:21 AM






"Tom" <lackey (AT) ltu (DOT) edu> writes:
Quote:
Description: user function call unexpected "input out of range"
Why does this surprise you? Floating point computation is inherently
inexact, so coming out with a value fractionally greater than 1 for
the acos() argument doesn't seem all that unlikely. You probably
ought to add some code to clamp the result to the legal range.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org


Reply With Quote
  #3  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: [BUGS] BUG #2037: user function call unexpected "input out of range" - 11-12-2005 , 11:32 AM



On Fri, Nov 11, 2005 at 09:26:47PM +0000, Tom wrote:
Quote:
This surfaced when calculating the distances between zip codes using
latidtudes and longitudes. When the latitudes and longitudes are the same
the distance should be zero. Number 1 gives the error. Number 2 differs by a
digit in the last decimal place and works. Number 3 is another test with
identical latitudes and longitudes and does calculate zero.
If you use psql and increase VERBOSITY you can see where the error
is happening:

test=> \set VERBOSITY verbose
test=> select find(42.3202,-83.2687,42.3202,-83.2687);
ERROR: 22003: input is out of range
CONTEXT: SQL function "find" statement 1
LOCATION: dacos, float.c:1602

"dacos" is the internal name of PostgreSQL's acos() function. I'd
guess the value that acos() receives is a wee bit bigger than 1.0
due to rounding and the inaccuracy of representing floating point
numbers in binary; you don't see the problem with another pair of
identical locations because the value calculated to the 16th or so
digit is slightly different.

Your function uses the law of cosines; consider using haversine
instead.

http://www.movable-type.co.uk/scripts/GIS-FAQ-5.1.html

If you're working with geospatial data then you might want to look
at PostGIS.

http://postgis.refractions.net/

See also PostgreSQL's contrib/earthdistance module.

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq


Reply With Quote
  #4  
Old   
Tomas Zerolo
 
Posts: n/a

Default Re: [BUGS] BUG #2037: user function call unexpected "input out of range" - 11-12-2005 , 02:16 PM




--8t9RHnE3ZwKMSgU+
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Nov 11, 2005 at 09:26:47PM +0000, Tom wrote:
Quote:
=20
The following bug has been logged online:
=20
Bug reference: 2037
Logged by: Tom
Email address: lackey (AT) ltu (DOT) edu
PostgreSQL version: 8.1-beta4
Operating system: Windows XP
Description: user function call unexpected "input out of range"
Details:=20
[ basically acos(sin(w1)*sin(w2) + cos(w1)*cos(w2)*cos(l1-l2)) ]

Seems to me that you are hitting an unfortunate roundoff error, where
the argument to acos is slightly greater than 1 (it will be
mathematically 1 in the case w1=3D=3Dw2 and l1=3D=3Dl2).

Maybe you should limit the argument to acos to -1..+1?

regards
-- tom=C3=A1s

--8t9RHnE3ZwKMSgU+
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD4DBQFDdiGBBcgs9XrR2kYRAk1aAJ96o691N5FUIEipsn+fPm gPUPNe4QCVGxoK
U6TeuaryPqzjAIostqzudw==
=Zd7f
-----END PGP SIGNATURE-----

--8t9RHnE3ZwKMSgU+--



Reply With Quote
  #5  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: [BUGS] BUG #2037: user function call unexpected "input out of range" - 11-12-2005 , 02:21 PM



On Fri, Nov 11, 2005 at 21:26:47 +0000,
Tom <lackey (AT) ltu (DOT) edu> wrote:
Quote:
--Calculate distance
select acos
(
sin($1*pi()/180)*sin($3*pi()/180)
+
cos($1*pi()/180)*cos($3*pi()/180)
*cos(($2-$4)*pi()/180)
)*60*1.1515*180/pi();
In addition to the other comments, you don't want to calculate distance this
way. It isn't very accurate when $2 and $4 are nearly equal which is the
normal case.
Do a search for haversine.

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.