[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

# Technical Reference: Base Operating System and Extensions, Volume 1

## floor, floorf, floorl, nearest, trunc, itrunc, or uitrunc Subroutine

### Purpose

The floor subroutine, floorf subroutine, floorl subroutine, nearest subroutine, and trunc subroutine, round floating-point numbers to floating-point integer values.

The itrunc subroutine and uitrunc subroutine round floating-point numbers to signed and unsigned integers, respectively.

### Libraries

IEEE Math Library (libm.a)
or System V Math Library (libmsaa.a)
Standard C Library (libc.a) (separate syntax follows)

### Syntax

`#include <math.h>`

double floor ( x)
double x;

```float floorf (x)
float x;
```

```long double floorl (x)
long double x;```

```double nearest (x)
double x;```

```double trunc (x)
double x;```

Standard C Library (libc.a)

```#include <stdlib.h>
#include <limits.h>```

```int itrunc (x)
double x;```

```unsigned int uitrunc (x)
double x;```

### Description

The floor subroutine and floorl subroutines return the largest floating-point integer value not greater than the x parameter.

An application wishing to check for error situations should set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these subroutines. Upon return, if errno is nonzero or fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is nonzero, an error has occurred.

The nearest subroutine returns the nearest floating-point integer value to the x parameter. If x lies exactly halfway between the two nearest floating-point integer values, an even floating-point integer is returned.

The trunc subroutine returns the nearest floating-point integer value to the x parameter in the direction of 0. This is equivalent to truncating off the fraction bits of the x parameter.

Note
The default floating-point rounding mode is round to nearest. All C main programs begin with the rounding mode set to round to nearest.

The itrunc subroutine returns the nearest signed integer to the x parameter in the direction of 0. This is equivalent to truncating the fraction bits from the x parameter and then converting x to a signed integer.

The uitrunc subroutine returns the nearest unsigned integer to the x parameter in the direction of 0. This action is equivalent to truncating off the fraction bits of the x parameter and then converting x to an unsigned integer.

Note
Compile any routine that uses subroutines from the libm.a library with the -lm flag. To compile the floor.c file, for example, enter:
`cc floor.c -lm`

The itrunc, uitrunc, trunc, and nearest subroutines are not part of the ANSI C Library.

### Parameters

 x Specifies a double-precision floating-point value. For the floorl subroutine, specifies a long double-precision floating-point value.

 y Specifies a double-precision floating-point value. For the floorl subroutine, specifies some long double-precision floating-point value.

### Return Values

Upon successful completion, the floor, floorf, and floorl subroutine returns the largest integral value not greater than x, expressed as a double, float, or long double, as appropriate for the return type of the function.

If x is NaN, a NaN is returned.

If x is ±0 or ±Inf, x is returned.

If the correct value would cause overflow, a range error occurs and the floor, floorf and floorl subroutines return the value of the macro -HUGE_VAL, -HUGE_VALF and -HUGE_VALL, respectively.

### Error Codes

The itrunc and uitrunc subroutines return the INT_MAX value if x is greater than or equal to the INT_MAX value and the INT_MIN value if x is equal to or less than the INT_MIN value. The itrunc subroutine returns the INT_MIN value if x is a Quiet NaN(not-a-number) or Silent NaN. The uitrunc subroutine returns 0 if x is a Quiet NaN or Silent NaN. (The INT_MAX and INT_MIN values are defined in the limits.h file.) The uitrunc subroutine INT_MAX if x is greater than INT_MAX and 0 if x is less than or equal 0.0

### Files

 float.h Contains the ANSI C FLT_ROUNDS macro.