Article Directory
Chapter 15 Caché Function Collection $FACTOR Function
Convert integer to $BIT
bit string.
Outline
$FACTOR(num,scale)
parameter
- num The result of the calculation is a numeric expression. num is converted to a positive integer before bit string conversion. Negative numbers will be converted to positive numbers (its absolute value). Decimals are rounded to integers.
- scale Optional-integer, used as the multiplier of decimal power (scientific notation) of integer. The default value is 0.
description
$FACTOR
Returns the $BIT
format bit string corresponding to the binary representation of the provided integer . It does the following:
- If a negative number is specified,
$FACTOR
the absolute value of the number is taken. - If you specify the number of decimal places, the
$FACTOR
integer multiplied by 10 ** decimal places. - If you specify decimal, it
$FACTOR
will be rounded to integer. When rounding, Caché rounds the decimal .5 to the next largest integer. $FACTOR
Convert an integer to its binary representation.$FACTOR
Convert this binary number into an$BIT
encoded binary format.
The returned binary string specifies the bit position starting from the least significant bit of position 1 (the position of position 1). This corresponds to $BIT
the bit string used by various functions.
parameter
on one
Number (or expression in which the result of calculation is a number). $factor
Apply the Scale parameter (if provided), convert this number to an integer by rounding, and then return the corresponding bit string. Num can be positive or negative. If num is mixed numeric string (e.g., " 7wwarves
" or " 5.6.8
"), $factor
converts the digital part of the string (as in our example 7
and 5.6
) until it encounters a non-numeric characters. If num is zero, rounded to zero, empty string ( “”
), or non-numeric string, an $factor
empty string is returned. $DOUBLE
Value INF
, -INF
and NaN
returns an empty string.
scale
An integer that specifies the scientific notation exponent to be applied to Num. For example, if the ratio is 2, the ratio represents 10 exponents of 2 or 100. The scale value is multiplied by Num. For example, $factor(7,2)
return the bit string corresponding to the integer 700. This multiplication is done before rounding num to an integer. By default, the scale is 0.
Example
The following example shows the conversion from integer 1 to 9 to bit string:
/// d ##class(PHA.TEST.Function).FACTOR()
ClassMethod FACTOR()
{
SET x=1
WHILE x<10 {
WRITE !,x,"="
FOR i=1:1:8 {
WRITE $BIT($FACTOR(x),i)
}
SET x=x+1
}
}
DHC-APP>d ##class(PHA.TEST.Function).FACTOR()
1=10000000
2=01000000
3=11000000
4=00100000
5=10100000
6=01100000
7=11100000
8=00010000
9=10010000
The following example shows $FACTOR
the conversion of negative numbers and decimals to positive integers:
/// d ##class(PHA.TEST.Function).FACTOR1()
ClassMethod FACTOR1()
{
FOR i=1:1:8 {
WRITE $BIT($FACTOR(17),i)}
WRITE " 正整数",!
FOR i=1:1:8 {
WRITE $BIT($FACTOR(-17),i)}
WRITE " 负整数 (绝对值)",!
FOR i=1:1:8 {
WRITE $BIT($FACTOR(16.5),i)}
WRITE " 正小数(四舍五入)",!
FOR i=1:1:8 {
WRITE $BIT($FACTOR(-16.5),i)}
WRITE " 负小数(四舍五入)",!
FOR i=1:1:8 {
WRITE $BIT($FACTOR(-16.4),i)}
WRITE " 负小数(四舍五入)"
}
DHC-APP>d ##class(PHA.TEST.Function).FACTOR1()
10001000 正整数
10001000 负整数 (绝对值)
10001000 正小数(四舍五入)
10001000 负小数(四舍五入)
00001000 负小数(四舍五入)
The following example shows the bit string returned when the scale parameter is specified:
/// d ##class(PHA.TEST.Function).FACTOR2()
ClassMethod FACTOR2()
{
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"
}
DHC-APP>d ##class(PHA.TEST.Function).FACTOR2()
2.7 scaled then rounded to an integer:
110000000000 binary = 3 decimal
110110000000 binary = 27 decimal
011100001000 binary = 270 decimal