cc: rbtneville at gmail.com Subject: Re: Re: [ksh93-integration-discuss] Minimum and maximum values for integer variables? --------
> I didn't knew that ksh has floating point variables. Where can I find > more information about floating point support? How do the casting > rules work, are they like in C, perl or python? Is there a tutorial on > ksh math somewhere? > -- > robert neville - it consultant Here is a quick summary of arithmetic in ksh93. Shell arithmetic is performed on arithmetic expressions inside ((...)) , $((...)) and for subscript calculations for indexed arrays. Note that ((...)) is a command that returns True if the value of the expression is non-zero and False if the value is zero, whereas, $((...)) is an expansion that can appear wherever a parameter expansion is legal and evaluates to a string which is the value of the expression inside, Shell arithmetic uses the same operators and precedents as ANSI-C. However, it does not support the * and & prefix operators, the -> operator, and the casting operators. It additionally supports the operator ** for exponentiation. In addition to the ANSI-C operators, the functions defined by ANSI-C are also available. On systems that supply C99 libraries, all the C99 floating point functions are available inside an arithmetic expression. By default variables store strings of arbitrary lengths and the strings are converted into either integer or into floating point quantities when performing arithmetic operations. By default, when converting string constants, the shell will choose the longest integer and the largest floating point variable available in the underlying C implementation. If the string does not contain a decimal point and it fits into an integer it will be converted to an integer, typically a long long integer. Otherwise, it will be converted to the largest floating point available, typically a long double. However, attributes for variables can be specified which cause the variable to be stored in other ways. The -E or -F attributes causes the variable to be stored as a double floating point quantity. The only difference between -E and -F is that when the variable is expanded using parameter expansion ($var), the -E uses scientific notation where as -F uses floating point notation. The -E attribute can be followed by a number representing the number of significant figures to use and the -F can be followed by a number giving the number of places after the decimal point. These attributes can be modified with -l to specify a long double, and -s to specify a float. The -i cause the variable to be stored as a long integer. It can be followed by a number representing a base from 2 to 64. The default is decimal, base 10. When a base other than 10 is uses, parameter expansion of the variable will have the base followed by a # prepended to the number in that base. The attribute -l can be used to specify a long long integer and -s can be used to specify a short integer. The -u attribute can be used to specify that the number should be treated as unsigned. By default any integer whose base is not 10 is treated as unsigned. Inside an arithmetic expression, a variable can be referenced by name, or can be used inside a parameter expansion. These are not always equivalent so that ((var)) and (($var)) need not be the same. For example, the -RZ attribute for a variable will cause leading zeros to be prepended as needed to fill the specified width. However, these zeros will not signify an octal constant when var is used, whereas $var is expanded before the arithmetic expression is evaluated so that the arithmetic expander will see this as an octal constant. When evaluating an expression, all floating point constants and variables are converted into long double and integer constants and variables into long long integer. Whenever an integer and a float are used in a binary operation, the integer is converted into floating point. Integer operations are converted into floating point when the results would overflow a long long integer. Overflows in floating point can result in the special constant Inf. Operations that do not yield valid results, for example, the square root of a negative number, yield the special numerical constant NaN. Integer division by zero will cause a divide error. Thus, $((4/0)) will cause a divide by zero error, whereas $((4.0/0)) will expand to Inf. David Korn dgk at research.att.com