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

标签 c

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

假设我有一个 vector

double values[size];

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

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

但是这不会返回与 counter 相同的值

if(counter > 0.0000001 || counter < -0.000001)

最佳答案

您使用了错误的功能。 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. fabs() ,2014 年 3 月 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 年 3 月 28 日访问, <http://en.wikipedia.org/wiki/Truncation>

关于c - 我在 C 中使用绝对值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22717840/

相关文章:

python - SciPy 的 ctypes Fibonacci 示例无法运行,并出现 "array must have data type int64"错误

c - 带有消息的 assert()

c++ - 数组在通过引用传递给函数之前和之后显示不同的地址

c - 为什么这个C程序的输出是0?

c - "implicit declaration of function"与函数原版的区别

c - 仅从程序计数器获取代码行信息?

c - 在C中逐字迭代字符串

c - 使用输入字符串并逐字符打印出来

c - 如何使用lldb调试segmentation fault

c - 从 stdin 读取输入到指针数组中直到 EOF,但没有任何输出?