beautypg.com

2 mathematical operations, 3 expressions with numeric data types – Campbell Scientific CR1000 Measurement and Control System User Manual

Page 143

background image

Section 7. Installation

143

 

Note Single-precision float has 24 bits of mantissa. Double precision has a 32-bit
extension of the mantissa, resulting in 56 bits of precision. Instructions that use
double precision are AddPrecise(), Average(), AvgRun(), AvgSpa(), CovSpa(),
MovePrecise(), RMSSpa(), StdDev(), StdDevSpa(), and Totalize().

Floating-point arithmetic is common in many electronic, computational systems,
but it has pitfalls high-level programmers should be aware of. Several sources
discuss floating-point arithmetic thoroughly. One readily available source is the
topic Floating Point at www.wikipedia.org. In summary, CR1000 programmers
should consider at least the following:

• Floating-point numbers do not perfectly mimic real numbers.
• Floating-point arithmetic does not perfectly mimic true arithmetic.
• Avoid use of equality in conditional statements. Use >= and <= instead. For

example, use If X >= Y then do rather than If X = Y then do.

• When programming extended-cyclical summation of non-integers, use the

AddPrecise() instruction. Otherwise, as the size of the sum increases,
fractional addends will have an ever decreasing effect on the magnitude of
the sum, because normal floating-point numbers are limited to about 7 digits
of resolution.

7.7.3.9.2 Mathematical Operations

Mathematical operations are written out much as they are algebraically. For
example, to convert Celsius temperature to Fahrenheit, the syntax is:

TempF = TempC * 1.8 + 32

Read More! To save code space while filling an array or partial array with the
same value, see CRBasic example Use of Move() to Conserve Code Space

(p. 150).

CRBasic example Use of Variable Arrays to Conserve Code Space

(p. 150)

shows

example code to convert twenty temperatures in a variable array from °C to °F.

7.7.3.9.3 Expressions with Numeric Data Types

FLOATs, LONGs and Booleans are cross-converted to other data types, such as
FP2, by using "=".

Boolean from FLOAT or LONG

When a FLOAT or LONG is converted to a Boolean as shown in CRBasic
example Conversion of FLOAT / LONG to Boolean (p. 143), zero becomes false
(0) and non-zero becomes true (-1).

CRBasic Example 20. Conversion of FLOAT / LONG to Boolean 

Public

Fa

As Float

Public

Fb

As Float

Public

L

As Long

Public

Ba

As Boolean

Public

Bb

As Boolean

Public

Bc

As Boolean