$FACTOR (ObjectScript)
Synopsis
$FACTOR(num,scale)
Arguments
Argument | Description |
---|---|
num | An expression that evaluates to a number. num is converted to a positive integer before bitstring conversion. A negative number is converted to a positive number (its absolute value). A fractional number is rounded to an integer. |
scale | Optional — An integer used as a power-of-ten exponent (scientific notation) multiplier for num. The default is 0. |
Description
$FACTOR returns the $BIT format bitstring that corresponds to the binary representation of the supplied integer. It performs the following operations:
-
If you specify a negative number, $FACTOR takes the absolute value of the number.
-
If you specify a scale $FACTOR multiplies the integer by 10**scale.
-
If you specify a fractional number $FACTOR rounds this number to an integer. When rounding numbers, InterSystems IRIS rounds the fraction .5 up to the next highest integer.
-
$FACTOR converts the integer to its binary representation.
-
$FACTOR converts this binary number to $BIT encoded binary format.
The binary string returned specifies bit positions starting from the least significant bit at position 1 (one's place at position 1). This corresponds to the bitstrings used by the various $BIT functions.
Arguments
scale
An integer that specifies the scientific notation exponent to apply to num. For example, if scale is 2, then scale represents 10 exponent 2, or 100. This scale value is multiplied by num. For example, $FACTOR(7,2) returns the bitstring that corresponds to the integer 700. This multiplication is done before rounding num to an integer. By default, scale is 0.
Examples
The following example show the conversion of the integers 1 through 9 to bitstrings:
SET x=1
WHILE x<10 {
WRITE !,x,"="
FOR i=1:1:8 {
WRITE $BIT($FACTOR(x),i) }
SET x=x+1 }
The following example show $FACTOR conversion of negative numbers and fractions to positive integers:
FOR i=1:1:8 {WRITE $BIT($FACTOR(17),i)}
WRITE " Positive integer",!
FOR i=1:1:8 {WRITE $BIT($FACTOR(-17),i)}
WRITE " Negative integer (absolute value)",!
FOR i=1:1:8 {WRITE $BIT($FACTOR(16.5),i)}
WRITE " Positive fraction (rounded up)",!
FOR i=1:1:8 {WRITE $BIT($FACTOR(-16.5),i)}
WRITE " Negative fraction (rounded up)"
The following example show the bitstring returned when the scale argument is specified:
SET x=2.7
WRITE !,x," scaled then rounded to an integer:",!!
FOR i=1:1:12 {
WRITE $BIT($FACTOR(x),i) }
WRITE " binary = ",$NORMALIZE(x,0)," decimal",!
SET scale=1
SET y=x*(10**scale)
FOR i=1:1:12 {
WRITE $BIT($FACTOR(x,scale),i) }
WRITE " binary = ",$NORMALIZE(y,0)," decimal",!
SET scale=2
SET y=x*(10**scale)
FOR i=1:1:12 {
WRITE $BIT($FACTOR(x,scale),i) }
WRITE " binary = ",$NORMALIZE(y,0)," decimal"