Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:15:27+00:00 2026-06-08T23:15:27+00:00

I have the problems with my function which needs to calculate distance between given

  • 0

I have the problems with my function which needs to calculate distance between given coordinates. As I can see problem is with negative values, and I am running out of ideas how to solve this, so if someone can help me I will really appreciate it!

DELIMITER $$

CREATE DEFINER=`sfff_user`@`%` FUNCTION `GetUserDistance`(lat VARCHAR (20), lon VARCHAR (20), userLat VARCHAR (20), userLon VARCHAR (20)) RETURNS int(11)
BEGIN
    DECLARE distance INT (11);

    IF ISNULL(lat) OR ISNULL(lon) OR lat = '' OR lon = '' THEN
        RETURN 0;
    ELSE
        SELECT
            3956 * 2 * ASIN(SQRT(POWER(SIN((lat - ABS(userLat)) * PI() / 180 / 2), 2) + COS(lat * PI() / 180) * COS(ABS(userLat) * PI() / 180) * POWER(SIN((lon - userLon) * PI() / 180 / 2), 2)))
        INTO
            distance;

        RETURN distance;
    END IF;

END

For example result for this call:

select GetUserDistance(44, 21, 44, 21) as distance; is 0 which is ok

But look at this:

select GetUserDistance('-15.4167', '28.2833', '-15.4167', '28.2833') as distance; 

is 2129 which is insane!

So if you can take a look it would be verry nice to have correct function, since I am dying to solve this 🙁

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:15:30+00:00Added an answer on June 8, 2026 at 11:15 pm

    For safe side you should CAST VARCHAR to DECIMAL and as @Scharron said no need of ABS try:

    CREATE FUNCTION `GetUserDistance`(arg_lat VARCHAR (20), arg_lon VARCHAR (20), arg_userLat VARCHAR (20), arg_userLon VARCHAR (20)) RETURNS int(11) NO SQL
    BEGIN
        DECLARE distance INT (11);
        DECLARE lat, lon, userLat, userLon DECIMAL(14,4);
    
        SET lat = CAST(arg_lat AS DECIMAL(14,4));
        SET lon = CAST(arg_lon AS DECIMAL(14,4));
        SET userLat = CAST(arg_userLat AS DECIMAL(14,4));
        SET userLon = CAST(arg_userLon AS DECIMAL(14,4));
    
        IF ISNULL(lat) OR ISNULL(lon) OR lat = '' OR lon = '' THEN
            RETURN 0;
        ELSE
            SELECT
                3956 * 2 * ASIN(SQRT(POWER(SIN((lat - userLat) * PI() / 180 / 2), 2) + COS(lat * PI() / 180) * COS(ABS(userLat) * PI() / 180) * POWER(SIN((lon - userLon) * PI() / 180 / 2), 2)))
            INTO
                distance;
    
            RETURN distance;
        END IF;
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have got a problem with calling a global function, which takes a pointer
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have some problems with header() function. It works and doesn't work at the
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
i want to use .animate function but i have some problems i want to
I have problem with function checking collision. No matter what I do it throws
I have a problem with Jquery function getJSON, the action url does not trigger
I have a problem with a function ( setLayerType(LAYER_TYPE_NONE, null) ) available in api

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.