Natural Logarithm

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search

This is a script for finding the natural logarithm (ln) of a number.

This is a Stage Function, and as such must be created as a Quest Stage of a global script and called as a function. This assumes a Quest called Wiki with the variables declared. Therefore, you must modify this code to include the name of your Quest (assuming it is not Wiki, which it shouldn't be). Alternatively, however, it would not be difficult to incorporate this code into your script directly.

The list of variables you will need declared in your Global Script:

float input ;(the number whose root you need)
float x1 ;(used internally)
float x3 ;(used internally)
float x5 ;(used internally)
float x7 ;(used internally)
float x9 ;(used internally)
;more x# variables will increase accuracy
float LN ;(the natural log, your answer)

Your input variable need not necessarily be a float, it could be a short or long, but the rest must be floats. You are free to rename either, but remember to change the function code.

; supplied by dysesothymteric
set wiki.x1 to ( ( wiki.input - 1 ) / ( wiki.input + 1 ) )
set wiki.x3 to ( wiki.x1 * wiki.x1 * wiki.x1 )
set wiki.x5 to ( wiki.x3 * wiki.x1 * wiki.x1 )
set wiki.x7 to ( wiki.x5 * wiki.x1 * wiki.x1 )
set wiki.x9 to ( wiki.x7 * wiki.x1 * wiki.x1 )
;again, follow this pattern for increased accuracy

set wiki.LN to ( wiki.x1 + ( wiki.x3 / 3 ) + ( wiki.x5 / 5 ) + ( wiki.x7 / 7 ) + ( wiki.x9 / 9 ) )
;yes, more accuracy means adding more terms here

As stated repeatedly in this code's comments, greater accuracy may be achieved by adding more x# variables.