3
3
* and long variables, both signed and unsigned, by printing appropriate values
4
4
* from standard headers and by direct computation. Harder if you compute them:
5
5
* determine the ranges of the various floating-point types.
6
+ *
7
+ * For signed variables:
8
+ * largest negative number is -(2^nbits-1)
9
+ * largest positive number is (2^nbits-1)-1
10
+ *
11
+ * The loop iterates until the variable overflows the positive limit by 1. In
12
+ * binary (two's complement), this results in achieving the largest negative
13
+ * number with the loop's index and the max number with the result of the
14
+ * calculation inside the loop minus 1. Note: we can also achieve the largest
15
+ * positive number by negating the loop index and subtract one from it.
16
+ * However; the cost of one extra variable (long long max), we gain in code
17
+ * clarity.
18
+ *
19
+ * For unsigned variables:
20
+ * min is 0
21
+ * max (2^nbits)-1
22
+ *
23
+ * We do the same for the loop but we iterate the other way: we start from -1 all
24
+ * the way up to 0.
25
+ *
26
+ * for floating point types:
27
+ * refer to IEEE 754 standards
28
+ *
6
29
* By Faisal Saadatmand
7
30
*/
8
31
@@ -17,70 +40,75 @@ void stdLibraryMacros(void);
17
40
18
41
void computeRanges (void )
19
42
{
43
+ long long unsigned int max ;
44
+
20
45
/* characters */
21
- signed char c ;
22
- int maxC = 1 ;
23
- for (c = 1 ; c > 0 ; c *= 2 )
24
- maxC *= 2 ;
25
- printf ("signed char\t\t%i\t\t\t%i \n" , c , maxC - 1 );
46
+ signed char c ;
47
+
48
+ for (c = max = 1 ; c > 0 ; c *= 2 )
49
+ max *= 2 ;
50
+ printf ("signed char\t\t%i\t\t\t%llu \n" , c , max - 1 );
26
51
27
52
unsigned char uc ;
53
+
28
54
for (uc = -1 ; uc < 0 ; uc *= 2 )
29
- ;
30
- printf ("unsigned char\t\t%2i\t\t\t%i \n" , 0 , uc );
55
+ max *= 2 ;
56
+ printf ("unsigned char\t\t%2i\t\t\t%u \n" , 0 , uc );
31
57
32
58
/* Integers */
33
59
signed short shrt ;
34
- int maxShrt = 1 ;
35
- for (shrt = 1 ; shrt > 0 ; shrt *= 2 )
36
- maxShrt *= 2 ;
37
- printf ("signed short\t\t%i\t\t\t%i \n" , shrt , maxShrt - 1 );
60
+
61
+ for (shrt = max = 1 ; shrt > 0 ; shrt *= 2 )
62
+ max *= 2 ;
63
+ printf ("signed short\t\t%i\t\t\t%llu \n" , shrt , max - 1 );
38
64
39
65
unsigned short uShrt ;
66
+
40
67
for (uShrt = -1 ; uShrt < 0 ; uShrt *= 2 )
41
- ;
42
- printf ("unsigned short\t\t%2i\t\t\t%i\n" , 0 , uShrt );
68
+ max += uShrt ;
69
+ printf ("unsigned short\t\t%2i\t\t\t%u\n" , 0 , uShrt );
70
+
71
+ signed int i ;
72
+
73
+ for (i = max = 1 ; i > 0 ; i *= 2 )
74
+ max *= 2 ;
75
+ printf ("signed int\t\t%i\t\t%llu\n" , i , max - 1 );
43
76
44
- signed int sInt ;
45
- int max_sInt = 1 ;
46
- for (sInt = 1 ; sInt > 0 ; sInt *= 2 )
47
- max_sInt *= 2 ;
48
- printf ("signed int\t\t%i\t\t%i\n" , sInt , max_sInt - 1 );
77
+ unsigned int ui ;
49
78
50
- unsigned int uInt ;
51
- for (uInt = -1 ; uInt < 0 ; uInt *= 2 )
79
+ for (ui = -1 ; ui < 0 ; ui *= 2 )
52
80
;
53
- printf ("unsigned int\t\t%2i \t\t\t%u\n" , 0 , uInt );
81
+ printf ("unsigned int\t\t%2u \t\t\t%u\n" , 0 , ui );
54
82
55
- signed long int lInt ;
56
- signed long int max_lInt = 1 ;
57
- for (lInt = 1 ; lInt > 0 ; lInt *= 2 )
58
- max_lInt *= 2 ;
59
- printf ("signed long\t\t%li\t%li\n" , lInt , max_lInt - 1 );
83
+ signed long int li ;
60
84
61
- unsigned long int ulInt ;
62
- for (ulInt = -1 ; ulInt < 0 ; ulInt *= 2 )
85
+ for (li = max = 1 ; li > 0 ; li *= 2 )
86
+ max *= 2 ;
87
+ printf ("signed long\t\t%li\t%llu\n" , li , max - 1 );
88
+
89
+ unsigned long int uli ;
90
+
91
+ for (uli = -1 ; uli < 0 ; uli *= 2 )
63
92
;
64
- printf ("unsigned long\t\t%2i\t\t\t%lu\n" , 0 , ulInt );
93
+ printf ("unsigned long\t\t%2u\t\t\t%lu\n" , 0 , uli );
94
+
95
+ signed long long lli ;
65
96
66
- signed long long llInt ;
67
- signed long long max_llInt = 1 ;
68
- for (llInt = 1 ; llInt > 0 ; llInt *= 2 )
69
- max_llInt *= 2 ;
70
- printf ("signed long long\t%lli\t%lli\n" , llInt , max_llInt - 1 );
97
+ for (lli = max = 1 ; lli > 0 ; lli *= 2 )
98
+ max *= 2 ;
99
+ printf ("signed long long\t%lli\t%llu\n" , lli , max - 1 );
71
100
72
- unsigned long long ullInt ;
73
- for (ullInt = -1 ; ullInt < 0 ; ullInt *= 2 )
101
+ unsigned long long ulli ;
102
+ for (ulli = -1 ; ulli < 0 ; ulli *= 2 )
74
103
;
75
- printf ("unsigned long long\t%2i\t\t\t%llu\n" , 0 , ullInt );
104
+ printf ("unsigned long long\t%2i\t\t\t%llu\n" , 0 , ulli );
76
105
77
106
printf ("\n" );
78
107
79
108
/* floating points */
80
109
/* see IEEE 754 standards */
81
110
float fltMin , fltMax ;
82
111
double dblMin , dblMax , mantissa , exponent ;
83
- int i ;
84
112
85
113
mantissa = 1.0 ;
86
114
exponent = 1.0 ;
0 commit comments