Difference between revisions of "Natural Logarithm"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>DragoonWraith
 
imported>DragoonWraith
(converted to stage function)
Line 1: Line 1:
This is a script for finding the natural logarithm (ln) of a number.
This is a script for finding the natural logarithm (ln) of a number.


  scn NaturalLogarithm
This is a [[:Category:Stage_Functions|Stage Function]], and as such must be created as a [[Quest Stages Tab|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 root, your answer)
Your input variable need not necessarily be a float, it could be a short or long, but SqRt needs to be a float, as few square roots are integers. You are free to rename either, but remember to change the function code.
 
  ; supplied by dysesothymteric
  ; supplied by dysesothymteric
   
  set x1 to ( ( input - 1 ) / ( input + 1 ) )
float inputNumber
set x3 to ( x1 * x1 * x1 )
set x5 to ( x3 * x1 * x1 )
float x1
set x7 to ( x5 * x1 * x1 )
float x3
set x9 to ( x7 * x1 * x1 )
float x5
float x7
float x9
; more can be used for greater accuracy
float LNvalue
Begin {appropriate blocktype}
;how inputNumber is set depends on implementation
;here it is set to the number of diseases the player has contracted
  set inputNumber to GetPCMiscStat 26
  set x1 to ( ( inputNumber - 1 ) / ( inputNumber + 1 ) )
  set x3 to ( x1 * x1 * x1 )
  set x5 to ( x3 * x1 * x1 )
  set x7 to ( x5 * x1 * x1 )
  set x9 to ( x7 * x1 * x1 )
  ;again, follow this pattern for increased accuracy
  ;again, follow this pattern for increased accuracy
   
   
  set LNvalue to ( x1 + ( x3 / 3 ) + ( x5 / 5 ) + ( x7 / 7 ) + ( x9 / 9 ) )
set LN to ( x1 + ( x3 / 3 ) + ( x5 / 5 ) + ( x7 / 7 ) + ( x9 / 9 ) )
  ;yes, more accuracy means adding more terms here
  ;yes, more accuracy means adding more terms here
End


The inputNumber variable is your input, and LNvalue is your output. In other words, LNvalue=LN(inputNumber). As stated repeatedly in this code's comments, greater accuracy may be achieved by adding more x# variables.
As stated repeatedly in this code's comments, greater accuracy may be achieved by adding more x# variables.


[[category: Extra_Math_Functions]]
[[Category: Stage Functions]]
[[category: Useful_Code]]
[[Category: Extra Math Functions]]

Revision as of 20:00, 9 May 2006

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 root, your answer)

Your input variable need not necessarily be a float, it could be a short or long, but SqRt needs to be a float, as few square roots are integers. You are free to rename either, but remember to change the function code.

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

set LN to ( x1 + ( x3 / 3 ) + ( x5 / 5 ) + ( x7 / 7 ) + ( 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.