Reputation: 563
Say that my C code uses a constant RANGEMAX
that is the largest power of 10 fitting in an int
. I can define it with:
#include <limits.h>
#if (INT_MAX < 1)
#define RANGEMAX ERROR1
#elif (INT_MAX >= 1) && (INT_MAX < 10)
#define RANGEMAX 1
#elif (INT_MAX >= 10) && (INT_MAX < 100)
#define RANGEMAX 10
#elif (INT_MAX >= 100) && (INT_MAX < 1000)
#define RANGEMAX 100
... /* well, you get the picture */
#elif (INT_MAX >= 10000000000000000)
#define RANGEMAX ERROR2
#endif
Is there a smarter way of doing such simple computations in the macro preprocessing phase?
And since these are "simple" computations, I prefer solutions that an average guy like me will understand by just reading the code.
Upvotes: 0
Views: 143
Reputation: 154242
With a series of test confirming the approximate range of INT_MAX
, multiplication by 1, 10, 100, 10000, 100000000, 10000000000000000, etc. can be done.
The following should work for 10000 <= INT_MAX < power(10,32)
#include <stdio.h>
#include <limits.h>
#define F1(x) (1)
#define F2(x) (((x) >= 10) ? 10 * F1((x)/10): F1(x))
#define F3(x) (((x) >= 100) ? 100 * F2((x)/100): F2(x))
#define F4(x) (((x) >= 10000) ? 10000 * F3((x)/10000): F3(x))
#define F5(x) (((x) >= 100000000) ? 100000000 * F4((x)/100000000): F4(x))
#define ITEST (INT_MAX)
#if ITEST/10000 >= 10000
#if ITEST/100000000 >= 100000000
#define INT10_MAX (10000000000000000 * F6(ITEST/10000000000000000))
#else
#define INT10_MAX (100000000 * F5(ITEST/100000000))
#endif
#else
#define INT10_MAX (10000 * F4(ITEST/10000))
#endif
int main(void) {
printf("%d\n", ITEST);
printf("%d\n", INT10_MAX);
return 0;
}
A pragmatic approach might assume INT_MAX
to be power(2,15)-1
, or power(2,31)-1
, power(2,63)-1
. Macro arithmetic needs to be careful about using values outside 32-bit range.
#if INT_MAX/10000 == 3
#define INT10_MAX 10000
#elif INT_MAX/1000000000 == 2
#define INT10_MAX 1000000000
#elif INT_MAX/1000000000/1000000000 == 9
#define INT10_MAX 1000000000000000000
#else
#error Unusual INT_MAX
#endif
Upvotes: 1
Reputation: 25189
INT_MAX
is defined to be at least 32767 (i.e. 2^15-1). As int
is signed, in practice you need only check for 2^15-1, 2^31-1 and (theoretically) 2^63-1. This will reduce the size of your #if
block.
Upvotes: 3