beautypg.com

Section 8. processing and math instructions, Abs (source) – Campbell Scientific CR5000 Measurement and Control Module User Manual

Page 173

background image

8-1

Section 8. Processing and Math
Instructions

Operators

^

Raise to Power

*

Multiply

/

Divide

+

Add

-

Subtract

=

Equals

<>

Not Equal

>

Greater Than

<

Less Than

>=

Greater Than or Equal

<=

Less Than or Equal

ABS (Source)

Returns the absolute value of a number.

Syntax
x = ABS (
source)

Remarks
Source can be any valid numeric expression. The absolute value of a number
is its unsigned magnitude. For example, ABS(-1) and ABS(1) both return 1.

ABS Function Example
The example finds the approximate value for a cube root. It uses Abs to
determine the absolute difference between two numbers.

Dim Precision, Value, X, X1, X2

'Declare variables.

Precision = .00000000000001
Value = Volt(3)

'Volt(3) will be evaluated.

X1 = 0: X2 = Value

'Make first two guesses.

'Loop until difference between guesses is less than precision.
Do Until Abs(X1 - X2) < Precision
X = (X1 + X2) / 2
If X * X * X - Value < 0 Then

'Adjust guesses.

X1 = X

Else

X2 = X

End If
Loop

'X is now the cube root of Volt(3).