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

  • Home
  • SEARCH
  • 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 7665191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:29:58+00:00 2026-05-31T14:29:58+00:00

The acos function works in the interval -1 <= x <= 1. Sometimes, because

  • 0

The acos function works in the interval -1 <= x <= 1.

Sometimes, because of rounding, I’m getting 1.00001 to calculate the acos over, then the following where clause is giving me a floating number error:

    where
        (6371 * acos(cos(radians(@ga_where)) * cos(radians(thing_geolocation.ga_thing)) * cos(radians(thing_geolocation.go_thing) - radians(@go_where)) + sin(radians(@ga_where)) * sin(radians(thing_geolocation.ga_thing)))) < 1.5

So, I’ll have to refactor this where clause into something like this:

    where
        (
            6371 *
            acos
            (
                case
                    when cos(radians(@latitude)) * cos(radians(thing_geolocation.ga_thing)) * cos(radians(thing_geolocation.go_thing) - radians(@longitude)) + sin(radians(@latitude)) * sin(radians(thing_geolocation.ga_thing)) > 1 then 1
                    when cos(radians(@latitude)) * cos(radians(thing_geolocation.ga_thing)) * cos(radians(thing_geolocation.go_thing) - radians(@longitude)) + sin(radians(@latitude)) * sin(radians(thing_geolocation.ga_thing)) < -1 then -1
                    else cos(radians(@latitude)) * cos(radians(thing_geolocation.ga_thing)) * cos(radians(thing_geolocation.go_thing) - radians(@longitude)) + sin(radians(@latitude)) * sin(radians(thing_geolocation.ga_thing))
                end
            )
        ) < 1.5 

But, is not sounding good (it will cause to calculate the distance 3x, right?).

I have other ideas, like a subquery or a outerapply, but because the entiry query is very complex and the same fields are used to other purposes, I’m trying to solve it without change the whole query.

ps: I tried to use the point.distance function, but it doesn’t work well with null values, so I choose the keep this way.

Any ideas?

  • 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-05-31T14:29:59+00:00Added an answer on May 31, 2026 at 2:29 pm

    I would suggest changing everything that you’re passing into acos into a user-defined scalar-valued function. Within that function, you can account for values greater than 1.0 or less than -1.0.

    By doing this, you would not have to perform any of the mathematical calculations more than what is necessary.

    I think your function would look something like this:

    CREATE FUNCTION [dbo].[udf_GetACOSInputValueByLatLong]
    (
        @LatStart DECIMAL(10,6),
        @LongStart DECIMAL(10,6),
        @LatEnd DECIMAL(10,6),
        @LongEnd DECIMAL(10,6)
    )
    RETURNS DECIMAL(10,6) AS 
    BEGIN
    
    DECLARE @Result DECIMAL(10, 6)
    SET @Result = ( 
        cos( radians( @LatStart ) ) * 
        cos( radians( @LatEnd ) ) * 
        cos( radians( @LongEnd ) - radians(@LongStart) ) + sin( radians(@LatStart) ) * 
        sin( radians( @LatEnd ) ) 
    )
    
    IF @Result > 1.0
        SET @Result = 1.0
    ELSE IF @Result < -1.0
        SET @Result = -1.0
    
    RETURN (@Result)
    
    END
    

    Which could then be used like the following:

    where
        (
            6371 * acos (YourDatabase.dbo.udf_GetACOSInputValueByLatLong(
                @latitude, 
                @longitude, 
                thing_geolocation.ga_thing, 
                thing_geolocation.go_thing))
        ) < 1.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. I'm computing the angle between two vectors, and sometimes Math.Acos() returns NaN
So: I have the following function, adapted from a formula found online, which takes
ActionScript3 states the following: Note: The Math functions acos, asin, atan, atan2, cos, exp,
I have a function that calculates the distance between two GPS coordinates. I then
The following query returns null in MySQL. I check all things like cos, acos
I have created following stored user defined it gets executed successfully. CREATE FUNCTION spherical_distance1(@a
Any reason why this code sometimes only generates 4 character strings? function genID() {
In the math module, I could only find cos/sin/tan/acos/asin/atan. These take the argument, respectively
I have this query which does work fine in MySQL SELECT ((ACOS(SIN(12.345 * PI()
I have my own, very fast cos function: float sine(float x) { const float

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.