lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Or your console may behave like a programmable calculator (that has a "RUN" or "f" button execute the monadic function just like when the user press the "sin" button to get the sinus, or the "x²" button to get the square):

x is the value of the display (that the user enters normally, it just has to be at least 3 at start), C is in fact the single "memory" store where you store the value from which you want to find the root.

The "program" is stored as a non-recursive function definition that will adjust the display y to converge to the next approximation value.

The user will just press the "f" or RUN button: he will get the next approximation. Pressing the RUN button 6 times will be enough to get the precise result (he can press the button more, nothing changes), and you then don't need any recursion code in your program, or any instruction to print the value.

With that basic calculator (which implements only the basic arithmetic or binary operations), you can compute many mathematics function not defined by the calculator itself (including trigonometry, hyperbolic, exponentials, resolve equations like x^x=C, using a convergent series that returns the correct answer with any precision needed and with a small number of steps needed for the series, as long as the starting value of the series is correctly entered to ensure that it will be in the convergent range; for your problem starting with the display showing x=3 is sufficient, and you need to press RUN six times).

The operations made by the user are simple: program the RUN button with the formula of the f series transforming f_n into f_(n+1): enter the Lua _expression_ only.
Then enter the value of C, press "M+" to store it (the Lua program will see its value as the global "C")
Enter a start value (e.g. "3") then press RUN 6 times, you have the result !
In a UI, you would have 3 input boxes: one for the value of "x" (the conventional "display" of the calculator), one for the value of the single memory (the value given to C) one for the program (the Lua _expression_ using only "x" and "C"), . And a single RUN button that the user will press multiple times (as he wants) until he gets the precision he wants or sees no more difference on the display. But we can warranty that it will get the most precise value for a 32-bit IEEE float by pressing it 5 times, and 6 times for a 64-bit IEEE float (more presses may be needed if your calculator can compute using Lua "number" with higher precision or infinite precision, but you'll need a larger display or a scrollbar in your calculator interface to show all the digits...


Le ven. 28 juin 2019 à 00:20, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :
Your program may also run in a console that will automatically print itself the last value computed when the code ends and may run it again in a loop if the program does not return a nil value, by calling the implied function again with that last value.
In that case, the program is a single _expression_ that does not have to be printed in the program itself; the program will terminate in just a few microseconds, and the user will actually wait for the screen to be refreshed (typically about 15 ms on average on a 60Hz monitor), before the user has even released the "enter" key or the click on the "run" button, and the user will still need about 30 ms to assimilate visually the value displayed (the console may only print the last non-nil value returned and not every value returned by the loop (which is equivalent to a trailing call).

But having to return a specific nil costs some bytes, it is simpler to just call the function recursively for a predefined number of steps, using  explicit function calls as "f(f(f(f(f(some _expression_)))))". The console will print that returned value itself, without having to insert the "out" or "=" statement which is implied by the console, just like the global name "f" of the function we can call inside the program will be implied (the console predefines that in the global environment with your "C" constant that must be defined somewhere (in the specific console program you use to run your input code, which can never run "alone").