|
|
1.1 root 1:
2:
3: abs() General Function abs()
4:
5:
6:
7:
8: Return the absolute value of an integer
9:
10: iinntt aabbss(_n) iinntt _n;
11:
12: abs returns the absolute value of integer _n. The _a_b_s_o_l_u_t_e _v_a_l_u_e
13: of a number is its distance from zero. This is _n if _n>=00, and -_n
14: otherwise.
15:
16: ***** Example *****
17:
18: This example prompts for a number, and returns its absolute
19: value.
20:
21:
22: #include <ctype.h>
23: #include <stdio.h>
24:
25:
26:
27: main()
28: {
29: extern char *gets();
30: extern int atoi();
31: char string[64];
32: int counter;
33: int input;
34:
35:
36:
37: printf("Enter an integer: ");
38: fflush(stdout);
39: gets(string);
40:
41:
42:
43: for (counter=0; counter < strlen(string); counter++) {
44: input = string[counter];
45:
46:
47:
48: if (!isascii(input)) {
49: fprintf(stderr,
50: "%s is not ASCII\n", string);
51: exit(1);
52: }
53:
54:
55:
56: if (!isdigit(input))
57: if (input != '-' || counter != 0) {
58: fprintf(stderr,
59: "%s is not a number\n", string);
60: exit(1);
61: }
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: abs() General Function abs()
70:
71:
72:
73: }
74:
75:
76:
77: input = atoi(string);
78: printf("abs(%d) is %d\n", input, abs(input));
79: exit(0);
80: }
81:
82:
83: ***** See Also *****
84:
85: fabs(), floor(), general functions, int
86:
87: ***** Notes *****
88:
89: On two's complement machines, the aabbss of the most negative in-
90: teger is itself.
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.