MAX.3, MIN.3: New page (and link page) to document MAX() and MIN()
PATCH 1/1 MAX.3, MIN.3: New page (and link page) to document MAX() and MIN()
---
Example tested:
$ man max \ | sed -n /^EXAMPLES/,/^[[:alpha:]]/p \ | grep -v '^[[:alpha:]]' \ | cc -Wall -Wextra -Werror -x c -; $ ./a.out 65 38 MAX(65, 38) is 65
man3/MAX.3 | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man3/MIN.3 | 1 + 2 files changed, 86 insertions(+) create mode 100644 man3/MAX.3 create mode 100644 man3/MIN.3
man3/MAX.3 new file +85 -0
@@ -0,0 +1,85 @@ +.\" Copyright (C) 2021 Alejandro Colomar <[email protected]> +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH MAX 3 2020-11-01 "Linux" "Linux Programmer's Manual" +.SH NAME +MAX, MIN \- maximum or minimum of two values +.SH SYNOPSIS +.nf +.B #include <sys/param.h> +.PP +.BI MAX( a ", " b ); +.BI MIN( a ", " b ); +.fi +.SH DESCRIPTION +These macros return the maximum or minimum of +.I a +and +.IR b . +.PP +If any of the arguments is of a floating-point type, +these macros shouldn't be used; see +.BR fmax (3) +or +.BR fmin (3) +instead. +.PP +The arguments may be evaluated more than once, +and their types might be promoted to a common type +if both arguments aren't of the same type. +.SH RETURN VALUE +These macros return the value of one of their arguments, +according to their relationship. +.SH ERRORS +These macros may raise the "invalid" floating-point exception +when any of the arguments is NaN. +.SH CONFORMING TO +These macros are present in glibc and the BSDs. +.SH EXAMPLES +.EX +#include <stdio.h> +#include <stdlib.h> +#include <sys/param.h> + +int +main(int argc, char *argv[]) +{ + int a, b, x; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <num> <num>\en", argv[0]); + exit(EXIT_FAILURE); + } + + a = atoi(argv[1]); + b = atoi(argv[2]); + x = MAX(a, b); + printf("MAX(%d, %d) is %d\en", a, b, x); + + exit(EXIT_SUCCESS); +} +.EE +.SH SEE ALSO +.BR fmax (3), +.BR fmin (3)
man3/MIN.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/MAX.3
Signature · 1 line
2.31.1
PATCH v2 1/1 MAX.3, MIN.3: New page (and link page) to document MAX() and MIN()
---
v2:
- Reword paragraph about fmax(3) and fmin(3) in DESCRIPTION.
man3/MAX.3 | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man3/MIN.3 | 1 + 2 files changed, 86 insertions(+) create mode 100644 man3/MAX.3 create mode 100644 man3/MIN.3
man3/MAX.3 new file +85 -0
@@ -0,0 +1,85 @@ +.\" Copyright (C) 2021 Alejandro Colomar <[email protected]> +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH MAX 3 2020-11-01 "Linux" "Linux Programmer's Manual" +.SH NAME +MAX, MIN \- maximum or minimum of two values +.SH SYNOPSIS +.nf +.B #include <sys/param.h> +.PP +.BI MAX( a ", " b ); +.BI MIN( a ", " b ); +.fi +.SH DESCRIPTION +These macros return the maximum or minimum of +.I a +and +.IR b . +.PP +If any of the arguments is of a floating-point type, +you might prefer to use +.BR fmax (3) +or +.BR fmin (3), +which can handle NaN. +.PP +The arguments may be evaluated more than once, +and their types might be promoted to a common type +if both arguments aren't of the same type. +.SH RETURN VALUE +These macros return the value of one of their arguments, +according to their relationship. +.SH ERRORS +These macros may raise the "invalid" floating-point exception +when any of the arguments is NaN. +.SH CONFORMING TO +These macros are present in glibc and the BSDs. +.SH EXAMPLES +.EX +#include <stdio.h> +#include <stdlib.h> +#include <sys/param.h> + +int +main(int argc, char *argv[]) +{ + int a, b, x; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <num> <num>\en", argv[0]); + exit(EXIT_FAILURE); + } + + a = atoi(argv[1]); + b = atoi(argv[2]); + x = MAX(a, b); + printf("MAX(%d, %d) is %d\en", a, b, x); + + exit(EXIT_SUCCESS); +} +.EE +.SH SEE ALSO +.BR fmax (3), +.BR fmin (3)
man3/MIN.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/MAX.3
Signature · 1 line
2.31.1
Hi Michael,
man3/MAX.3 line 46
Quoted 12 lines · Alejandro Colomar
+.SH DESCRIPTION
man3/MAX.3 new file +11
-0+These macros return the maximum or minimum of +.I a +and +.IR b . +.PP +If any of the arguments is of a floating-point type, +you might prefer to use +.BR fmax (3) +or +.BR fmin (3), +which can handle NaN.
Would you move this to a NOTES section?
Thanks,
Alex
Signature · 3 lines
Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/
Quoted 1 line · Alejandro Colomar via Libc-alpha
+If any of the arguments is of a floating-point type,
"any" -> "either"
Quoted 1 line
+these macros shouldn't be used
That's too strong. It's often OK to use MAX and MIN on floating point arguments.
man3/MAX.3 line 50
Quoted 3 lines
man3/MAX.3 new file +3
-0+The arguments may be evaluated more than once, +and their types might be promoted to a common type +if both arguments aren't of the same type.
This is muddy. It should state clearly that even if A and B are both integers, MAX (a, b) might not return their maximum. For example, on a typical C platform today, MAX (-1, 2147483648) returns 4294967295 and MIN (-1, 2147483648) returns 2147483648.
Also, the man page shouldn't require the arguments to be evaluated at least once. It's possible to implement MAX so that it sometimes doesn't evaluate one argument, and the documentation shouldn't preclude such an implementation.
The man page should more specifically mention that although MIN and MAX are defined <sys/param.h> on GNU platforms, other platforms define them elsewhere or not at all.
Quoted 1 line
+These macros return the value of one of their arguments,
Unfortunately they don't necessarily do that, as shown in the MAX example above.
I suggest looking at the remarks about MAX and MIN that are made here, and incorporating the useful parts of these remarks into the man page:
https://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/minmax.h
Hello Paul,
Quoted 5 lines · Paul Eggert
Quoted 1 line · Alejandro Colomar via Libc-alpha
+If any of the arguments is of a floating-point type,
"any" -> "either"
Okay. I thought that either meant XOR, but now I learnt it may also be OR.
Quoted 5 lines
Quoted 2 lines
+these macros shouldn't be used
That's too strong. It's often OK to use MAX and MIN on floating point arguments.
Yup, I changed it a few minutes later.
Quoted 9 lines
man3/MAX.3 line 50
Quoted 4 lines
man3/MAX.3 new file +3
-0+The arguments may be evaluated more than once, +and their types might be promoted to a common type +if both arguments aren't of the same type.This is muddy. It should state clearly that even if A and B are both integers, MAX (a, b) might not return their maximum. For example, on a typical C platform today, MAX (-1, 2147483648) returns 4294967295 and MIN (-1, 2147483648) returns 2147483648.
Hmm, yes, I'll add a BUGS section for this. Usual arithmetic conversions and why you should avoid them :)
Quoted 5 lines
Also, the man page shouldn't require the arguments to be evaluated at least once. It's possible to implement MAX so that it sometimes doesn't evaluate one argument, and the documentation shouldn't preclude such an implementation.
In which case they might not be evaluated at all? I'm curious. Maybe if one of the arguments is literal NAN and the implementation raises an exception for it?
Quoted 4 lines
The man page should more specifically mention that although MIN and MAX are defined <
sys/param.h> on GNU platforms, other platforms define them elsewhere or not at all.
Okay.
Quoted 5 lines
Quoted 2 lines
+These macros return the value of one of their arguments,
Unfortunately they don't necessarily do that, as shown in the MAX example above.
Fixed.
Quoted 6 lines
I suggest looking at the remarks about MAX and MIN that are made here, and incorporating the useful parts of these remarks into the man page:
https://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/minmax.h
Okay.
Thank you very much!
Cheers,
Alex
Signature · 3 lines
Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/
Quoted 3 lines · Alejandro Colomar (man-pages)
In which case they might not be evaluated at all? I'm curious. Maybe if one of the arguments is literal NAN and the implementation raises an exception for it?
Or, one argument of MIN might be negative and the other might be unsigned short. In that case, the unsigned short argument need not be evaluated at all. Admittedly not something we'd be likely to optimize for, but the documentation shouldn't preclude such optimization.
PATCH v3 1/1 MAX.3, MIN.3: New pages to document MAX() and MIN()
---
v2:
wfix
v3:
Adress Paul's review (rewrote most sections, and added BUGS and NOTES).
man3/MAX.3 | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man3/MIN.3 | 1 + 2 files changed, 91 insertions(+) create mode 100644 man3/MAX.3 create mode 100644 man3/MIN.3
man3/MAX.3 new file +90 -0
@@ -0,0 +1,90 @@ +.\" Copyright (C) 2021 Alejandro Colomar <[email protected]> +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH MAX 3 2020-11-01 "Linux" "Linux Programmer's Manual" +.SH NAME +MAX, MIN \- maximum or minimum of two values +.SH SYNOPSIS +.nf +.B #include <sys/param.h> +.PP +.BI MAX( a ", " b ); +.BI MIN( a ", " b ); +.fi +.SH DESCRIPTION +These macros return the maximum or minimum of +.I a +and +.IR b . +.SH RETURN VALUE +These macros return the value of one of their arguments, +possibly converted to a different type (see BUGS). +.SH ERRORS +These macros may raise the "invalid" floating-point exception +when any of the arguments is NaN. +.SH CONFORMING TO +These nonstandard macros are present in glibc and the BSDs. +.SH NOTES +If either of the arguments is of a floating-point type, +you might prefer to use +.BR fmax (3) +or +.BR fmin (3), +which can handle NaN. +.PP +The arguments may be evaluated more than once, or not at all. +.PP +Some UNIX systems might provide these macros in a different header, +or not at all. +.SH BUGS +Due to the usual arithmetic conversions, +the result of these macros may be very different from either of the arguments. +To avoid this, ensure that both arguments have the same type. +.SH EXAMPLES +.EX +#include <stdio.h> +#include <stdlib.h> +#include <sys/param.h> + +int +main(int argc, char *argv[]) +{ + int a, b, x; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <num> <num>\en", argv[0]); + exit(EXIT_FAILURE); + } + + a = atoi(argv[1]); + b = atoi(argv[2]); + x = MAX(a, b); + printf("MAX(%d, %d) is %d\en", a, b, x); + + exit(EXIT_SUCCESS); +} +.EE +.SH SEE ALSO +.BR fmax (3), +.BR fmin (3)
man3/MIN.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/MAX.3
Signature · 1 line
2.31.1
Ping: [PATCH v3] MAX.3, MIN.3: New pages to document MAX() and MIN()
Ping
Quoted 120 lines · Alejandro Colomar
Signed-off-by Alejandro Colomar <[email protected]>Cc Paul Eggert <[email protected]>---
v2:
wfix
v3:
Adress Paul's review (rewrote most sections, and added BUGS and NOTES).man3/MAX.3 | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man3/MIN.3 | 1 + 2 files changed, 91 insertions(+) create mode 100644 man3/MAX.3 create mode 100644 man3/MIN.3man3/MAX.3 new file +90
-0@@ -0,0 +1,90 @@ +.\" Copyright (C) 2021 Alejandro Colomar <[email protected]> +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH MAX 3 2020-11-01 "Linux" "Linux Programmer's Manual" +.SH NAME +MAX, MIN \- maximum or minimum of two values +.SH SYNOPSIS +.nf +.B #include <sys/param.h> +.PP +.BI MAX( a ", " b ); +.BI MIN( a ", " b ); +.fi +.SH DESCRIPTION +These macros return the maximum or minimum of +.I a +and +.IR b . +.SH RETURN VALUE +These macros return the value of one of their arguments, +possibly converted to a different type (see BUGS). +.SH ERRORS +These macros may raise the "invalid" floating-point exception +when any of the arguments is NaN. +.SH CONFORMING TO +These nonstandard macros are present in glibc and the BSDs. +.SH NOTES +If either of the arguments is of a floating-point type, +you might prefer to use +.BR fmax (3) +or +.BR fmin (3), +which can handle NaN. +.PP +The arguments may be evaluated more than once, or not at all. +.PP +Some UNIX systems might provide these macros in a different header, +or not at all. +.SH BUGS +Due to the usual arithmetic conversions, +the result of these macros may be very different from either of the arguments. +To avoid this, ensure that both arguments have the same type. +.SH EXAMPLES +.EX +#include <stdio.h> +#include <stdlib.h> +#include <sys/param.h> + +int +main(int argc, char *argv[]) +{ + int a, b, x; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <num> <num>\en", argv[0]); + exit(EXIT_FAILURE); + } + + a = atoi(argv[1]); + b = atoi(argv[2]); + x = MAX(a, b); + printf("MAX(%d, %d) is %d\en", a, b, x); + + exit(EXIT_SUCCESS); +} +.EE +.SH SEE ALSO +.BR fmax (3), +.BR fmin (3)man3/MIN.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/MAX.3
Signature · 3 lines
Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/
Re: [PATCH v3] MAX.3, MIN.3: New pages to document MAX() and MIN()
In reply to Alejandro ColomarHi Alex,
Quoted 8 lines · Alejandro Colomar
Signed-off-by Alejandro Colomar <[email protected]>Cc Paul Eggert <[email protected]>---
v2:
wfix
v3:
Adress Paul's review (rewrote most sections, and added BUGS and NOTES).
Thanks.
Patch applied.
Cheers,
Michael
Quoted 110 lines
man3/MAX.3 | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man3/MIN.3 | 1 + 2 files changed, 91 insertions(+) create mode 100644 man3/MAX.3 create mode 100644 man3/MIN.3man3/MAX.3 new file +90
-0@@ -0,0 +1,90 @@ +.\" Copyright (C) 2021 Alejandro Colomar <[email protected]> +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH MAX 3 2020-11-01 "Linux" "Linux Programmer's Manual" +.SH NAME +MAX, MIN \- maximum or minimum of two values +.SH SYNOPSIS +.nf +.B #include <sys/param.h> +.PP +.BI MAX( a ", " b ); +.BI MIN( a ", " b ); +.fi +.SH DESCRIPTION +These macros return the maximum or minimum of +.I a +and +.IR b . +.SH RETURN VALUE +These macros return the value of one of their arguments, +possibly converted to a different type (see BUGS). +.SH ERRORS +These macros may raise the "invalid" floating-point exception +when any of the arguments is NaN. +.SH CONFORMING TO +These nonstandard macros are present in glibc and the BSDs. +.SH NOTES +If either of the arguments is of a floating-point type, +you might prefer to use +.BR fmax (3) +or +.BR fmin (3), +which can handle NaN. +.PP +The arguments may be evaluated more than once, or not at all. +.PP +Some UNIX systems might provide these macros in a different header, +or not at all. +.SH BUGS +Due to the usual arithmetic conversions, +the result of these macros may be very different from either of the arguments. +To avoid this, ensure that both arguments have the same type. +.SH EXAMPLES +.EX +#include <stdio.h> +#include <stdlib.h> +#include <sys/param.h> + +int +main(int argc, char *argv[]) +{ + int a, b, x; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <num> <num>\en", argv[0]); + exit(EXIT_FAILURE); + } + + a = atoi(argv[1]); + b = atoi(argv[2]); + x = MAX(a, b); + printf("MAX(%d, %d) is %d\en", a, b, x); + + exit(EXIT_SUCCESS); +} +.EE +.SH SEE ALSO +.BR fmax (3), +.BR fmin (3)man3/MIN.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/MAX.3
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/