diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-02-27 20:08:23 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-02-27 20:08:23 -0500 |
commit | faa4adc9bb20a530e1195a9ee9b9cadeb07e96de (patch) | |
tree | 90c59a562e703870b51780af06f7f32dd71dc8a2 | |
parent | 9ecdb9893c6b040aeb67560aa6c4f2810a42a1e4 (diff) |
consistent (imax|ll|l)abs() behavior and documentation
-rw-r--r-- | src/inttypes/imaxabs.c | 20 | ||||
-rw-r--r-- | src/stdlib/labs.c | 4 | ||||
-rw-r--r-- | src/stdlib/llabs.c | 15 |
3 files changed, 21 insertions, 18 deletions
diff --git a/src/inttypes/imaxabs.c b/src/inttypes/imaxabs.c index 7d6be923..be184ebf 100644 --- a/src/inttypes/imaxabs.c +++ b/src/inttypes/imaxabs.c @@ -1,28 +1,24 @@ #include "stddef.h" #include <inttypes.h> +/** absolute value **/ + intmax_t imaxabs(intmax_t j) { - /* returns the absolute value of j */ - /* if imaxabs(j) can't be representeted (i.e. imaxabs(INTMAX_MIN), behavior is undefined */ -/* - if (j == INTMAX_MIN) { - __ungol_libc_undefined("Cannot represent absolute value of %" PRIxMAX "\n", j); - } -*/ if (j == INTMAX_MIN) { /* undefined behavior */ return INTMAX_MIN; } - if (j < 0) { - return -j; - } - - return j; + return j < 0 ? -j : j; } +/*** +computes the absolute value of ARGUMENT(j). +***/ /* +UNDEFINED(ABS(ARGUMENT(j)) cannot be represented +RETURN_SUCCESS(ABS(ARGUMENT(j)) STDC(199901) */ diff --git a/src/stdlib/labs.c b/src/stdlib/labs.c index fd092f5a..298c2e64 100644 --- a/src/stdlib/labs.c +++ b/src/stdlib/labs.c @@ -2,17 +2,19 @@ #include "limits.h" /** absolute value **/ + long int labs(long int j) { if (j == LONG_MIN) { /* undefined */ + return LONG_MIN; } return j < 0 ? -j : j; } /*** -function computes the absolute value of ARGUMENT(j). +computes the absolute value of ARGUMENT(j). ***/ /* diff --git a/src/stdlib/llabs.c b/src/stdlib/llabs.c index 6ccf9c49..9fc00ab6 100644 --- a/src/stdlib/llabs.c +++ b/src/stdlib/llabs.c @@ -1,19 +1,24 @@ #include <stdlib.h> #include "limits.h" +/** absolute value **/ + long long int llabs(long long int j) { if (j == LLONG_MIN) { /* undefined */ + return LLONG_MIN; } - if (j < 0) { - return -j; - } - - return j; + return j < 0 ? -j : j; } +/*** +computes the absolute value of ARGUMENT(j). +***/ + /* +UNDEFINED(ABS(ARGUMENT(j)) cannot be represented +RETURN_SUCCESS(ABS(ARGUMENT(j)) STDC(199901) */ |