HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index


Chapter 7
Math Functions

Table 7-1 lists and describes the math functions in the HP C Run-Time Library (RTL). For more detailed information on each function, see the Reference Section.

Table 7-1 Math Functions
Function Description
abs Returns the absolute value of an integer.
acos Returns the arc cosine of its radian argument, in the range [0,pi] radians.
acosd (ALPHA, I64) Returns the arc cosine of its radian argument, in the range [0,180] degrees.
acosh (ALPHA, I64) Returns the hyperbolic arc cosine of its argument.
asin Returns the arc sine of its radian argument in the range [ - pi/2,pi/2] radians.
asind (ALPHA, I64) Returns the arc sine of its radian argument, in the range [ -90,90 ] degrees.
asinh (ALPHA, I64) Returns the hyperbolic arc sine of its argument.
atan Returns the arc tangent of its radian argument, in the range [ - pi/2,pi/2] radians.
atand (ALPHA, I64) Returns the arc tangent of its radian argument, in the range [ -90,90 ] degrees.
atan2 Returns the arc tangent of y/ x (its two radian arguments), in the range [ - pi,pi] radians.
atand2 (ALPHA, I64) Returns the arc tangent of y/ x (its two radian arguments), in the range [ -180,180 ] degrees.
atanh (ALPHA, I64) Returns the hyperbolic arc tangent of its radian argument.
cabs Returns the absolute value of a complex number as: sqrt ( x 2 + y 2) .
cbrt (ALPHA, I64) Returns the rounded cube root of its argument.
ceil Returns the smallest integer greater than or equal to its argument.
copysign (ALPHA, I64) Returns its first argument with the same sign as its second.
cos Returns the cosine of its radian argument in radians.
cosd (ALPHA, I64) Returns the cosine of its radian argument in degrees.
cosh Returns the hyperbolic cosine of its argument.
cot Returns the cotangent of its radian argument in radians.
cotd (ALPHA, I64) Returns the cotangent of its radian argument in degrees.
drand48 , erand48 , jrand48 , lrand48 , mrand48 , nrand48 Generates uniformly distributed pseudorandom number sequences. Returns 48-bit, nonnegative, double-precision floating-point values.
erf (ALPHA, I64) Returns the error function of its argument.
erfc (ALPHA, I64) Returns (1.0 - erf (x )).
exp Returns the base e raised to the power of the argument.
expm1 (ALPHA, I64) Returns exp (x ) - 1.
fabs Returns the absolute value of a floating-point value.
finite (ALPHA, I64) Returns 1 if its argument is a finite number; 0 if not.
floor Returns the largest integer less than or equal to its argument.
fmod Computes the floating-point remainder of its first argument divided by its second.
fp_class (ALPHA, I64) Determines the class of IEEE floating-point values, returning a constant from the <fp_class.h> header file.
isnan (ALPHA, I64) Test for NaN. Returns 1 if its argument is a NaN; 0 if not.
j0, j1, jn (ALPHA, I64) Computes Bessel functions of the first kind.
frexp Calculates the fractional and exponent parts of a floating-point value.
hypot Returns the square root of the sum of the squares of two arguments.
initstate Initializes random number generators.
labs Returns the absolute value of an integer as a long int .
lcong48 Initializes a 48-bit uniformly distributed pseudorandom number sequence.
lgamma (ALPHA, I64) Computes the logarithm of the gamma function.
llabs, qabs (ALPHA, I64) Returns the absolute value of an __int64 integer.
ldexp Returns its first argument multiplied by 2 raised to the power of its second argument.
ldiv, div Returns the quotient and remainder after the division of their arguments.
lldiv, qdiv (ALPHA, I64) Returns the quotient and remainder after the division of their arguments.
log2 (ALPHA, I64) , log, log10 Returns the logarithm of their arguments.
log1p (ALPHA, I64) Computes ln(1+ x) accurately.
logb (ALPHA, I64) Returns the radix-independent exponent of its argument.
nextafter (ALPHA, I64) Returns the next machine-representable number following x in the direction of y.
nint (ALPHA, I64) Returns the nearest integral value to the argument.
modf Returns the positive fractional part of its first argument and assigns the integral part to the object whose address is specified by the second argument.
pow Returns the first argument raised to the power of the second.
rand, srand Returns pseudorandom numbers in the range 0 to 2 31-1 .
random , srandom Generates pseudorandom numbers in a more random sequence.
rint (ALPHA, I64) Rounds its argument to an integral value according to the current IEEE rounding direction specified by the user.
scalb (ALPHA, I64) Returns the exponent of a floating-point number.
seed48 , srand48 Initializes a 48-bit random number generator.
setstate Restarts, and changes random number generators.
sin Returns the sine of its radian argument in radians.
sind (ALPHA, I64) Returns the sine of its radian argument in degrees.
sinh Returns the hyperbolic sine of its argument.
sqrt Returns the square root of its argument.
tan Returns the tangent of its radian argument in radians.
tand (ALPHA, I64) Returns the tangent of its radian argument in degrees.
tanh Returns the hyperbolic tangent of its argument.
trunc (ALPHA, I64) Truncates its argument to an integral value.
unordered (ALPHA, I64) Returns 1 if either or both of its arguments is a NaN; 0, if not.
y0, y1, yn (ALPHA, I64) Computes Bessel functions of the second kind.

7.1 Math Function Variants---float, long double (ALPHA, I64)

Additional math routine variants are supported for HP C on OpenVMS Alpha and I64 systems only. They are defined in <math.h> and are float and long double variants of the routines listed in Table 7-1.

Float variants take float arguments and return float values. Their names have an f suffix. For example:


float cosf (float x); 
float tandf (float x); 

Long double variants take long double arguments and return long double values. Their names have an l suffix. For example:


long double cosl (long double x); 
long double tandl (long double x); 

All math routine variants are included in the Reference Section of this manual.

Note that for programs compiled without /L_DOUBLE=64 (that is, compiled with the default /L_DOUBLE=128), the long double variants of these HP C RTL math routines map to the X_FLOAT entry points documented in the HP Portable Mathematics Library (HPML) manual.

7.2 Error Detection

To help you detect run-time errors, the <errno.h> header file defines the following two symbolic values that are returned by many (but not all) of the mathematical functions:

When using the math functions, you can check the external variable errno for either or both of these values and take the appropriate action if an error occurs.

The following program example checks the variable errno for the value EDOM, which indicates that a negative number was specified as input to the function sqrt :


#include <errno.h> 
#include <math.h> 
#include <stdio.h> 
 
main() 
{ 
   double input, square_root; 
 
   printf("Enter a number: "); 
   scanf("%le", &input); 
   errno = 0; 
   square_root = sqrt(input); 
 
   if (errno == EDOM) 
      perror("Input was negative"); 
   else 
      printf("Square root of %e = %e\n", 
              input, square_root); 
} 

If you did not check errno for this symbolic value, the sqrt function returns 0 when a negative number is entered. For more information about the <errno.h> header file, see Chapter 4.

7.3 The <fp.h> Header File

The <fp.h> header file implements some of the features defined by the Numerical C Extensions Group of the ANSI X3J11 committee. You might find this useful for applications that make extensive use of floating-point functions.

Some of the double-precision functions listed in this chapter return the value ±HUGE_VAL (defined in either <math.h> or <fp.h> ) if the result is out of range. The float version of those functions return the value HUGE_VALF (defined only in <fp.h> ) for the same conditions. The long double version returns the value HUGE_VALL (also defined in <fp.h> ).

For programs compiled to enable IEEE infinity and NaN values, the values HUGE_VAL, HUGE_VALF, and HUGE_VALL are expressions, not compile-time constants. Initializations such as the following cause a compile-time error:


$ CREATE IEEE_INFINITY.C 
#include <fp.h> 
 
double my_huge_val = HUGE_VAL 
^Z 
$ CC /FLOAT=IEEE/IEEE=DENORM IEEE_INFINITY 
 
double my_huge_val = HUGE_VAL; 
.....................^ 
%CC-E-NEEDCONSTEXPR, In the initializer for my_huge_val, "decc$gt_dbl_infinity" 
is not constant, but occurs in a context that requires a constant expression. 
at line number 3 in file WORK1$:[RTL]IEEE_INFINITY.C;1 
$ 

When using both <math.h> and <fp.h> , be aware that <math.h> defines a function isnan and <fp.h> defines a macro by the same name. Whichever header is included first in the application will resolve a reference to isnan .

7.4 Example

Example 7-1 shows how the tan , sin , and cos functions operate.

Example 7-1 Calculating and Verifying a Tangent Value

/*        CHAP_7_MATH_EXAMPLE.C                                 */ 
 
/* This example uses two functions --- mytan and main ---       */ 
/* to calculate the tangent value of a number, and to check     */ 
/* the calculation using the sin and cos functions.             */ 
 
#include <math.h> 
#include <stdio.h> 
 
/* This function calculates the tangent using the sin and       */ 
/* cos functions.                                               */ 
 
double mytan(x) 
    double x; 
{ 
    double y, 
           y1, 
           y2; 
 
    y1 = sin(x); 
    y2 = cos(x); 
 
    if (y2 == 0) 
        y = 0; 
    else 
        y = y1 / y2; 
 
    return y; 
} 
main() 
{ 
    double x; 
 
    /*  Print values: compare   */ 
    for (x = 0.0; x < 1.5; x += 0.1) 
        printf("tan of %4.1f = %6.2f\t%6.2f\n", x, mytan(x), tan(x)); 
} 

Example 7-1 produces the following output:


$ RUN  EXAMPLE
tan of  0.0 =   0.00      0.00
tan of  0.1 =   0.10      0.10
tan of  0.2 =   0.20      0.20
tan of  0.3 =   0.31      0.31
tan of  0.4 =   0.42      0.42
tan of  0.5 =   0.55      0.55
tan of  0.6 =   0.68      0.68
tan of  0.7 =   0.84      0.84
tan of  0.8 =   1.03      1.03
tan of  0.9 =   1.26      1.26
tan of  1.0 =   1.56      1.56
tan of  1.1 =   1.96      1.96
tan of  1.2 =   2.57      2.57
tan of  1.3 =   3.60      3.60
tan of  1.4 =   5.80      5.80
$ 


Previous Next Contents Index