Note that the IF-ENDIF part is not optional, since round-off errors could make tmp bigger than 1 or smaller than -1.
For those values ACOS is not defined and will return a NULL value.
DELIMITER //
DROP FUNCTION IF EXISTS km_from_deg //
CREATE FUNCTION km_from_deg (latA DOUBLE, longA DOUBLE, latB DOUBLE, longB DOUBLE)
RETURNS DOUBLE
BEGIN
DECLARE tmp DOUBLE;
SET tmp =
COS(RADIANS(longA - longB)) * COS(RADIANS(latB))*COS(RADIANS(latA)) +
SIN(RADIANS(latB))*SIN(RADIANS(latA));
IF tmp > 1 THEN set tmp = 1;
ELSEIF tmp < -1 THEN set tmp = -1;
END IF;
return 6372 * ACOS(tmp);
END;
//
DELIMITER ;
1 comment:
really useful, great!
thankyou
Post a Comment