# Haversine Formulas (Or ACOS/COS/SINor SQT) Not functioning with Sensor Location

**URL:** https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592
**Category:** Issues / Bugs
**Tags:** help
**Created:** [December 17, 2023, 9:35pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592 "2023-12-17T21:35:58Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 17, 2023, 9:35pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/1 "2023-12-17T21:35:58Z")

</div>

Anyone having issues with syncing live LAT and LONG to the haversine formula. This started not too long ago. I went back through and tried some elements to resolve variable operations.

But not getting correct math. I have ran it against Excel Formula for measuring distance. I have it converting into yards. It worked not too long ago.

The app keeps jumping between two calculations. Even though I move around right now it either goes to 97 yards or 138 yrds.

Not sure if it is functionality with coded blocks.

0.07922

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 17, 2023, 9:41pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/2 "2023-12-17T21:41:41Z")

</div>

or it bounces into 0.0560168856

Its not even close.

Excel is spot on. Using this formula

=ACOS(COS(RADIANS(90-Lat1)) \*COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) \*SIN(RADIANS(90-Lat2)) \*COS(RADIANS(Long1-Long2))) \* 3958.76

I then multiply that number by 1760( yards in a mile)

Spot on.

App is not

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [December 17, 2023, 11:41pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/3 "2023-12-17T23:41:21Z")

</div>

The first rule of getting help on the forums is to show your work. But we can’t see your blocks and we don’t have a link to your project. So there’s no way we can help you yet. See this: [How to ask Great Questions v2.0](https://community.thunkable.com/t/how-to-ask-great-questions-v2-0/2689983).

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 19, 2023, 8:37pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/5 "2023-12-19T20:37:28Z")

</div>

![App code error 1](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/3/8338864f2cb5816a394bdce78e6813148cc9e592.png)  
 ![app code error 2](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/f/2f29fa1abc3ce3a66d263a9a57d5ae99d183dce0.png)  
 ![app code error 3](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/e/1e6643ad727b5225918a88fdf360154b69f42f2b.jpeg)

Here is what Im comparing to in excel. the 108 is correct. 139 is not. The precalculation isnt correct. I even put in (PI) vs. the pie symbol in thunkable. Even adding the symbol back in it doesnt work.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 19, 2023, 8:57pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/6 "2023-12-19T20:57:06Z")

</div>

I also adjusted the multiplier (3959.87) just to see if anything changes

Not sure why they are different. I have messed around with a ton.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 19, 2023, 9:02pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/7 "2023-12-19T21:02:57Z")

</div>

I changed the multiplier to 3050. Numbers match. But, that means one of the math flows is not correct

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 19, 2023, 11:28pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/8 "2023-12-19T23:28:49Z")

</div>

Just sent some details to you

Thanks!!

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 20, 2023, 3:27am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/9 "2023-12-20T03:27:14Z")

</div>

I have also tried converting all variables into text blocks. So it is absolute. Still the same math error. This was working not too long ago. Very frustrating. Not sure which update caused the issues.

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [December 20, 2023, 4:49am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/10 "2023-12-20T04:49:18Z")

</div>

> [@cresto.pgaxq2](#):
>
> Just sent some details to you

In a private message? I don’t see one from you.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 20, 2023, 5:27am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/11 "2023-12-20T05:27:27Z")

</div>

Just in the messenger

I’m looking back at old app inventor logs

Would love to process this through excel 365 to do the math as data inputs.

But want the calculations to go fast through the app.

I’m not sure why it’s not computing.

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [December 20, 2023, 6:36am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/12 "2023-12-20T06:36:04Z")

</div>

> [@cresto.pgaxq2](#):
>
> Just in the messenger

What do you mean by that? The only way I’m aware that you can contact another user is through private messages or by notifying them using the @ symbol like @cresto.pgaxq2.

I sent you a project by PM that works for me. But you mentioned the calculations are off so I’m not sure what else to recommend.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 21, 2023, 4:59am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/13 "2023-12-21T04:59:18Z")

</div>

Do you get the same output in your app from the last screenshot I sent from the third party site.

If you use the lat and longs I saved

Please let me know

Or if you have any insight into the calc error

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [December 21, 2023, 5:24am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/14 "2023-12-21T05:24:50Z")

</div>

You have access to my project but if you need me to run the calculations using your lat/long values then please post them as text so I can copy and paste them into the project preview.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 24, 2023, 6:45pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/15 "2023-12-24T18:45:39Z")

</div>

Here is a link to the project where the app math is not correct.

[https://x.thunkable.com/projects/6587784c856ddf4fb5cabc7d/1bcce8b7-3d25-409a-93b8-75deecebd725/blocks](https://x.thunkable.com/projects/6587784c856ddf4fb5cabc7d/1bcce8b7-3d25-409a-93b8-75deecebd725/blocks)

App says one thing based on Havesine and Google Sheets says another. The google sheets is accurate. App isnt.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/c/acdffec737d7dee24988365c591810c7cf04b719.png)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/f/5ffcc8e4cf8c87c466a12be11ada5007d3572561.png)

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [December 24, 2023, 7:37pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/16 "2023-12-24T19:37:40Z")

</div>

This is what I’ve always used for my [Air Quality app](https://community.thunkable.com/t/find-your-aqi-air-quality-app/1001836) and it’s been fine for my needs which are to the nearest 0.25 miles. I haven’t tested it with more specificity than that.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/5/058e8ed87917537ed0760c534c340b7001c242b3.png)

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 25, 2023, 10:04am UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/17 "2023-12-25T10:04:45Z")

</div>

first of all, the formula is approximate (see wiki [Haversine formula - Wikipedia](https://en.wikipedia.org/wiki/Haversine_formula)) and answer varies depending on where you are on the globe - near the pole or equator - and the proximity of the points you’re measuring.

in your example, if you look at that lat longs for the 2 points,they’re practically adjacent! as if you’re measuring the distance from one corner of your garden to the diagonally opposite corner!  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/1/c1fcb84ef69f0117cb170e17b6a8c92092bed06b.png)

i’ve proved that our thunkable formula works by using this example from [Haversine formula - Rosetta Code](https://rosettacode.org/wiki/Haversine_formula)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/6/365f84dfd3a43c56d5b98dd5460e070ef9d4264a.png)

and i got an answer of 2887.35km (the average answer is 2887.26+ km).

so i know that the formula works.

however your example hovers on an extreme case - the points are so close  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/c/cceeb3267c2574658d51edbd3becd9929e2c3a90.png)

(note: Using 4 decimal places gives a precision of about 11 meters at the equator and about 7 meters at 60 degrees)

after plugging them into into the formula i eventually got these values for variable w and variable v  
w=0.345418744  
v=0.654581256

if you follow the formula we need to compute acos(w+v) - but w+v happens to be 1.00 and acos(1) function means - how many degrees would an angle be such that its cosine is 1? the answer is , according to trigonometry, supposed to be 0 (zero) - ie. cosine of zero is 1.0

somehow (w+v) is not exactly 1.0 and excel stores the values at a higher precision such that w+v is not exactly 1. hence it is able to return a value for acos(w+v) - a value of 1.54516E-05 (which is a very small value, close to zero)

thunkable’s acos is not so precise - it looks like it’s precise only to 5(6?) decimal points and it comes back with a value of 0.000020 (compared to excel’s 0.0000154516)

so i suggest,you apply this formula only to points that are greater than 1 degree each way - by lat or by long.

the formula works very well for computing distances (approximate) between 2 points that are not adjacent.

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 25, 2023, 3:57pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/18 "2023-12-25T15:57:16Z")

</div>

Merry Christmas !!

So better to use KM instead of miles.

Or switch to this formula

 ![IMG_7990](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/c/5cb91e6911ace3dfdd9dd9d32e96fc04559b7786.jpeg)

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 25, 2023, 5:20pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/19 "2023-12-25T17:20:07Z")

</div>

/The Vincenty Formula

---

<div class="post-metadata">

### Author: ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)
#### Post date: [December 25, 2023, 5:40pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/20 "2023-12-25T17:40:50Z")

</div>

You’ll get the same result. The formula we"re using in the thunkable demo i provided (c=acos(sin(lat1(\*sin(lar2) … blah blah) is derived from the set of variables you gave. I’ve used all those formula in excel and proved that t single formula so it won’t make a difference.

Merry Christmas to you too!

---

<div class="post-metadata">

### Author: ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)
#### Post date: [December 25, 2023, 5:45pm UTC](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592/21 "2023-12-25T17:45:01Z")

</div>

I see what you’re saying.

The app I’m creating doesn’t have a static value to calculate on.

Current lat and current long would be live pulls from the device current lat and long.

So if it is really close , the accuracy won’t be there?

[Next page](https://community.thunkable.com/t/haversine-formulas-or-acos-cos-sinor-sqt-not-functioning-with-sensor-location/2753592.md?page=2)
