Current Lua downloadable firmware will be posted here

User avatar
By picstart
#64496 Then there is the COORDIC. Some will think of it as binary angles where tan45 is binary 1 and binary 0.1 is tan (26.56) it does a bunch of things calculating mostly just by shifting and adding the sin cos arctan sqrt( x^2+y^2)
It was very useful for calculating during Moon landings with 8 bit computers and slow processors given a signal from an earth based mainframe took seconds to arrive.
User avatar
By alex_g
#64575
Yomero wrote:When I create the arcsin function I am not getting the correct values ​​for the calculated values ​​(-1 to 1) in this function using the series equivalent to the arcsin function of this link https://en.wikipedia.org/wiki/Inverse_t ... _functions.


Hi Yomero!
I copied your code and it seems to work more or less OK, although it converges VERY slowly as you approach 1 or -1. I don't think 60 iterations is going to get you close.

However, I looked at the code and it seems a bit wasteful, so I took the trouble to rewrite it a bit, and now I can get over 35000 iterations before my chip reboots. That's good enough to give the right answer for asin(0.9999) to six decimal places or so. If you need to calculate values near 1 you may be better off with an approximation routine. More "normal" values are fine with just 50 iterations, or less.

Have a tryout, 'n' is the number of iterations, obviously, just hardcode it if there's some value you're happy with.

Code: Select all
function arcsin(x, n, term, sum, a, b, c)
  if n==1 then return sum
  else
    local nextterm = term*x*x*a*a/(b*c)
    --print(sum)
    return arcsin (x, n-1, nextterm, sum+nextterm, a+2, b+2, c+2)
  end
end

function asin(x)
   local n
   local ax = math.abs(x)
   if ax<0.25 then n=10
   elseif ax< 0.5 then n=20
   elseif ax< 0.6 then n=30
   elseif ax< 0.7 then n=40
   elseif ax< 0.8 then n=60
   elseif ax< 0.9 then n=130
   elseif ax< 0.95 then n=250
   elseif ax< 0.99 then n=1200
   -- probably better to use something else for the cases below
   elseif ax< 0.999 then n=10000
   else n = 35000
   end
   return arcsin(x,n,x,x,1,2,3)
end


Last edited by alex_g on Tue Apr 04, 2017 7:30 am, edited 3 times in total.
User avatar
By grytole
#67655 Probably it can be useful for someone:
https://github.com/grytole/iot/blob/master/nodemcu-sketches/trigonometry.lua

According to guys from github it is useless, because you can get a linux, setup toolchains, enable libmath and build own shiny bin file with native math, aren't you? ;)