关于求绝对值的问题

来源:互联网 发布:mac sass安装出错 编辑:程序博客网 时间:2024/05/18 20:10

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

当用abs求绝对值时,需要注意不同的数据类型调用不同的API函数,其中abs、labs、fabs分别对应整型、长整型、浮点型。假如对浮点数用了abs得到的将是强制转换后的整型。下面是MSDN上的例子:

Example

/* ABS.C: This program computes and displays * the absolute values of several numbers. */#include  <stdio.h>#include  <math.h>#include  <stdlib.h>void main( void ){   int    ix = -4, iy;   long   lx = -41567L, ly;   double dx = -3.141593, dy;   iy = abs( ix );   printf( "The absolute value of %d is %d/n", ix, iy);   ly = labs( lx );   printf( "The absolute value of %ld is %ld/n", lx, ly);   dy = fabs( dx );   printf( "The absolute value of %f is %f/n", dx, dy );}

Output

The absolute value of -4 is 4The absolute value of -41567 is 41567The absolute value of -3.141593 is 3.141593

 

更多技术文章请参看施昌权的个人网站: http://www.joyvc.cn