Chapter 15 Caché Function Collection $FACTOR Function

Article Directory

Chapter 15 Caché Function Collection $FACTOR Function

Convert integer to $BITbit 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

$FACTORReturns the $BITformat bit string corresponding to the binary representation of the provided integer . It does the following:

  • If a negative number is specified, $FACTORthe absolute value of the number is taken.
  • If you specify the number of decimal places, the $FACTORinteger multiplied by 10 ** decimal places.
  • If you specify decimal, it $FACTORwill be rounded to integer. When rounding, Caché rounds the decimal .5 to the next largest integer.
  • $FACTORConvert an integer to its binary representation.
  • $FACTORConvert this binary number into an $BITencoded 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 $BITthe bit string used by various functions.

parameter

on one

Number (or expression in which the result of calculation is a number). $factorApply 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"), $factorconverts the digital part of the string (as in our example 7and 5.6) until it encounters a non-numeric characters. If num is zero, rounded to zero, empty string ( “”), or non-numeric string, an $factorempty string is returned. $DOUBLEValue INF, -INFand NaNreturns 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 $FACTORthe 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

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/108417182