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 978513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:04:10+00:00 2026-05-16T04:04:10+00:00

The code below is used to calculate the miles between two cities. In this

  • 0

The code below is used to calculate the miles between two cities. In this case, it’s for the distance from Yarmouth, ME to Yarmouth, ME – obviously zero – meaning that results for cities within X miles of Yarmouth should include Yarmouth itself.

The problem is that the latitude and longitude for Yarmouth seem to be causing some kind of floating point problem (I haven’t seen this with other cities):

DECLARE @fromlong FLOAT, @fromlat FLOAT, @tolong FLOAT, @tolat FLOAT, @test FLOAT

SET @fromlong = 43.8219
SET @fromlat = -70.1758
SET @tolong = 43.8219
SET @tolat = -70.1758
SET @test = SIN(@fromlong / ( 180 / PI() )) * SIN(@tolong / ( 180 / PI() )) + COS(@fromlong / ( 180 / PI() )) * COS(@tolong / ( 180 / PI() )) * COS(@fromlat / ( 180 / PI() ) - @tolat / ( 180 / PI() ))

PRINT @test /*** Displays "1" ***/

SELECT 3963.0 * ACOS(@test) /*** Displays "a domain error has occurred" ***/

First, is this a SQL Server bug?

Second, what can I do to get around it? I know in the example above I could have some logic for IF @test > 1, but this example is distilled from a query embedded in a web app (not my choice), so I need to fix the query, i.e. fix the calculation, without resorting to TSQL if possible, and without distorting any other return values. 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-16T04:04:11+00:00Added an answer on May 16, 2026 at 4:04 am

    The consensus from the comments seems to be the use of FLOAT. I only used FLOAT in the example because that was the datatype of the columns from which the latitude/longitude was being read, but that seems to be the heart of the problem. Since I can’t change the datatypes of the columns themselves, the simplest solution was to change the datatype of the resulting calculation, which seems to work fine in all cases:

    SELECT 3963.0 * ACOS(CONVERT(DECIMAL(10, 6), @test))
    

    I realize leaving the calculation as a float could result in small rounding errors, but they are acceptably small for this application. Thanks all who commented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below shows a sample that I've used recently to explain the different
I used the code below to compress files and they keep growing instead of
The code below gives me this mysterious error, and i cannot fathom it. I
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below works. But if I comment out the line Dim objRequest As
The code below gives an error: Property 'Int32 Key' is not defined for type
The code below is the code i am using. It works fine in thunderbird
The code below continues many lines until it ends with a expected /veotherwise /vechoose.
The code below is checking performance of three different ways to do same solution.

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.