floor:
double floor (double x);
Round down value
Rounds
x downward, returning the largest integral value that is not greater than
x.
Header
<tgmath.h> provides a type-generic macro version of this function.
Parameters
-
x
- Value to round down.
Return Value
The value of x rounded downward (as a floating-point value)./* floor example */
#include <stdio.h> /* printf */
#include <math.h> /* floor */
int main ()
{
printf ( "floor of 2.3 is %.1lf\n", floor (2.3) );
printf ( "floor of 3.8 is %.1lf\n", floor (3.8) );
printf ( "floor of -2.3 is %.1lf\n", floor (-2.3) );
printf ( "floor of -3.8 is %.1lf\n", floor (-3.8) );
return 0;
}
Output:
floor of 2.3 is 2.0 floor of 3.8 is 3.0 floor of -2.3 is -3.0 floor of -3.8 is -4.0 |
ceil:
double ceil (double x);
Round up value
Rounds
x upward, returning the smallest integral value that is not less than
x.
Header
<tgmath.h> provides a type-generic macro version of this function.
Parameters
-
x
- Value to round up.
Return Value
The smallest integral value that is not less than x (as a floating-point value)./* ceil example */
#include <stdio.h> /* printf */
#include <math.h> /* ceil */
int main ()
{
printf ( "ceil of 2.3 is %.1f\n", ceil(2.3) );
printf ( "ceil of 3.8 is %.1f\n", ceil(3.8) );
printf ( "ceil of -2.3 is %.1f\n", ceil(-2.3) );
printf ( "ceil of -3.8 is %.1f\n", ceil(-3.8) );
return 0;
}
ceil of 2.3 is 3.0 ceil of 3.8 is 4.0 ceil of -2.3 is -2.0 ceil of -3.8 is -3.0 |
double fabs (double x);
Compute absolute value
Returns the
absolute value of
x: |
x|.
Header
<tgmath.h> provides a type-generic macro version of this function.
Parameters
-
x
- Value whose absolute value is returned.
Return Value
The absolute value of x./* fabs example */
#include <stdio.h> /* printf */
#include <math.h> /* fabs */
int main ()
{
printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) );
printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) );
return 0;
}
Output:
The absolute value of 3.1416 is 3.141600 The absolute value of -10.6 is 10.600000 |
abs:
int abs (int n);
Absolute value
Returns the absolute value of parameter
n (
/n/
).
In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs).
Parameters
-
n
- Integral value.
Return Value
The absolute value of n.Portability
In C, only theint
version exists.
For the
long int
equivalent see
labs.
For the
long long int
equivalent see
llabs.
/* abs example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* abs */
int main ()
{
int n,m;
n=abs(23);
m=abs(-11);
printf ("n=%d\n",n);
printf ("m=%d\n",m);
return 0;
}
Output:
n=23 m=11 |
double modf (double x, double* intpart);
Break into fractional and integral parts
Breaks
x into an integral and a fractional part.
The integer part is stored in the object pointed by intpart, and the fractional part is returned by the function.
Both parts have the same sign as x.
Additional overloads are provided in this header (
<cmath>
) for the
integral types: These overloads effectively cast
xto a
double
before calculations (defined for
T being any
integral type).
Parameters
-
x
- Floating point value to break into parts. intpart
- Pointer to an object (of the same type as x) where the integral part is stored with the same sign as x.
Return Value
The fractional part of x, with the same sign./* modf example */
#include <stdio.h> /* printf */
#include <math.h> /* modf */
int main ()
{
double param, fractpart, intpart;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%f = %f + %f \n", param, intpart, fractpart);
return 0;
}
Output:
3.141593 = 3.000000 + 0.141593 |