Android Question Trigonometry functions lookup table?

wonder

Expert
Licensed User
Longtime User
From a performance (nanosecond critical) point of view, would there be an advantage in creating such a table (sin, cos) for non-precise operations?

I'm thinking about creating, for example, an array with 36000 elements:
B4X:
Dim cosTable() As Double = _
Array As Double( _
..., _
..., _
...)

'Get's the angle's (degrees) cosine
Sub getCosD(angle As Double) As Double
    NormalizeAngleTo_0_360(angle)
    Return cosTable(angle * 100)
End Sub

Something like this...
 
Last edited:

sorex

Expert
Licensed User
Longtime User
the LUT method should be faster since you bypass extra calls, return from calls which both include stack operations and other things.

where do you get these 36000 items from? there's only 360 angles.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Hummm... let's see...

cos // sin 23.00º = 0.92050485345 // 0.39073112848
cos // sin 23.01º = 0.92043664398 // 0.39089178094

Now image I have a vector, DistEarthMoonBase with magnitude 384400000 meters.

At 23.00º, the Moon Base is at
(x, y): 384400000*0.92050485345, 384400000*0.39073112848 <=>
(x, y): 353842.065666, 150197.045788 kilometers

At 23.01º, the Moon Base is at
(x, y): 384400000*0.92043664398, 384400000*0.39089178094 <=>
(x, y): 353815.845946, 150258.800593 kilometers

deltaX = 26.219720
deltaY = 61.754805

Distance given by Sqrt(deltaX^2 + deltaY^2): 67.090 Kilometers

So... if you're sending a spaceship to a lunar base at 23.01º, but you opt to discard the 0.01º, you'd miss the target by 67 kilometers.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I advise to put a pilot in the spaceship, don't trust those engineers...
Those engineers are landing unmanned spacecraft on bodies all over the solar system...their maths must be spot on I would say.;)

Have you ever read up about Hohman transfer orbits and how for eg get to Mars when launching from earth. There is only a small launching window once in a while to get there and you need to aim at a target that is millions of kilometers away from where you are heading and hope to meet the planet at the time when you get there. It takes some serious calcs to get this right - wether it is a manned or unmanned craft.
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
Now image I have a vector, DistEarthMoonBase with magnitude 384400000 meters.

that's what I was saying, if it is for visual projection stuff you won't see a difference.

when working with large distances that's another thing but when you display them to the screen it won't be noticable either since they all will have that distance problem.

also as @KitCarlson mentioned you could reduce the table since half the sinus is a mirror/negative of the other half.
that division or 0.xx multiplication might slower than the lookup in a really large array tho.
 
Upvote 0
Top