Missing math library functions

I was trying to generate a certain curve and found math.tanh missing, I decided to check all of the math functions listed in the documentation and found several more are missing.

function setup()
    local val,val2 = 0.75, 1 --example values
    local missingFunctions = {
math.sinh, math.cosh, math.tanh, math.frexp, math.ldexp, math.log10, 
math.sin} --note: math.sin is not missing but here as a counterexample
    for k,func in pairs(missingFunctions) do print( func(val,val2) ) end
1 Like

Interesting, in the Lua source code these are listed as deprecated functions and are #defined out of the codebase. I think we need to make a decision on whether we remove them from documentation, or add them back in. Perhaps removing them from docs is the way to go?

@sim for me it would be better to add them rather than remove them, especially if most of the work is already done!

I haven’t made much use of them so either way would work for me.
perhaps those hyperbolic functions could come in handy some day tho…

@piinthesky I would lean towards removing them because I imagine one day soon they will be removed from Lua (given they are deprecated and already removed by default), and then the decision will be made for us.

pow has been replaced by the ^ operator in Lua

I think the only ones that don’t have simple replacements are math.frexp and math.ldexp

yepp, i didn’t trigger that Lua had depreciated them, i thought it was Codea that had done so.

I would say that removing support for deprecated functions entirely will have some downsides when it comes to running older projects though. Ideally, it shouldn’t matter if you’re running the latest version of Codea, a project should run still. Easier said than done but it should always be a consideration.

@sim Could we perhaps consider some backward compatibility functions for things like this that can easily be inserted into older projects. That way we can redefine the missing functions ourselves.

Something like this perhaps:

-- Code using old math functions here

Possibly, or we could just adopt support for the most useful math functions and maintain our implementations going forward?

Are there any in the list of deprecated functions that seem particularly useful? Are there any math functions in general missing from Lua that we would like to have (e.g., math.round?)

1 Like

math.round would be hugely beneficial. At the moment the only way to round is using string formatting then converting back to a number. Unless I missed something. Coming from Python this blew my mind.

Is there an easy way to truncate? Because that seemed a bit tedious as well so maybe some built in way to truncate a number without having to do string formatting then convert. Might help some individuals.

you could round a number to 2 decimals like so

roundedNumber = math.floor(numberToRound*100)/100
1 Like

Rounding should be to the nearest whatever, so:

math.floor(number * 10^precision + 0.5)/10^precision

Then we have:

  • cosh is (e^x + e^(-x))/2
  • sinh is (e^x - e^(-x))/2
  • tanh is (e^(2*x)-1)/(e^(2*x)+1)
  • log10 is math.ln(x)/math.ln(10)
  • ldexp is m*2^e
  • fdexp is something like math.floor(math.ln(x)/math.ln(2)) for the exponent, and 2^(math.ln(x)/math.ln(2) - math.floor(math.ln(x)/math.ln(2))) (or math.fract, if that still exists), but I may have a fence-post error here.

I love that you grace us with maths all the time @LoopSpace. Perhaps we should re-implement these in the terms you provide

1 Like

I guess the question is, are these used sufficiently often that they are worth putting in the C code, or can they be provided via a pure Lua extension library?

I’m going to add these to my VecExt library, since I do use some of them there. But I’ll wrap the definitions in tests to see if they are needed. (Then I can safely forget about them! If you do remove them, I’m okay, and also if you don’t. )

1 Like

i haven’t used these in my framework either, so i don’t see a need to keep them around

speaking of round, i had to implement my own because of negative numbers

for example -1.7 will round to -1 using math.floor so you have to switch to using math.ceil

These are now added to my VecExt library (not math.round since that didn’t originally exist).

@Steppers backward-compatibility proposal is an interesting concept because there are two major things that make old projects crash in my experience:

  • the removal of atan2
  • foo.unpack() becoming table.unpack(foo)

…if Codea had some kind of legacy support for those syntaxes it would make lots of old things run without crashing.

@Sim is it anathema to consider giving Codea some support for obsolete lua syntax?

I don’t mind putting them back in the Legacy runtime. Probably as undocumented, and not part of autocomplete? That way it doesn’t seem supported