floor ceil fabs abs modf in c++

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;
}


Output:

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


fabs:

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 the  int 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


modf:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值