Positron Compiler Documentation

Sqr

Positron8 · Revision 4.0.6.5 · PDF page 112

Assignment Variable = Sqr(pVariable)

Deduce the Square Root of a floating point value

pVariable can be a constant, variable or expression that requires the Square Root extracted. Assignment Variable can be any valid variable type.

With 12 and 14-bit core devices, Sqr returns an integer square root of a value, compatible with the BASIC Stamp syntax. Remember that most square roots have a fractional part that the compiler discards in doing its integer-only maths. Therefore it computes the square root of 100 as 10 (correct), but the square root of 99 as 9 (the actual is close to 9.95).

Example: -

Var1 = Sqr(Var2)

or

HRsoutLn Sqr 100       ' Display square root of 100 (10).
HRsoutLn Sqr 99        ' Display of square root of 99 (9 due to truncation)

However, with the extra functionality, and more linear memory offered by the 18F devices, full 32-bit floating point Sqr is implemented.

Device = 18F26K40             ' Select the device to compile for
Declare Xtal = 16  ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600   ' Set the Baud rate for HRsoutLn

Dim Floatin as Float        ' Holds the value to Sqr
Dim Floatout as Float        ' Holds the result of the Sqr

Floatin = 600               ' Load the variable
Floatout = Sqr(Floatin)      ' Extract the Sqr of the value
HRsoutLn Dec Floatout        ' Display the result
Stop

Floating point trigonometry is extremely memory hungry, so do not be surprised if a large chunk of the microcontroller’s code memory is used with a single operator. This also means that floating point trigonometry is comparatively slow to operate.

When implementing trigonometry, or other built in, functions within an expression, always wrap them in parenthesis, otherwise the parser may consider the extra operands as part of the trigonometry parameter and produce an incorrect result. For example:

MyAssignment = (Sqr(MyVar1)) + MyVar2
View original PDF page 112Open page in PDFOriginal PDF page 112