1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 1994-2009  Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34 FUNCTION
35 <<abs>>---integer absolute value (magnitude)
36 
37 INDEX
38 	abs
39 
40 ANSI_SYNOPSIS
41 	#include <stdlib.h>
42 	int abs(int <[i]>);
43 
44 TRAD_SYNOPSIS
45 	#include <stdlib.h>
46 	int abs(<[i]>)
47 	int <[i]>;
48 
49 DESCRIPTION
50 <<abs>> returns
51 @tex
52 $|x|$,
53 @end tex
54 the absolute value of <[i]> (also called the magnitude
55 of <[i]>).  That is, if <[i]> is negative, the result is the opposite
56 of <[i]>, but if <[i]> is nonnegative the result is <[i]>.
57 
58 The similar function <<labs>> uses and returns <<long>> rather than <<int>> values.
59 
60 RETURNS
61 The result is a nonnegative integer.
62 
63 PORTABILITY
64 <<abs>> is ANSI.
65 
66 No supporting OS subroutines are required.
67 */
68 
69 #include "_ansi.h"
70 #include <stdlib.h>
71 
72 int
73 _DEFUN (abs, (i), int i)
74 {
75   return (i < 0) ? -i : i;
76 }
77