Difference between revisions of "Square Root"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>DragoonWraith
imported>DragoonWraith
(forgot the set inputNumber part)
Line 13: Line 13:
  ;how inputNumber is set depends on implementation.
  ;how inputNumber is set depends on implementation.
  ;Here it is the number of training sessions the player has had.
  ;Here it is the number of training sessions the player has had.
  set inputNumber to GetPCMiscStat 3
   set SqRtValue to inputNumber/2
   set SqRtValue to inputNumber/2
   set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2
   set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2

Revision as of 16:24, 6 May 2006

Almost unbelievably, Oblivion does not include a square root function. Therefore one must use this code to get the square root of a number.

This uses Newton-Raphson approximation, and gives accuracy to two decimal places for numbers less than or equal to 100. Adding more steps to it increases accuracy very quickly.

scriptname SquareRoot
;originally supplied by Galerion

float inputNumber
float SqRtValue

Begin {appropriate blocktype}

;how inputNumber is set depends on implementation.
;Here it is the number of training sessions the player has had.
  set inputNumber to GetPCMiscStat 3

  set SqRtValue to inputNumber/2
  set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2
  set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2
  set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2
  set SqRtValue to ( SqRtValue + ( inputNumber / SqRtValue ) ) / 2

End

The inputNumber variable is your input and the SqRtValue is the output variable. In other words, SqRtValue=(inputValue)^(1/2)