我在C中使用绝对值时遇到问题

时间:2014-03-28 16:06:39

标签: c

我在理解某事时遇到了问题......

说我有一个载体

double values[size];

我想计算其中非零值的数量......

int counter;
if(abs(counter) > 0.000001)
   ++counter;

但是,这不会返回与

相同的计数器值
if(counter > 0.0000001 || counter < -0.000001)

1 个答案:

答案 0 :(得分:4)

你使用了错误的功能。 abs()返回一个整数值。你真的想使用fabs()函数。在您的代码中,由于整数截断,数字的小数部分完全丢失。

其次,您可以将该值与0.0进行比较。在ANSI C中,根据C99,您可以同时使用正0.0和负0.0,但等价运算符检查(即:==)仍会报告它们是等于/等效的。

最后,这里使用绝对值函数有点矫枉过正。你真正需要使用它的唯一原因是你想要计算你的数组中的值是否“足够接近”为零。

琐碎案例 - 是零吗?

示例代码


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double fRand(double fMin, double fMax) {
   double f = (double)rand() / RAND_MAX;
   return fMin + f * (fMax - fMin);
}

int main(void) {
   int i;
   int nZeroes = 0;
   int nElements = 20;
   double* lfArray;
   if ((lfArray = calloc(nElements, sizeof(double))) == NULL) {
      /* Memory error, abort */
      return (-1);
   }

   /* Populate array */
   for (i=0; i<nElements; i++) {
      lfArray[i] = fRand(-10.0, +10.0);
   }
   /* Set a few arbitrary elements to 0.0 */
   lfArray[5] = 0.0;
   lfArray[10] = 0.0;
   lfArray[15] = 0.0;

   /* Determine how many zeroes are present, and log the results */
   for (i=0; i<nElements; i++) {
      if (fabs(lfArray[i]) == 0.0) {
         nZeroes++;
      }
   }
   printf("Number of zeroes detected:%d\n", nZeroes);
   for (i=0; i<nElements; i++) {
      printf("Element:%02d, Value:%3.4lf\n", i, lfArray[i]);
   }

   return 0;
}

示例输出


Number of zeroes detected:3
Element:00, Value:-9.9998
Element:01, Value:-7.3692
Element:02, Value:5.1121
Element:03, Value:-0.8270
Element:04, Value:0.6553
Element:05, Value:0.0000
Element:06, Value:-9.0591
Element:07, Value:3.5773
Element:08, Value:3.5859
Element:09, Value:8.6939
Element:10, Value:0.0000
Element:11, Value:0.3883
Element:12, Value:6.6193
Element:13, Value:-9.3086
Element:14, Value:-8.9308
Element:15, Value:0.0000
Element:16, Value:3.4230
Element:17, Value:-9.8460
Element:18, Value:-2.3317
Element:19, Value:-8.6632

非平凡案例 - 我们“足够接近”为零吗?

示例代码 - 案例2


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double fRand(double fMin, double fMax) {
   double f = (double)rand() / RAND_MAX;
   return fMin + f * (fMax - fMin);
}

int main(void) {
   int i;
   float lowerLimit = -0.001;
   float upperLimit = +0.001;
   int nZeroes = 0;
   int nElements = 20;
   double* lfArray;
   if ((lfArray = calloc(nElements, sizeof(double))) == NULL) {
      /* Memory error, abort */
      return (-1);
   }

   /* Populate array */
   for (i=0; i<nElements; i++) {
      lfArray[i] = fRand(2.0*lowerLimit, 2.0*upperLimit);
   }

   /* Determine how many zeroes are present, and log the results */
   for (i=0; i<nElements; i++) {
      if (fabs(lfArray[i]) < upperLimit) {
         lfArray[i] = 0.0;
         nZeroes++;
      }
   }
   printf("Number of zeroes detected:%d\n", nZeroes);
   for (i=0; i<nElements; i++) {
      printf("Element:%02d, Value:%3.4lf\n", i, lfArray[i]);
   }

   return 0;
}

示例输出 - 案例2


Number of zeroes detected:9
Element:00, Value:-0.0020
Element:01, Value:-0.0015
Element:02, Value:0.0010
Element:03, Value:0.0000
Element:04, Value:0.0000
Element:05, Value:-0.0011
Element:06, Value:-0.0018
Element:07, Value:0.0000
Element:08, Value:0.0000
Element:09, Value:0.0017
Element:10, Value:0.0000
Element:11, Value:0.0000
Element:12, Value:0.0013
Element:13, Value:-0.0019
Element:14, Value:-0.0018
Element:15, Value:0.0000
Element:16, Value:0.0000
Element:17, Value:-0.0020
Element:18, Value:0.0000
Element:19, Value:-0.0017

<强>参考


  1. Double fabs() ,已访问2014-03-28,<http://www.cplusplus.com/reference/cmath/fabs/>
  2. 生成随机双号,访问次数2014-03-28,<https://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c>
  3. 截断,访问次数2014-03-28,<http://en.wikipedia.org/wiki/Truncation>