Difference between revisions of "Natural Logarithm"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>DragoonWraith
 
imported>DragoonWraith
 
(3 intermediate revisions by the same user not shown)
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 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
  ; supplied by dysesothymteric
   
  set wiki.x1 to ( ( wiki.input - 1 ) / ( wiki.input + 1 ) )
float inputNumber
set wiki.x3 to ( wiki.x1 * wiki.x1 * wiki.x1 )
set wiki.x5 to ( wiki.x3 * wiki.x1 * wiki.x1 )
float x1
set wiki.x7 to ( wiki.x5 * wiki.x1 * wiki.x1 )
float x3
set wiki.x9 to ( wiki.x7 * wiki.x1 * wiki.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 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
  ;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: Math Functions (CS 1.0)]]

Latest revision as of 20:18, 25 April 2010

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.