Function returns value to cell as Single Function is defined to return Double

pcoiner

New Member
Joined
May 14, 2013
Messages
26
Function Haversine has correct value in debugger but in cell it has the same value as Haversine2.
Is this a known bug?



Public Function Haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
Dim temp As Double
Dim temp1 As Double
Dim temp2 As Double
Dim temp3 As Double
Dim temp4 As Double

temp1 = Math.Cos(WorksheetFunction.radians(90 - lat1)) * Math.Cos(WorksheetFunction.radians(90 - lat2))
temp2 = Math.Sin(WorksheetFunction.radians(90 - lat1)) * Math.Sin(WorksheetFunction.radians(90 - lat2)) * Math.Cos(WorksheetFunction.radians(long1 - long2))
temp3 = WorksheetFunction.Acos(temp1 + temp2)
temp4 = temp3 * 6371# * 1000#
Haversine = temp4
End Function
Public Function Haversine2(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Single
Dim temp As Double
Dim temp1 As Double
Dim temp2 As Double
Dim temp3 As Double
Dim temp4 As Double

temp1 = Math.Cos(WorksheetFunction.radians(90 - lat1)) * Math.Cos(WorksheetFunction.radians(90 - lat2))
temp2 = Math.Sin(WorksheetFunction.radians(90 - lat1)) * Math.Sin(WorksheetFunction.radians(90 - lat2)) * Math.Cos(WorksheetFunction.radians(long1 - long2))
temp3 = WorksheetFunction.Acos(temp1 + temp2)
temp4 = temp3 * 6371# * 1000#
Haversine2 = temp4
End Function

Public Sub Call_it()
Dim x As Double
Dim y As Double
x = Haversine(38.32936982, -104.7080829, 38.329384, -104.708088)
y = Haversine2(38.32936982, -104.7080829, 38.329384, -104.708088)
x = x
End Sub
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to MrExcel.

When used on a worksheet I get 1.6388373364904 for Haversine and 1.63883733749389 for Haversine2. What results do you get?
 
Upvote 0
Me too.

Also put in a break point at the x = x line.
When it gets there..here is the contents of my locals window
: x : 1.6388373364904 : Double
: y : 1.6388373374939 : Double

I would guess that you have your cell's formatted to only 8 decimals or less.
So they are rounded in the cell to 8 decimals, therefor the values 'appear' to be the same.
 
Last edited:
Upvote 0
Welcome to MrExcel.

When used on a worksheet I get 1.6388373364904 for Haversine and 1.63883733749389 for Haversine2. What results do you get?

Hi Andrew
Same/That is correct.
If you call Haversine from a cell you get 1.63883733749389
if you do this =ACOS(COS(RADIANS(90-A9)) *COS(RADIANS(90-A14)) +SIN(RADIANS(90-A9)) *SIN(RADIANS(90-A14)) *COS(RADIANS(B9-B14))) *6371 * 1000
you get 1.6388373364904.
There in lies my rub
 
Upvote 0
Hi Jonmo1
Formatting is Number with 15 decimal places.
1.638837336490400
1.636085299376870


<colgroup><col></colgroup><tbody>
</tbody>
I'm telling you the function passes back a Single not a Double
 
Upvote 0
Hi Andrew
Same/That is correct.
If you call Haversine from a cell you get 1.63883733749389
if you do this =ACOS(COS(RADIANS(90-A9)) *COS(RADIANS(90-A14)) +SIN(RADIANS(90-A9)) *SIN(RADIANS(90-A14)) *COS(RADIANS(B9-B14))) *6371 * 1000
you get 1.6388373364904.
There in lies my rub

No, for Haversine in a cell I get 1.6388373364904.
 
Upvote 0
Hi Andrew
I might have misread your reply.
When in the Visual Basic editor I get the correct answer.
When in a cell of a worksheet When I call Haversine I get the same answer as Haversine2 from the Visual Basic editor.
 
Upvote 0
Hi Andrew
Any Suggestions.......................got a staff meeting will be right back.
thanks in advance
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,699
Members
449,180
Latest member
craigus51286

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top