4

I need to perform log calculations for a thermistor, however the Lua math library (math.log) doesn't seem to be implemented, or I'm doing something wrong. It's not a module on NodeMCU-build.com or in the docs, either.

Any ideas/suggestions/solutions?

Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
  • 1
    Have you tried if it's actually there, or are you just assuming that because it's not in the documentation? – DarkWiiPlayer Sep 30 '18 at 16:54
  • No, it's not there... I tried running code, and then I looked at the documentation, and then I searched for documentation regarding math functions in the NodeMCU firmware, and found none except for questions like my own regarding powers, and people highlighting that they could use x ^ n rather than math,pwr(). – poorandunlucky Oct 01 '18 at 18:00
  • It ain't there: https://nodemcu.readthedocs.io/en/latest/en/lua-developer-faq/#how-is-nodemcu-lua-different-to-standard-lua – Marcel Stör Oct 25 '18 at 06:56

1 Answers1

2
local function log(x)
   assert(x > 0)
   local a, b, c, d, e, f = x < 1 and x or 1/x, 0, 0, 1, 1
   repeat
      repeat
         c, d, e, f = c + d, b * d / e, e + 1, c
      until c == f
      b, c, d, e, f = b + 1 - a * c, 0, 1, 1, b
   until b <= f
   return a == x and -f or f
end

local function log10(x)
   return log(x) / 2.3025850929940459
end
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64