Re: Suppressing the warning: "variably modified '<variable>' at file scope" generated due to floating-point computation in an integer expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 20/12/2024 12:46, beni.falk--- via Gcc-help wrote:
#define X_MIN -7.5

#define X_MAX 12.7

#define X_STEP 0.1

#define NPOINTS ((unsigned) (X_MAX - X_MIN) / X_STEP))

static int state[NPOINTS];

First, I hope you also got an error about the incorrect parentheses for NPOINTS - when giving sample code, it's a good idea to test it!

The obvious fix here is :


#define X_MIN -75
#define X_MAX 127
#define X_STEP 01

#define NPOINTS (unsigned) ((X_MAX - X_MIN) / X_STEP)

int state[NPOINTS];


This has the additional advantage of getting NPOINTS correct. When you use floating point, you can get surprising rounding errors - your floating point version gave an array of 201 int's, while the expected answer (which you get from the integer arithmetic version) is 202.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux