Difference between revisions of "Square Root"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>DragoonWraith
m
imported>DragoonWraith
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Almost unbelievably, Oblivion does '''not''' include a square root function. Therefore one must use this code to get the square root of a number.
Oblivion does '''not''' include a square root function. The [[:Category: Oblivion Script Extender|Oblivion Script Extender]] does - you can find out about it at [[SquareRoot]]. Without OBSE, you must use this code:


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.
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
NOTE: For a check such as the distance between two points it's not required to actually take the square root. The Forumal of Pythagoras goes A^2 + B^2 = C^2, so instead of "if sqrt(X*X + Y*Y) == C" one can use "If (X*X + Y*Y) == (C*C)", where C is the distance you want to have checked. (For the mundane stuff GetDistance should work, but you may not always want to work in units)
 
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 SqRt ;(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.
 
  ;originally supplied by Galerion
  ;originally supplied by Galerion
   
  set Wiki.SqRt to input/2
float inputNumber
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
float SqRtValue
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
Begin {appropriate blocktype}
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
;how inputNumber is set depends on implementation.
;Here it is the number of training sessions the player has had.
  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)


[[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

Oblivion does not include a square root function. The Oblivion Script Extender does - you can find out about it at SquareRoot. Without OBSE, you must use this code:

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.

NOTE: For a check such as the distance between two points it's not required to actually take the square root. The Forumal of Pythagoras goes A^2 + B^2 = C^2, so instead of "if sqrt(X*X + Y*Y) == C" one can use "If (X*X + Y*Y) == (C*C)", where C is the distance you want to have checked. (For the mundane stuff GetDistance should work, but you may not always want to work in units)

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 SqRt ;(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.

;originally supplied by Galerion
set Wiki.SqRt to input/2
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2
set Wiki.SqRt to ( Wiki.SqRt + ( Wiki.input / Wiki.SqRt ) ) / 2