diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | NEWS | 8 | ||||
-rw-r--r-- | awk.h | 9 | ||||
-rw-r--r-- | awklib/eg/test-programs/gen-float-table.awk | 59 | ||||
-rw-r--r-- | awklib/eg/test-programs/gen-float-table.c | 60 | ||||
-rw-r--r-- | awklib/eg/test-programs/gen-float-table.py | 42 | ||||
-rw-r--r-- | builtin.c | 4 | ||||
-rw-r--r-- | doc/ChangeLog | 27 | ||||
-rw-r--r-- | doc/gawk.info | 1284 | ||||
-rw-r--r-- | doc/gawk.texi | 273 | ||||
-rw-r--r-- | doc/gawktexi.in | 273 | ||||
-rw-r--r-- | doc/it/ChangeLog | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | doc/it/gawktexi.in | 308 | ||||
-rw-r--r-- | doc/wordlist | 2 | ||||
-rw-r--r-- | eval.c | 96 | ||||
-rw-r--r-- | interpret.h | 12 | ||||
-rw-r--r-- | mpfr.c | 44 | ||||
-rw-r--r-- | node.c | 5 |
18 files changed, 1804 insertions, 719 deletions
@@ -1,5 +1,18 @@ 2020-11-02 Arnold D. Robbins <arnold@skeeve.com> + Make gawk numeric comparisons act like C doubles. + MPFR differs from doubles w.r.t. NaN, not sure why yet. + + * awk.h (scalar_cmp_t): New enum. + * builtin.c (format_nan_inf): Use mpfr_signbit, not mpfr_sgn. + * eval.c (cmp_doubles): New routine. + (cmp_scalars): Change type to bool, rework logic. + * interpret.h (r_interpret): Rework scalar comparisons. + * mpfr.c (mpg_cmp_as_numbers): New routine. + * node.c: Use <math.h>, not "math.h", minor comment edits. + +2020-11-02 Arnold D. Robbins <arnold@skeeve.com> + * re.c (make_regexp): Cast len parameter to int to avoid compiler warnings. @@ -4,6 +4,14 @@ are permitted in any medium without royalty provided the copyright notice and this notice are preserved. +Changes from 5.1.x to 5.2.0 +--------------------------- + +1. Numeric scalars now compare in the same way as C for the relational +operators. Comparison order for sorting has not changed. This only +makes a difference when comparing Infinity and NaN values with +regular numbers; it should not be noticeable most of the time. + Changes from 5.1.0 to 5.1.1 --------------------------- @@ -1574,6 +1574,15 @@ typedef enum { extern field_sep_type current_field_sep(void); extern const char *current_field_sep_str(void); +typedef enum { + SCALAR_EQ, + SCALAR_NEQ, + SCALAR_LT, + SCALAR_LE, + SCALAR_GT, + SCALAR_GE, +} scalar_cmp_t; + /* gawkapi.c: */ extern gawk_api_t api_impl; extern void init_ext_api(void); diff --git a/awklib/eg/test-programs/gen-float-table.awk b/awklib/eg/test-programs/gen-float-table.awk new file mode 100644 index 00000000..c35f2dff --- /dev/null +++ b/awklib/eg/test-programs/gen-float-table.awk @@ -0,0 +1,59 @@ +function eq(left, right) +{ + return left == right +} + +function ne(left, right) +{ + return left != right +} + +function lt(left, right) +{ + return left < right +} + +function le(left, right) +{ + return left <= right +} + +function gt(left, right) +{ + return left > right +} + +function ge(left, right) +{ + return left >= right +} + +BEGIN { + nan = sqrt(-1) + inf = -log(0) + split("== != < <= > >=", names) + names[3] = names[3] " " + names[5] = names[5] " " + split("eq ne lt le gt ge", funcs) + + compare[1] = 2.0 + compare[2] = values[1] = -sqrt(-1.0) # nan + compare[3] = values[2] = sqrt(-1.0) # -nan + compare[4] = values[3] = -log(0.0) # inf + compare[5] = values[4] = log(0.0) # -inf + + for (i = 1; i in values; i++) { + for (j = 1; j in compare; j++) { + for (k = 1; k in names; k++) { + the_func = funcs[k] + printf("%g %s %g -> %s\n", + values[i], + names[k], + compare[j], + the_func(values[i], compare[j]) ? + "true" : "false"); + } + printf("\n"); + } + } +} diff --git a/awklib/eg/test-programs/gen-float-table.c b/awklib/eg/test-programs/gen-float-table.c new file mode 100644 index 00000000..ae1d5dd4 --- /dev/null +++ b/awklib/eg/test-programs/gen-float-table.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <math.h> +#include <stdbool.h> + +#define def_func(name, op) \ + bool name(double left, double right) { \ + return left op right; \ + } + +def_func(eq, ==) +def_func(ne, !=) +def_func(lt, <) +def_func(le, <=) +def_func(gt, >) +def_func(ge, >=) + +struct { + const char *name; + bool (*func)(double left, double right); +} functions[] = { + { "==", eq }, + { "!=", ne }, + { "< ", lt }, + { "<=", le }, + { "> ", gt }, + { ">=", ge }, + { 0, 0 } +}; + +int main() +{ + double values[] = { + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + double compare[] = { 2.0, + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + + int i, j, k; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 5; j++) { + for (k = 0; functions[k].name != NULL; k++) { + printf("%g %s %g -> %s\n", values[i], + functions[k].name, + compare[j], + functions[k].func(values[i], compare[j]) ? "true" : "false"); + } + printf("\n"); + } + } + + return 0; +} diff --git a/awklib/eg/test-programs/gen-float-table.py b/awklib/eg/test-programs/gen-float-table.py new file mode 100644 index 00000000..8631b817 --- /dev/null +++ b/awklib/eg/test-programs/gen-float-table.py @@ -0,0 +1,42 @@ +from math import * + +nan = float('NaN') +inf = float('Inf') + +def eq(left, right): + return left == right + +def ne(left, right): + return left != right + +def lt(left, right): + return left < right + +def le(left, right): + return left <= right + +def gt(left, right): + return left > right + +def ge(left, right): + return left >= right + +func_map = { + "==": eq, + "!=": ne, + "< ": lt, + "<=": le, + "> ": gt, + ">=": ge, +} + +compare = [2.0, nan, -nan, inf, -inf] +values = [nan, -nan, inf, -inf] + +for i in range(len(values)): + for j in range(len(compare)): + for op in func_map: + print("%g %s %g -> %s" % + (values[i], op, compare[j], func_map[op](values[i], compare[j]))) + + print("") @@ -4293,11 +4293,11 @@ format_nan_inf(NODE *n, char format) return NULL; else if (is_mpg_float(n)) { if (mpfr_nan_p(n->mpg_numbr)) { - strcpy(buf, mpfr_sgn(n->mpg_numbr) < 0 ? "-nan" : "+nan"); + strcpy(buf, mpfr_signbit(n->mpg_numbr) ? "-nan" : "+nan"); goto fmt; } else if (mpfr_inf_p(n->mpg_numbr)) { - strcpy(buf, mpfr_sgn(n->mpg_numbr) < 0 ? "-inf" : "+inf"); + strcpy(buf, mpfr_signbit(n->mpg_numbr) ? "-inf" : "+inf"); goto fmt; } else diff --git a/doc/ChangeLog b/doc/ChangeLog index 3e58a174..796051a5 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -9,12 +9,39 @@ * gawkworkflow.texi: Add an additional web resource. * gawktexi.in: More edits in sample programs chapter. +2020-11-20 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Strange values): Correct the description of what + happens with infinity. Thanks to Antonio Columbo for pointing + out the problem. + 2020-11-16 Arnold D. Robbins <arnold@skeeve.com> * gawktexi.in (Nextfile Statement): Clarify what happens in a BEGINFILE rule. * gawktexi.in: Additional small fixes. +2020-11-15 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Strange values): Add test programs inside + @ignore; extracted to example directory. + +2020-11-09 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in: Samll improvement in strange numbers section. + +2020-11-04 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Strange values): New section on NaN and infinity. + Update some other bits to point to it. + * wordlist: Updated with more words. + +2020-11-16 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Nextfile Statement): Clarify what happens in + a BEGINFILE rule. +>>>>>>> master + 2020-10-31 Arnold D. Robbins <arnold@skeeve.com> * texinfo.tex: Updated from GNULIB. diff --git a/doc/gawk.info b/doc/gawk.info index 985e4c6a..22a15a62 100644 --- a/doc/gawk.info +++ b/doc/gawk.info @@ -547,6 +547,7 @@ in (a) below. A copy of the license is included in the section entitled * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. * Getting Accuracy:: Getting more accuracy takes some work. * Try To Round:: Add digits and round. * Setting precision:: How to set the precision. @@ -6927,8 +6928,8 @@ width. Here is a list of the format-control letters: On systems supporting IEEE 754 floating-point format, values representing negative infinity are formatted as '-inf' or '-infinity', and positive infinity as 'inf' or 'infinity'. The - special "not a number" value formats as '-nan' or 'nan' (*note Math - Definitions::). + special "not a number" value formats as '-nan' or 'nan' (*note + Strange values::). '%F' Like '%f', but the infinity and "not a number" values are spelled @@ -12820,7 +12821,7 @@ brackets ([ ]): 'log(X)' Return the natural logarithm of X, if X is positive; otherwise, - return 'NaN' ("not a number") on IEEE 754 systems. Additionally, + return NaN ("not a number") on IEEE 754 systems. Additionally, 'gawk' prints a warning message when 'x' is negative. 'rand()' @@ -24203,18 +24204,10 @@ material here: another number and infinity produce infinity. "NaN" - "Not a number."(1) A special value that results from attempting a - calculation that has no answer as a real number. In such a case, - programs can either receive a floating-point exception, or get - 'NaN' back as the result. The IEEE 754 standard recommends that - systems return 'NaN'. Some examples: - - 'sqrt(-1)' - This makes sense in the range of complex numbers, but not in - the range of real numbers, so the result is 'NaN'. - - 'log(-8)' - -8 is out of the domain of 'log()', so the result is 'NaN'. + "Not a number." A special value that results from attempting a + calculation that has no answer as a real number. *Note Strange + values::, for more information about infinity and not-a-number + values. "Normalized" How the significand (see later in this list) is usually stored. @@ -24273,11 +24266,6 @@ Table 16.3: Basic IEEE format values NOTE: The precision numbers include the implied leading one that gives them one extra bit of significand. - ---------- Footnotes ---------- - - (1) Thanks to Michael Brennan for this description, which we have -paraphrased, and for the examples. - File: gawk.info, Node: MPFR features, Next: FP Math Caution, Prev: Math Definitions, Up: Arbitrary Precision Arithmetic @@ -24370,6 +24358,7 @@ be sure of the number of significant decimal places in the final result. * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. File: gawk.info, Node: Inexact representation, Next: Comparing FP Values, Up: Inexactness of computations @@ -24434,7 +24423,7 @@ values with a delta, you should be sure to use 'difference < abs(delta)' in case someone passes in a negative delta value. -File: gawk.info, Node: Errors accumulate, Prev: Comparing FP Values, Up: Inexactness of computations +File: gawk.info, Node: Errors accumulate, Next: Strange values, Prev: Comparing FP Values, Up: Inexactness of computations 16.4.1.3 Errors Accumulate .......................... @@ -24482,6 +24471,64 @@ representations yield an unexpected result: -| 4 +File: gawk.info, Node: Strange values, Prev: Errors accumulate, Up: Inexactness of computations + +16.4.1.4 Floating Point Values They Didn't Talk About In School +............................................................... + +Both IEEE 754 floating-point hardware, and MPFR, support two kinds of +values that you probably didn't learn about in school. The first is +"infinity", a special value, that can be either negative or positive, +and which is either smaller than any other value (negative infinity), or +larger than any other value (positive infinity). When such values are +generated, 'gawk' prints them as either '-inf' or '+inf', respectively. +It accepts those strings as data input and converts them to the proper +floating-point values internally. + + Infinity values of the same sign compare as equal to each other. +Otherwise, operations (addition, subtraction, etc.) involving another +number and infinity produce mathematically reasonable results. + + The second kind of value is "not a number", or NaN for short.(1) +This is a special value that results from attempting a calculation that +has no answer as a real number. In such a case, programs can either +receive a floating-point exception, or get NaN back as the result. The +IEEE 754 standard recommends that systems return NaN. Some examples: + +'sqrt(-1)' + This makes sense in the range of complex numbers, but not in the + range of real numbers, so the result is NaN. + +'log(-8)' + -8 is out of the domain of 'log()', so the result is NaN. + + NaN values are strange. In particular, they cannot be compared with +other floating point values; any such comparison, except for "is not +equal to", returns false. NaN values are so much unequal to other +values that even comparing two identical NaN values with '!=' returns +true! + + NaN values can also be signed, although it depends upon the +implementation as to which sign you get for any operation that returns a +NaN. For example, on some systems, 'sqrt(-1)' returns a negative NaN. On +others, it returns a positive NaN. + + When such values are generated, 'gawk' prints them as either '-nan' +or '+nan', respectively. Here too, 'gawk' accepts those strings as data +input and converts them to the proper floating-point values internally. + + If you want to dive more deeply into this topic, you can find test +programs in C, 'awk' and Python in the directory +'awklib/eg/test-programs' in the 'gawk' distribution. These programs +enable comparison among programming languages as to how they handle NaN +and infinity values. + + ---------- Footnotes ---------- + + (1) Thanks to Michael Brennan for this description, which we have +paraphrased, and for the examples. + + File: gawk.info, Node: Getting Accuracy, Next: Try To Round, Prev: Inexactness of computations, Up: FP Math Caution 16.4.2 Getting the Accuracy You Need @@ -37984,603 +38031,604 @@ Index Tag Table: Node: Top1200 -Node: Foreword344559 -Node: Foreword449001 -Node: Preface50533 -Ref: Preface-Footnote-153392 -Ref: Preface-Footnote-253501 -Ref: Preface-Footnote-353735 -Node: History53877 -Node: Names56229 -Ref: Names-Footnote-157333 -Node: This Manual57480 -Ref: This Manual-Footnote-164119 -Node: Conventions64219 -Node: Manual History66588 -Ref: Manual History-Footnote-169585 -Ref: Manual History-Footnote-269626 -Node: How To Contribute69700 -Node: Acknowledgments70626 -Node: Getting Started75563 -Node: Running gawk78002 -Node: One-shot79192 -Node: Read Terminal80455 -Node: Long82448 -Node: Executable Scripts83961 -Ref: Executable Scripts-Footnote-186594 -Node: Comments86697 -Node: Quoting89181 -Node: DOS Quoting94707 -Node: Sample Data Files96763 -Node: Very Simple99358 -Node: Two Rules105460 -Node: More Complex107345 -Node: Statements/Lines109677 -Ref: Statements/Lines-Footnote-1114161 -Node: Other Features114426 -Node: When115362 -Ref: When-Footnote-1117116 -Node: Intro Summary117181 -Node: Invoking Gawk118065 -Node: Command Line119579 -Node: Options120377 -Ref: Options-Footnote-1138291 -Ref: Options-Footnote-2138522 -Node: Other Arguments138547 -Node: Naming Standard Input142558 -Node: Environment Variables143768 -Node: AWKPATH Variable144326 -Ref: AWKPATH Variable-Footnote-1147738 -Ref: AWKPATH Variable-Footnote-2147772 -Node: AWKLIBPATH Variable148143 -Ref: AWKLIBPATH Variable-Footnote-1149840 -Node: Other Environment Variables150215 -Node: Exit Status154036 -Node: Include Files154713 -Node: Loading Shared Libraries158403 -Node: Obsolete159831 -Node: Undocumented160523 -Node: Invoking Summary160820 -Node: Regexp163661 -Node: Regexp Usage165115 -Node: Escape Sequences167152 -Node: Regexp Operators173393 -Node: Regexp Operator Details173878 -Ref: Regexp Operator Details-Footnote-1180310 -Node: Interval Expressions180457 -Ref: Interval Expressions-Footnote-1181878 -Node: Bracket Expressions181976 -Ref: table-char-classes184452 -Node: Leftmost Longest187778 -Node: Computed Regexps189081 -Node: GNU Regexp Operators192508 -Node: Case-sensitivity196245 -Ref: Case-sensitivity-Footnote-1199111 -Ref: Case-sensitivity-Footnote-2199346 -Node: Regexp Summary199454 -Node: Reading Files200920 -Node: Records203189 -Node: awk split records204264 -Node: gawk split records209539 -Ref: gawk split records-Footnote-1214272 -Node: Fields214309 -Node: Nonconstant Fields217050 -Ref: Nonconstant Fields-Footnote-1219286 -Node: Changing Fields219490 -Node: Field Separators225521 -Node: Default Field Splitting228219 -Node: Regexp Field Splitting229337 -Node: Single Character Fields232690 -Node: Command Line Field Separator233750 -Node: Full Line Fields236968 -Ref: Full Line Fields-Footnote-1238490 -Ref: Full Line Fields-Footnote-2238536 -Node: Field Splitting Summary238637 -Node: Constant Size240711 -Node: Fixed width data241443 -Node: Skipping intervening244910 -Node: Allowing trailing data245708 -Node: Fields with fixed data246745 -Node: Splitting By Content248263 -Ref: Splitting By Content-Footnote-1252046 -Node: More CSV252209 -Node: Testing field creation253519 -Node: Multiple Line255144 -Node: Getline261421 -Node: Plain Getline263890 -Node: Getline/Variable266463 -Node: Getline/File267614 -Node: Getline/Variable/File269002 -Ref: Getline/Variable/File-Footnote-1270607 -Node: Getline/Pipe270695 -Node: Getline/Variable/Pipe273399 -Node: Getline/Coprocess274534 -Node: Getline/Variable/Coprocess275801 -Node: Getline Notes276543 -Node: Getline Summary279340 -Ref: table-getline-variants279764 -Node: Read Timeout280512 -Ref: Read Timeout-Footnote-1284418 -Node: Retrying Input284476 -Node: Command-line directories285675 -Node: Input Summary286581 -Node: Input Exercises289753 -Node: Printing290187 -Node: Print292021 -Node: Print Examples293478 -Node: Output Separators296258 -Node: OFMT298275 -Node: Printf299631 -Node: Basic Printf300416 -Node: Control Letters301990 -Node: Format Modifiers307154 -Node: Printf Examples313169 -Node: Redirection315655 -Node: Special FD322496 -Ref: Special FD-Footnote-1325664 -Node: Special Files325738 -Node: Other Inherited Files326355 -Node: Special Network327356 -Node: Special Caveats328216 -Node: Close Files And Pipes329165 -Ref: table-close-pipe-return-values336072 -Ref: Close Files And Pipes-Footnote-1336885 -Ref: Close Files And Pipes-Footnote-2337033 -Node: Nonfatal337185 -Node: Output Summary339523 -Node: Output Exercises340745 -Node: Expressions341424 -Node: Values342612 -Node: Constants343290 -Node: Scalar Constants343981 -Ref: Scalar Constants-Footnote-1346491 -Node: Nondecimal-numbers346741 -Node: Regexp Constants349742 -Node: Using Constant Regexps350268 -Node: Standard Regexp Constants350890 -Node: Strong Regexp Constants354078 -Node: Variables357090 -Node: Using Variables357747 -Node: Assignment Options359657 -Node: Conversion362128 -Node: Strings And Numbers362652 -Ref: Strings And Numbers-Footnote-1365715 -Node: Locale influences conversions365824 -Ref: table-locale-affects368582 -Node: All Operators369200 -Node: Arithmetic Ops369829 -Node: Concatenation372545 -Ref: Concatenation-Footnote-1375392 -Node: Assignment Ops375499 -Ref: table-assign-ops380490 -Node: Increment Ops381803 -Node: Truth Values and Conditions385263 -Node: Truth Values386337 -Node: Typing and Comparison387385 -Node: Variable Typing388205 -Ref: Variable Typing-Footnote-1394668 -Ref: Variable Typing-Footnote-2394740 -Node: Comparison Operators394817 -Ref: table-relational-ops395236 -Node: POSIX String Comparison398731 -Ref: POSIX String Comparison-Footnote-1400426 -Ref: POSIX String Comparison-Footnote-2400565 -Node: Boolean Ops400649 -Ref: Boolean Ops-Footnote-1405131 -Node: Conditional Exp405223 -Node: Function Calls406959 -Node: Precedence410836 -Node: Locales414495 -Node: Expressions Summary416127 -Node: Patterns and Actions418700 -Node: Pattern Overview419820 -Node: Regexp Patterns421497 -Node: Expression Patterns422039 -Node: Ranges425820 -Node: BEGIN/END428928 -Node: Using BEGIN/END429689 -Ref: Using BEGIN/END-Footnote-1432443 -Node: I/O And BEGIN/END432549 -Node: BEGINFILE/ENDFILE434862 -Node: Empty438093 -Node: Using Shell Variables438410 -Node: Action Overview440684 -Node: Statements443009 -Node: If Statement444857 -Node: While Statement446352 -Node: Do Statement448380 -Node: For Statement449528 -Node: Switch Statement452699 -Node: Break Statement455140 -Node: Continue Statement457232 -Node: Next Statement459059 -Node: Nextfile Statement461442 -Node: Exit Statement464131 -Node: Built-in Variables466534 -Node: User-modified467667 -Node: Auto-set475434 -Ref: Auto-set-Footnote-1492241 -Ref: Auto-set-Footnote-2492447 -Node: ARGC and ARGV492503 -Node: Pattern Action Summary496716 -Node: Arrays499146 -Node: Array Basics500475 -Node: Array Intro501319 -Ref: figure-array-elements503294 -Ref: Array Intro-Footnote-1505998 -Node: Reference to Elements506126 -Node: Assigning Elements508590 -Node: Array Example509081 -Node: Scanning an Array510840 -Node: Controlling Scanning513862 -Ref: Controlling Scanning-Footnote-1520318 -Node: Numeric Array Subscripts520634 -Node: Uninitialized Subscripts522818 -Node: Delete524437 -Ref: Delete-Footnote-1527189 -Node: Multidimensional527246 -Node: Multiscanning530341 -Node: Arrays of Arrays531932 -Node: Arrays Summary536700 -Node: Functions538793 -Node: Built-in539831 -Node: Calling Built-in540912 -Node: Numeric Functions542908 -Ref: Numeric Functions-Footnote-1546936 -Ref: Numeric Functions-Footnote-2547584 -Ref: Numeric Functions-Footnote-3547632 -Node: String Functions547904 -Ref: String Functions-Footnote-1572045 -Ref: String Functions-Footnote-2572173 -Ref: String Functions-Footnote-3572421 -Node: Gory Details572508 -Ref: table-sub-escapes574299 -Ref: table-sub-proposed575818 -Ref: table-posix-sub577181 -Ref: table-gensub-escapes578722 -Ref: Gory Details-Footnote-1579545 -Node: I/O Functions579699 -Ref: table-system-return-values586153 -Ref: I/O Functions-Footnote-1588233 -Ref: I/O Functions-Footnote-2588381 -Node: Time Functions588501 -Ref: Time Functions-Footnote-1599172 -Ref: Time Functions-Footnote-2599240 -Ref: Time Functions-Footnote-3599398 -Ref: Time Functions-Footnote-4599509 -Ref: Time Functions-Footnote-5599621 -Ref: Time Functions-Footnote-6599848 -Node: Bitwise Functions600114 -Ref: table-bitwise-ops600708 -Ref: Bitwise Functions-Footnote-1606771 -Ref: Bitwise Functions-Footnote-2606944 -Node: Type Functions607135 -Node: I18N Functions609998 -Node: User-defined611649 -Node: Definition Syntax612461 -Ref: Definition Syntax-Footnote-1618155 -Node: Function Example618226 -Ref: Function Example-Footnote-1621148 -Node: Function Calling621170 -Node: Calling A Function621758 -Node: Variable Scope622716 -Node: Pass By Value/Reference625710 -Node: Function Caveats628354 -Ref: Function Caveats-Footnote-1630401 -Node: Return Statement630521 -Node: Dynamic Typing633500 -Node: Indirect Calls634430 -Ref: Indirect Calls-Footnote-1644682 -Node: Functions Summary644810 -Node: Library Functions647515 -Ref: Library Functions-Footnote-1651122 -Ref: Library Functions-Footnote-2651265 -Node: Library Names651436 -Ref: Library Names-Footnote-1655103 -Ref: Library Names-Footnote-2655326 -Node: General Functions655412 -Node: Strtonum Function656515 -Node: Assert Function659537 -Node: Round Function662863 -Node: Cliff Random Function664403 -Node: Ordinal Functions665419 -Ref: Ordinal Functions-Footnote-1668482 -Ref: Ordinal Functions-Footnote-2668734 -Node: Join Function668944 -Ref: Join Function-Footnote-1670714 -Node: Getlocaltime Function670914 -Node: Readfile Function674656 -Node: Shell Quoting676633 -Node: Data File Management678034 -Node: Filetrans Function678666 -Node: Rewind Function682762 -Node: File Checking684671 -Ref: File Checking-Footnote-1686005 -Node: Empty Files686206 -Node: Ignoring Assigns688185 -Node: Getopt Function689735 -Ref: Getopt Function-Footnote-1704946 -Node: Passwd Functions705146 -Ref: Passwd Functions-Footnote-1713985 -Node: Group Functions714073 -Ref: Group Functions-Footnote-1721971 -Node: Walking Arrays722178 -Node: Library Functions Summary725186 -Node: Library Exercises726592 -Node: Sample Programs727057 -Node: Running Examples727827 -Node: Clones728555 -Node: Cut Program729779 -Node: Egrep Program739919 -Node: Id Program748930 -Node: Split Program758877 -Ref: Split Program-Footnote-1768767 -Node: Tee Program768940 -Node: Uniq Program771730 -Node: Wc Program779318 -Node: Bytes vs. Characters779715 -Node: Using extensions781263 -Node: wc program782017 -Node: Miscellaneous Programs786882 -Node: Dupword Program788095 -Node: Alarm Program790125 -Node: Translate Program794980 -Ref: Translate Program-Footnote-1799545 -Node: Labels Program799815 -Ref: Labels Program-Footnote-1803166 -Node: Word Sorting803250 -Node: History Sorting807322 -Node: Extract Program809547 -Node: Simple Sed817601 -Node: Igawk Program820675 -Ref: Igawk Program-Footnote-1835006 -Ref: Igawk Program-Footnote-2835208 -Ref: Igawk Program-Footnote-3835330 -Node: Anagram Program835445 -Node: Signature Program838507 -Node: Programs Summary839754 -Node: Programs Exercises840968 -Ref: Programs Exercises-Footnote-1845098 -Node: Advanced Features845184 -Node: Nondecimal Data847251 -Node: Array Sorting848842 -Node: Controlling Array Traversal849542 -Ref: Controlling Array Traversal-Footnote-1857910 -Node: Array Sorting Functions858028 -Ref: Array Sorting Functions-Footnote-1863119 -Node: Two-way I/O863315 -Ref: Two-way I/O-Footnote-1871036 -Ref: Two-way I/O-Footnote-2871223 -Node: TCP/IP Networking871305 -Node: Profiling874423 -Node: Extension Philosophy883732 -Node: Advanced Features Summary885183 -Node: Internationalization887182 -Node: I18N and L10N888662 -Node: Explaining gettext889349 -Ref: Explaining gettext-Footnote-1895241 -Ref: Explaining gettext-Footnote-2895426 -Node: Programmer i18n895591 -Ref: Programmer i18n-Footnote-1900540 -Node: Translator i18n900589 -Node: String Extraction901383 -Ref: String Extraction-Footnote-1902515 -Node: Printf Ordering902601 -Ref: Printf Ordering-Footnote-1905387 -Node: I18N Portability905451 -Ref: I18N Portability-Footnote-1907907 -Node: I18N Example907970 -Ref: I18N Example-Footnote-1911245 -Ref: I18N Example-Footnote-2911318 -Node: Gawk I18N911427 -Node: I18N Summary912076 -Node: Debugger913417 -Node: Debugging914417 -Node: Debugging Concepts914858 -Node: Debugging Terms916667 -Node: Awk Debugging919242 -Ref: Awk Debugging-Footnote-1920187 -Node: Sample Debugging Session920319 -Node: Debugger Invocation920853 -Node: Finding The Bug922239 -Node: List of Debugger Commands928713 -Node: Breakpoint Control930046 -Node: Debugger Execution Control933740 -Node: Viewing And Changing Data937102 -Node: Execution Stack940643 -Node: Debugger Info942280 -Node: Miscellaneous Debugger Commands946351 -Node: Readline Support951413 -Node: Limitations952309 -Node: Debugging Summary954863 -Node: Namespaces956142 -Node: Global Namespace957253 -Node: Qualified Names958651 -Node: Default Namespace959650 -Node: Changing The Namespace960391 -Node: Naming Rules962005 -Node: Internal Name Management963853 -Node: Namespace Example964895 -Node: Namespace And Features967457 -Node: Namespace Summary968892 -Node: Arbitrary Precision Arithmetic970369 -Node: Computer Arithmetic971856 -Ref: table-numeric-ranges975622 -Ref: table-floating-point-ranges976115 -Ref: Computer Arithmetic-Footnote-1976773 -Node: Math Definitions976830 -Ref: table-ieee-formats980146 -Ref: Math Definitions-Footnote-1980749 -Node: MPFR features980854 -Node: FP Math Caution982572 -Ref: FP Math Caution-Footnote-1983644 -Node: Inexactness of computations984013 -Node: Inexact representation984973 -Node: Comparing FP Values986333 -Node: Errors accumulate987574 -Node: Getting Accuracy989007 -Node: Try To Round991717 -Node: Setting precision992616 -Ref: table-predefined-precision-strings993313 -Node: Setting the rounding mode995143 -Ref: table-gawk-rounding-modes995517 -Ref: Setting the rounding mode-Footnote-1999448 -Node: Arbitrary Precision Integers999627 -Ref: Arbitrary Precision Integers-Footnote-11002802 -Node: Checking for MPFR1002951 -Node: POSIX Floating Point Problems1004425 -Ref: POSIX Floating Point Problems-Footnote-11008710 -Node: Floating point summary1008748 -Node: Dynamic Extensions1010938 -Node: Extension Intro1012491 -Node: Plugin License1013757 -Node: Extension Mechanism Outline1014554 -Ref: figure-load-extension1014993 -Ref: figure-register-new-function1016558 -Ref: figure-call-new-function1017650 -Node: Extension API Description1019712 -Node: Extension API Functions Introduction1021425 -Ref: table-api-std-headers1023261 -Node: General Data Types1027510 -Ref: General Data Types-Footnote-11036140 -Node: Memory Allocation Functions1036439 -Ref: Memory Allocation Functions-Footnote-11040940 -Node: Constructor Functions1041039 -Node: API Ownership of MPFR and GMP Values1044505 -Node: Registration Functions1045818 -Node: Extension Functions1046518 -Node: Exit Callback Functions1051840 -Node: Extension Version String1053090 -Node: Input Parsers1053753 -Node: Output Wrappers1066474 -Node: Two-way processors1070986 -Node: Printing Messages1073251 -Ref: Printing Messages-Footnote-11074422 -Node: Updating ERRNO1074575 -Node: Requesting Values1075314 -Ref: table-value-types-returned1076051 -Node: Accessing Parameters1076987 -Node: Symbol Table Access1078224 -Node: Symbol table by name1078736 -Ref: Symbol table by name-Footnote-11081760 -Node: Symbol table by cookie1081888 -Ref: Symbol table by cookie-Footnote-11086073 -Node: Cached values1086137 -Ref: Cached values-Footnote-11089673 -Node: Array Manipulation1089826 -Ref: Array Manipulation-Footnote-11090917 -Node: Array Data Types1090954 -Ref: Array Data Types-Footnote-11093612 -Node: Array Functions1093704 -Node: Flattening Arrays1098202 -Node: Creating Arrays1105178 -Node: Redirection API1109945 -Node: Extension API Variables1112778 -Node: Extension Versioning1113489 -Ref: gawk-api-version1113918 -Node: Extension GMP/MPFR Versioning1115649 -Node: Extension API Informational Variables1117277 -Node: Extension API Boilerplate1118350 -Node: Changes from API V11122324 -Node: Finding Extensions1123896 -Node: Extension Example1124455 -Node: Internal File Description1125253 -Node: Internal File Ops1129333 -Ref: Internal File Ops-Footnote-11140683 -Node: Using Internal File Ops1140823 -Ref: Using Internal File Ops-Footnote-11143206 -Node: Extension Samples1143480 -Node: Extension Sample File Functions1145009 -Node: Extension Sample Fnmatch1152658 -Node: Extension Sample Fork1154145 -Node: Extension Sample Inplace1155363 -Node: Extension Sample Ord1158989 -Node: Extension Sample Readdir1159825 -Ref: table-readdir-file-types1160714 -Node: Extension Sample Revout1161781 -Node: Extension Sample Rev2way1162370 -Node: Extension Sample Read write array1163110 -Node: Extension Sample Readfile1165052 -Node: Extension Sample Time1166147 -Node: Extension Sample API Tests1167899 -Node: gawkextlib1168391 -Node: Extension summary1171309 -Node: Extension Exercises1175011 -Node: Language History1176253 -Node: V7/SVR3.11177909 -Node: SVR41180061 -Node: POSIX1181495 -Node: BTL1182876 -Node: POSIX/GNU1183605 -Node: Feature History1189383 -Node: Common Extensions1205702 -Node: Ranges and Locales1206985 -Ref: Ranges and Locales-Footnote-11211601 -Ref: Ranges and Locales-Footnote-21211628 -Ref: Ranges and Locales-Footnote-31211863 -Node: Contributors1212086 -Node: History summary1218083 -Node: Installation1219463 -Node: Gawk Distribution1220407 -Node: Getting1220891 -Node: Extracting1221854 -Node: Distribution contents1223492 -Node: Unix Installation1229972 -Node: Quick Installation1230654 -Node: Shell Startup Files1233068 -Node: Additional Configuration Options1234157 -Node: Configuration Philosophy1236472 -Node: Non-Unix Installation1238841 -Node: PC Installation1239301 -Node: PC Binary Installation1240139 -Node: PC Compiling1240574 -Node: PC Using1241691 -Node: Cygwin1245244 -Node: MSYS1246468 -Node: VMS Installation1247070 -Node: VMS Compilation1247861 -Ref: VMS Compilation-Footnote-11249090 -Node: VMS Dynamic Extensions1249148 -Node: VMS Installation Details1250833 -Node: VMS Running1253086 -Node: VMS GNV1257365 -Node: VMS Old Gawk1258100 -Node: Bugs1258571 -Node: Bug address1259234 -Node: Usenet1262216 -Node: Maintainers1263220 -Node: Other Versions1264405 -Node: Installation summary1271493 -Node: Notes1272702 -Node: Compatibility Mode1273496 -Node: Additions1274278 -Node: Accessing The Source1275203 -Node: Adding Code1276640 -Node: New Ports1282859 -Node: Derived Files1287234 -Ref: Derived Files-Footnote-11292894 -Ref: Derived Files-Footnote-21292929 -Ref: Derived Files-Footnote-31293527 -Node: Future Extensions1293641 -Node: Implementation Limitations1294299 -Node: Extension Design1295509 -Node: Old Extension Problems1296653 -Ref: Old Extension Problems-Footnote-11298171 -Node: Extension New Mechanism Goals1298228 -Ref: Extension New Mechanism Goals-Footnote-11301592 -Node: Extension Other Design Decisions1301781 -Node: Extension Future Growth1303894 -Node: Notes summary1304500 -Node: Basic Concepts1305658 -Node: Basic High Level1306339 -Ref: figure-general-flow1306621 -Ref: figure-process-flow1307306 -Ref: Basic High Level-Footnote-11310607 -Node: Basic Data Typing1310792 -Node: Glossary1314120 -Node: Copying1346005 -Node: GNU Free Documentation License1383548 -Node: Index1408668 +Node: Foreword344638 +Node: Foreword449080 +Node: Preface50612 +Ref: Preface-Footnote-153471 +Ref: Preface-Footnote-253580 +Ref: Preface-Footnote-353814 +Node: History53956 +Node: Names56308 +Ref: Names-Footnote-157412 +Node: This Manual57559 +Ref: This Manual-Footnote-164198 +Node: Conventions64298 +Node: Manual History66667 +Ref: Manual History-Footnote-169664 +Ref: Manual History-Footnote-269705 +Node: How To Contribute69779 +Node: Acknowledgments70705 +Node: Getting Started75642 +Node: Running gawk78081 +Node: One-shot79271 +Node: Read Terminal80534 +Node: Long82527 +Node: Executable Scripts84040 +Ref: Executable Scripts-Footnote-186673 +Node: Comments86776 +Node: Quoting89260 +Node: DOS Quoting94786 +Node: Sample Data Files96842 +Node: Very Simple99437 +Node: Two Rules105539 +Node: More Complex107424 +Node: Statements/Lines109756 +Ref: Statements/Lines-Footnote-1114240 +Node: Other Features114505 +Node: When115441 +Ref: When-Footnote-1117195 +Node: Intro Summary117260 +Node: Invoking Gawk118144 +Node: Command Line119658 +Node: Options120456 +Ref: Options-Footnote-1138370 +Ref: Options-Footnote-2138601 +Node: Other Arguments138626 +Node: Naming Standard Input142637 +Node: Environment Variables143847 +Node: AWKPATH Variable144405 +Ref: AWKPATH Variable-Footnote-1147817 +Ref: AWKPATH Variable-Footnote-2147851 +Node: AWKLIBPATH Variable148222 +Ref: AWKLIBPATH Variable-Footnote-1149919 +Node: Other Environment Variables150294 +Node: Exit Status154115 +Node: Include Files154792 +Node: Loading Shared Libraries158482 +Node: Obsolete159910 +Node: Undocumented160602 +Node: Invoking Summary160899 +Node: Regexp163740 +Node: Regexp Usage165194 +Node: Escape Sequences167231 +Node: Regexp Operators173472 +Node: Regexp Operator Details173957 +Ref: Regexp Operator Details-Footnote-1180389 +Node: Interval Expressions180536 +Ref: Interval Expressions-Footnote-1181957 +Node: Bracket Expressions182055 +Ref: table-char-classes184531 +Node: Leftmost Longest187857 +Node: Computed Regexps189160 +Node: GNU Regexp Operators192587 +Node: Case-sensitivity196324 +Ref: Case-sensitivity-Footnote-1199190 +Ref: Case-sensitivity-Footnote-2199425 +Node: Regexp Summary199533 +Node: Reading Files200999 +Node: Records203268 +Node: awk split records204343 +Node: gawk split records209618 +Ref: gawk split records-Footnote-1214351 +Node: Fields214388 +Node: Nonconstant Fields217129 +Ref: Nonconstant Fields-Footnote-1219365 +Node: Changing Fields219569 +Node: Field Separators225600 +Node: Default Field Splitting228298 +Node: Regexp Field Splitting229416 +Node: Single Character Fields232769 +Node: Command Line Field Separator233829 +Node: Full Line Fields237047 +Ref: Full Line Fields-Footnote-1238569 +Ref: Full Line Fields-Footnote-2238615 +Node: Field Splitting Summary238716 +Node: Constant Size240790 +Node: Fixed width data241522 +Node: Skipping intervening244989 +Node: Allowing trailing data245787 +Node: Fields with fixed data246824 +Node: Splitting By Content248342 +Ref: Splitting By Content-Footnote-1252125 +Node: More CSV252288 +Node: Testing field creation253598 +Node: Multiple Line255223 +Node: Getline261500 +Node: Plain Getline263969 +Node: Getline/Variable266542 +Node: Getline/File267693 +Node: Getline/Variable/File269081 +Ref: Getline/Variable/File-Footnote-1270686 +Node: Getline/Pipe270774 +Node: Getline/Variable/Pipe273478 +Node: Getline/Coprocess274613 +Node: Getline/Variable/Coprocess275880 +Node: Getline Notes276622 +Node: Getline Summary279419 +Ref: table-getline-variants279843 +Node: Read Timeout280591 +Ref: Read Timeout-Footnote-1284497 +Node: Retrying Input284555 +Node: Command-line directories285754 +Node: Input Summary286660 +Node: Input Exercises289832 +Node: Printing290266 +Node: Print292100 +Node: Print Examples293557 +Node: Output Separators296337 +Node: OFMT298354 +Node: Printf299710 +Node: Basic Printf300495 +Node: Control Letters302069 +Node: Format Modifiers307231 +Node: Printf Examples313246 +Node: Redirection315732 +Node: Special FD322573 +Ref: Special FD-Footnote-1325741 +Node: Special Files325815 +Node: Other Inherited Files326432 +Node: Special Network327433 +Node: Special Caveats328293 +Node: Close Files And Pipes329242 +Ref: table-close-pipe-return-values336149 +Ref: Close Files And Pipes-Footnote-1336962 +Ref: Close Files And Pipes-Footnote-2337110 +Node: Nonfatal337262 +Node: Output Summary339600 +Node: Output Exercises340822 +Node: Expressions341501 +Node: Values342689 +Node: Constants343367 +Node: Scalar Constants344058 +Ref: Scalar Constants-Footnote-1346568 +Node: Nondecimal-numbers346818 +Node: Regexp Constants349819 +Node: Using Constant Regexps350345 +Node: Standard Regexp Constants350967 +Node: Strong Regexp Constants354155 +Node: Variables357167 +Node: Using Variables357824 +Node: Assignment Options359734 +Node: Conversion362205 +Node: Strings And Numbers362729 +Ref: Strings And Numbers-Footnote-1365792 +Node: Locale influences conversions365901 +Ref: table-locale-affects368659 +Node: All Operators369277 +Node: Arithmetic Ops369906 +Node: Concatenation372622 +Ref: Concatenation-Footnote-1375469 +Node: Assignment Ops375576 +Ref: table-assign-ops380567 +Node: Increment Ops381880 +Node: Truth Values and Conditions385340 +Node: Truth Values386414 +Node: Typing and Comparison387462 +Node: Variable Typing388282 +Ref: Variable Typing-Footnote-1394745 +Ref: Variable Typing-Footnote-2394817 +Node: Comparison Operators394894 +Ref: table-relational-ops395313 +Node: POSIX String Comparison398808 +Ref: POSIX String Comparison-Footnote-1400503 +Ref: POSIX String Comparison-Footnote-2400642 +Node: Boolean Ops400726 +Ref: Boolean Ops-Footnote-1405208 +Node: Conditional Exp405300 +Node: Function Calls407036 +Node: Precedence410913 +Node: Locales414572 +Node: Expressions Summary416204 +Node: Patterns and Actions418777 +Node: Pattern Overview419897 +Node: Regexp Patterns421574 +Node: Expression Patterns422116 +Node: Ranges425897 +Node: BEGIN/END429005 +Node: Using BEGIN/END429766 +Ref: Using BEGIN/END-Footnote-1432520 +Node: I/O And BEGIN/END432626 +Node: BEGINFILE/ENDFILE434939 +Node: Empty438170 +Node: Using Shell Variables438487 +Node: Action Overview440761 +Node: Statements443086 +Node: If Statement444934 +Node: While Statement446429 +Node: Do Statement448457 +Node: For Statement449605 +Node: Switch Statement452776 +Node: Break Statement455217 +Node: Continue Statement457309 +Node: Next Statement459136 +Node: Nextfile Statement461519 +Node: Exit Statement464208 +Node: Built-in Variables466611 +Node: User-modified467744 +Node: Auto-set475511 +Ref: Auto-set-Footnote-1492318 +Ref: Auto-set-Footnote-2492524 +Node: ARGC and ARGV492580 +Node: Pattern Action Summary496793 +Node: Arrays499223 +Node: Array Basics500552 +Node: Array Intro501396 +Ref: figure-array-elements503371 +Ref: Array Intro-Footnote-1506075 +Node: Reference to Elements506203 +Node: Assigning Elements508667 +Node: Array Example509158 +Node: Scanning an Array510917 +Node: Controlling Scanning513939 +Ref: Controlling Scanning-Footnote-1520395 +Node: Numeric Array Subscripts520711 +Node: Uninitialized Subscripts522895 +Node: Delete524514 +Ref: Delete-Footnote-1527266 +Node: Multidimensional527323 +Node: Multiscanning530418 +Node: Arrays of Arrays532009 +Node: Arrays Summary536777 +Node: Functions538870 +Node: Built-in539908 +Node: Calling Built-in540989 +Node: Numeric Functions542985 +Ref: Numeric Functions-Footnote-1547011 +Ref: Numeric Functions-Footnote-2547659 +Ref: Numeric Functions-Footnote-3547707 +Node: String Functions547979 +Ref: String Functions-Footnote-1572120 +Ref: String Functions-Footnote-2572248 +Ref: String Functions-Footnote-3572496 +Node: Gory Details572583 +Ref: table-sub-escapes574374 +Ref: table-sub-proposed575893 +Ref: table-posix-sub577256 +Ref: table-gensub-escapes578797 +Ref: Gory Details-Footnote-1579620 +Node: I/O Functions579774 +Ref: table-system-return-values586228 +Ref: I/O Functions-Footnote-1588308 +Ref: I/O Functions-Footnote-2588456 +Node: Time Functions588576 +Ref: Time Functions-Footnote-1599247 +Ref: Time Functions-Footnote-2599315 +Ref: Time Functions-Footnote-3599473 +Ref: Time Functions-Footnote-4599584 +Ref: Time Functions-Footnote-5599696 +Ref: Time Functions-Footnote-6599923 +Node: Bitwise Functions600189 +Ref: table-bitwise-ops600783 +Ref: Bitwise Functions-Footnote-1606846 +Ref: Bitwise Functions-Footnote-2607019 +Node: Type Functions607210 +Node: I18N Functions610073 +Node: User-defined611724 +Node: Definition Syntax612536 +Ref: Definition Syntax-Footnote-1618230 +Node: Function Example618301 +Ref: Function Example-Footnote-1621223 +Node: Function Calling621245 +Node: Calling A Function621833 +Node: Variable Scope622791 +Node: Pass By Value/Reference625785 +Node: Function Caveats628429 +Ref: Function Caveats-Footnote-1630476 +Node: Return Statement630596 +Node: Dynamic Typing633575 +Node: Indirect Calls634505 +Ref: Indirect Calls-Footnote-1644757 +Node: Functions Summary644885 +Node: Library Functions647590 +Ref: Library Functions-Footnote-1651197 +Ref: Library Functions-Footnote-2651340 +Node: Library Names651511 +Ref: Library Names-Footnote-1655178 +Ref: Library Names-Footnote-2655401 +Node: General Functions655487 +Node: Strtonum Function656590 +Node: Assert Function659612 +Node: Round Function662938 +Node: Cliff Random Function664478 +Node: Ordinal Functions665494 +Ref: Ordinal Functions-Footnote-1668557 +Ref: Ordinal Functions-Footnote-2668809 +Node: Join Function669019 +Ref: Join Function-Footnote-1670789 +Node: Getlocaltime Function670989 +Node: Readfile Function674731 +Node: Shell Quoting676708 +Node: Data File Management678109 +Node: Filetrans Function678741 +Node: Rewind Function682837 +Node: File Checking684746 +Ref: File Checking-Footnote-1686080 +Node: Empty Files686281 +Node: Ignoring Assigns688260 +Node: Getopt Function689810 +Ref: Getopt Function-Footnote-1705021 +Node: Passwd Functions705221 +Ref: Passwd Functions-Footnote-1714060 +Node: Group Functions714148 +Ref: Group Functions-Footnote-1722046 +Node: Walking Arrays722253 +Node: Library Functions Summary725261 +Node: Library Exercises726667 +Node: Sample Programs727132 +Node: Running Examples727902 +Node: Clones728630 +Node: Cut Program729854 +Node: Egrep Program739994 +Node: Id Program749005 +Node: Split Program758952 +Ref: Split Program-Footnote-1768842 +Node: Tee Program769015 +Node: Uniq Program771805 +Node: Wc Program779393 +Node: Bytes vs. Characters779790 +Node: Using extensions781338 +Node: wc program782092 +Node: Miscellaneous Programs786957 +Node: Dupword Program788170 +Node: Alarm Program790200 +Node: Translate Program795055 +Ref: Translate Program-Footnote-1799620 +Node: Labels Program799890 +Ref: Labels Program-Footnote-1803241 +Node: Word Sorting803325 +Node: History Sorting807397 +Node: Extract Program809622 +Node: Simple Sed817676 +Node: Igawk Program820750 +Ref: Igawk Program-Footnote-1835081 +Ref: Igawk Program-Footnote-2835283 +Ref: Igawk Program-Footnote-3835405 +Node: Anagram Program835520 +Node: Signature Program838582 +Node: Programs Summary839829 +Node: Programs Exercises841043 +Ref: Programs Exercises-Footnote-1845173 +Node: Advanced Features845259 +Node: Nondecimal Data847326 +Node: Array Sorting848917 +Node: Controlling Array Traversal849617 +Ref: Controlling Array Traversal-Footnote-1857985 +Node: Array Sorting Functions858103 +Ref: Array Sorting Functions-Footnote-1863194 +Node: Two-way I/O863390 +Ref: Two-way I/O-Footnote-1871111 +Ref: Two-way I/O-Footnote-2871298 +Node: TCP/IP Networking871380 +Node: Profiling874498 +Node: Extension Philosophy883807 +Node: Advanced Features Summary885258 +Node: Internationalization887257 +Node: I18N and L10N888737 +Node: Explaining gettext889424 +Ref: Explaining gettext-Footnote-1895316 +Ref: Explaining gettext-Footnote-2895501 +Node: Programmer i18n895666 +Ref: Programmer i18n-Footnote-1900615 +Node: Translator i18n900664 +Node: String Extraction901458 +Ref: String Extraction-Footnote-1902590 +Node: Printf Ordering902676 +Ref: Printf Ordering-Footnote-1905462 +Node: I18N Portability905526 +Ref: I18N Portability-Footnote-1907982 +Node: I18N Example908045 +Ref: I18N Example-Footnote-1911320 +Ref: I18N Example-Footnote-2911393 +Node: Gawk I18N911502 +Node: I18N Summary912151 +Node: Debugger913492 +Node: Debugging914492 +Node: Debugging Concepts914933 +Node: Debugging Terms916742 +Node: Awk Debugging919317 +Ref: Awk Debugging-Footnote-1920262 +Node: Sample Debugging Session920394 +Node: Debugger Invocation920928 +Node: Finding The Bug922314 +Node: List of Debugger Commands928788 +Node: Breakpoint Control930121 +Node: Debugger Execution Control933815 +Node: Viewing And Changing Data937177 +Node: Execution Stack940718 +Node: Debugger Info942355 +Node: Miscellaneous Debugger Commands946426 +Node: Readline Support951488 +Node: Limitations952384 +Node: Debugging Summary954938 +Node: Namespaces956217 +Node: Global Namespace957328 +Node: Qualified Names958726 +Node: Default Namespace959725 +Node: Changing The Namespace960466 +Node: Naming Rules962080 +Node: Internal Name Management963928 +Node: Namespace Example964970 +Node: Namespace And Features967532 +Node: Namespace Summary968967 +Node: Arbitrary Precision Arithmetic970444 +Node: Computer Arithmetic971931 +Ref: table-numeric-ranges975697 +Ref: table-floating-point-ranges976190 +Ref: Computer Arithmetic-Footnote-1976848 +Node: Math Definitions976905 +Ref: table-ieee-formats979881 +Node: MPFR features980448 +Node: FP Math Caution982166 +Ref: FP Math Caution-Footnote-1983238 +Node: Inexactness of computations983607 +Node: Inexact representation984638 +Node: Comparing FP Values985998 +Node: Errors accumulate987239 +Node: Strange values988695 +Ref: Strange values-Footnote-1991283 +Node: Getting Accuracy991388 +Node: Try To Round994098 +Node: Setting precision994997 +Ref: table-predefined-precision-strings995694 +Node: Setting the rounding mode997524 +Ref: table-gawk-rounding-modes997898 +Ref: Setting the rounding mode-Footnote-11001829 +Node: Arbitrary Precision Integers1002008 +Ref: Arbitrary Precision Integers-Footnote-11005183 +Node: Checking for MPFR1005332 +Node: POSIX Floating Point Problems1006806 +Ref: POSIX Floating Point Problems-Footnote-11011091 +Node: Floating point summary1011129 +Node: Dynamic Extensions1013319 +Node: Extension Intro1014872 +Node: Plugin License1016138 +Node: Extension Mechanism Outline1016935 +Ref: figure-load-extension1017374 +Ref: figure-register-new-function1018939 +Ref: figure-call-new-function1020031 +Node: Extension API Description1022093 +Node: Extension API Functions Introduction1023806 +Ref: table-api-std-headers1025642 +Node: General Data Types1029891 +Ref: General Data Types-Footnote-11038521 +Node: Memory Allocation Functions1038820 +Ref: Memory Allocation Functions-Footnote-11043321 +Node: Constructor Functions1043420 +Node: API Ownership of MPFR and GMP Values1046886 +Node: Registration Functions1048199 +Node: Extension Functions1048899 +Node: Exit Callback Functions1054221 +Node: Extension Version String1055471 +Node: Input Parsers1056134 +Node: Output Wrappers1068855 +Node: Two-way processors1073367 +Node: Printing Messages1075632 +Ref: Printing Messages-Footnote-11076803 +Node: Updating ERRNO1076956 +Node: Requesting Values1077695 +Ref: table-value-types-returned1078432 +Node: Accessing Parameters1079368 +Node: Symbol Table Access1080605 +Node: Symbol table by name1081117 +Ref: Symbol table by name-Footnote-11084141 +Node: Symbol table by cookie1084269 +Ref: Symbol table by cookie-Footnote-11088454 +Node: Cached values1088518 +Ref: Cached values-Footnote-11092054 +Node: Array Manipulation1092207 +Ref: Array Manipulation-Footnote-11093298 +Node: Array Data Types1093335 +Ref: Array Data Types-Footnote-11095993 +Node: Array Functions1096085 +Node: Flattening Arrays1100583 +Node: Creating Arrays1107559 +Node: Redirection API1112326 +Node: Extension API Variables1115159 +Node: Extension Versioning1115870 +Ref: gawk-api-version1116299 +Node: Extension GMP/MPFR Versioning1118030 +Node: Extension API Informational Variables1119658 +Node: Extension API Boilerplate1120731 +Node: Changes from API V11124705 +Node: Finding Extensions1126277 +Node: Extension Example1126836 +Node: Internal File Description1127634 +Node: Internal File Ops1131714 +Ref: Internal File Ops-Footnote-11143064 +Node: Using Internal File Ops1143204 +Ref: Using Internal File Ops-Footnote-11145587 +Node: Extension Samples1145861 +Node: Extension Sample File Functions1147390 +Node: Extension Sample Fnmatch1155039 +Node: Extension Sample Fork1156526 +Node: Extension Sample Inplace1157744 +Node: Extension Sample Ord1161370 +Node: Extension Sample Readdir1162206 +Ref: table-readdir-file-types1163095 +Node: Extension Sample Revout1164162 +Node: Extension Sample Rev2way1164751 +Node: Extension Sample Read write array1165491 +Node: Extension Sample Readfile1167433 +Node: Extension Sample Time1168528 +Node: Extension Sample API Tests1170280 +Node: gawkextlib1170772 +Node: Extension summary1173690 +Node: Extension Exercises1177392 +Node: Language History1178634 +Node: V7/SVR3.11180290 +Node: SVR41182442 +Node: POSIX1183876 +Node: BTL1185257 +Node: POSIX/GNU1185986 +Node: Feature History1191764 +Node: Common Extensions1208083 +Node: Ranges and Locales1209366 +Ref: Ranges and Locales-Footnote-11213982 +Ref: Ranges and Locales-Footnote-21214009 +Ref: Ranges and Locales-Footnote-31214244 +Node: Contributors1214467 +Node: History summary1220464 +Node: Installation1221844 +Node: Gawk Distribution1222788 +Node: Getting1223272 +Node: Extracting1224235 +Node: Distribution contents1225873 +Node: Unix Installation1232353 +Node: Quick Installation1233035 +Node: Shell Startup Files1235449 +Node: Additional Configuration Options1236538 +Node: Configuration Philosophy1238853 +Node: Non-Unix Installation1241222 +Node: PC Installation1241682 +Node: PC Binary Installation1242520 +Node: PC Compiling1242955 +Node: PC Using1244072 +Node: Cygwin1247625 +Node: MSYS1248849 +Node: VMS Installation1249451 +Node: VMS Compilation1250242 +Ref: VMS Compilation-Footnote-11251471 +Node: VMS Dynamic Extensions1251529 +Node: VMS Installation Details1253214 +Node: VMS Running1255467 +Node: VMS GNV1259746 +Node: VMS Old Gawk1260481 +Node: Bugs1260952 +Node: Bug address1261615 +Node: Usenet1264597 +Node: Maintainers1265601 +Node: Other Versions1266786 +Node: Installation summary1273874 +Node: Notes1275083 +Node: Compatibility Mode1275877 +Node: Additions1276659 +Node: Accessing The Source1277584 +Node: Adding Code1279021 +Node: New Ports1285240 +Node: Derived Files1289615 +Ref: Derived Files-Footnote-11295275 +Ref: Derived Files-Footnote-21295310 +Ref: Derived Files-Footnote-31295908 +Node: Future Extensions1296022 +Node: Implementation Limitations1296680 +Node: Extension Design1297890 +Node: Old Extension Problems1299034 +Ref: Old Extension Problems-Footnote-11300552 +Node: Extension New Mechanism Goals1300609 +Ref: Extension New Mechanism Goals-Footnote-11303973 +Node: Extension Other Design Decisions1304162 +Node: Extension Future Growth1306275 +Node: Notes summary1306881 +Node: Basic Concepts1308039 +Node: Basic High Level1308720 +Ref: figure-general-flow1309002 +Ref: figure-process-flow1309687 +Ref: Basic High Level-Footnote-11312988 +Node: Basic Data Typing1313173 +Node: Glossary1316501 +Node: Copying1348386 +Node: GNU Free Documentation License1385929 +Node: Index1411049 End Tag Table diff --git a/doc/gawk.texi b/doc/gawk.texi index 7f9816aa..40c85a81 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -920,6 +920,7 @@ particular records in a file and perform operations upon them. * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. * Getting Accuracy:: Getting more accuracy takes some work. * Try To Round:: Add digits and round. * Setting precision:: How to set the precision. @@ -3136,11 +3137,12 @@ column means that the person is a friend. An @samp{R} means that the person is a relative: @example -@c system if test ! -d eg ; then mkdir eg ; fi -@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi -@c system if test ! -d eg/data ; then mkdir eg/data ; fi -@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi -@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg ; then mkdir eg ; fi +@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi +@c system if test ! -d eg/data ; then mkdir eg/data ; fi +@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi +@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg/test-programs ; then mkdir eg/test-programs ; fi @c file eg/data/mail-list Amelia 555-5553 amelia.zodiacusque@@gmail.com F Anthony 555-3412 anthony.asserturo@@hotmail.com A @@ -8414,7 +8416,7 @@ FPAT = "([^,]*)|(\"[^\"]+\")" @c Per email from Ed Morton <mortoneccc@comcast.net> @c @c WONTFIX: 10/2020 -@c This is too much work. FPAT and CSV files are very flakey and +@c This is too much work. FPAT and CSV files are very flaky and @c fragile. Doing something like this is merely inviting trouble. As with @code{FS}, the @code{IGNORECASE} variable (@pxref{User-modified}) @@ -10064,7 +10066,7 @@ infinity are formatted as and positive infinity as @samp{inf} or @samp{infinity}. The special ``not a number'' value formats as @samp{-nan} or @samp{nan} -(@pxref{Math Definitions}). +(@pxref{Strange values}). @item @code{%F} Like @samp{%f}, but the infinity and ``not a number'' values are spelled @@ -18300,7 +18302,7 @@ compatibility mode (@pxref{Options}). @cindexawkfunc{log} @cindex logarithm Return the natural logarithm of @var{x}, if @var{x} is positive; -otherwise, return @code{NaN} (``not a number'') on IEEE 754 systems. +otherwise, return NaN (``not a number'') on IEEE 754 systems. Additionally, @command{gawk} prints a warning message when @code{x} is negative. @@ -33647,21 +33649,9 @@ A special value representing infinity. Operations involving another number and infinity produce infinity. @item NaN -``Not a number.''@footnote{Thanks to Michael Brennan for this description, -which we have paraphrased, and for the examples.} A special value that -results from attempting a calculation that has no answer as a real number. -In such a case, programs can either receive a floating-point exception, -or get @code{NaN} back as the result. The IEEE 754 standard recommends -that systems return @code{NaN}. Some examples: - -@table @code -@item sqrt(-1) -This makes sense in the range of complex numbers, but not in the -range of real numbers, so the result is @code{NaN}. - -@item log(-8) -@minus{}8 is out of the domain of @code{log()}, so the result is @code{NaN}. -@end table +``Not a number.'' A special value that results from attempting a +calculation that has no answer as a real number. @xref{Strange values}, +for more information about infinity and not-a-number values. @item Normalized How the significand (see later in this list) is usually stored. The @@ -33830,6 +33820,7 @@ decimal places in the final result. * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. @end menu @node Inexact representation @@ -33951,6 +33942,242 @@ $ @kbd{gawk 'BEGIN @{} @print{} 4 @end example +@node Strange values +@subsubsection Floating Point Values They Didn't Talk About In School + +Both IEEE 754 floating-point hardware, and MPFR, support two kinds of +values that you probably didn't learn about in school. The first is +@dfn{infinity}, a special value, that can be either negative or positive, +and which is either smaller than any other value (negative infinity), +or larger than any other value (positive infinity). When such values +are generated, @command{gawk} prints them as either @samp{-inf} or +@samp{+inf}, respectively. It accepts those strings as data input and +converts them to the proper floating-point values internally. + +Infinity values of the same sign compare as equal to each other. +Otherwise, operations (addition, subtraction, etc.) involving another +number and infinity produce mathematically reasonable results. + +The second kind of value is ``not a number'', or NaN for +short.@footnote{Thanks to Michael Brennan for this description, which we +have paraphrased, and for the examples.} This is a special value that results +from attempting a calculation that has no answer as a real number. +In such a case, programs can either receive a floating-point exception, +or get NaN back as the result. The IEEE 754 standard recommends +that systems return NaN. Some examples: + +@table @code +@item sqrt(-1) +@iftex +The @math{\sqrt{-1}} +@end iftex +@ifnottex +This +@end ifnottex +makes sense in the range of complex numbers, but not in the +range of real numbers, so the result is NaN. + +@item log(-8) +@minus{}8 is out of the domain of @code{log()}, so the result is NaN. +@end table + +NaN values are strange. In particular, they cannot be compared with other +floating point values; any such comparison, except for ``is not equal +to'', returns false. NaN values are so much unequal to other values that +even comparing two identical NaN values with @code{!=} returns true! + +NaN values can also be signed, although it depends upon the implementation +as to which sign you get for any operation that returns a NaN. For +example, on some systems, @code{sqrt(-1)} returns a negative NaN. On +others, it returns a positive NaN. + +When such values are generated, @command{gawk} prints them as either +@samp{-nan} or @samp{+nan}, respectively. Here too, @command{gawk} +accepts those strings as data input and converts them to the proper +floating-point values internally. + +If you want to dive more deeply into this topic, you can find +test programs in C, @command{awk} and Python in the directory +@file{awklib/eg/test-programs} in the @command{gawk} distribution. +These programs enable comparison among programming languages as to how +they handle NaN and infinity values. + +@ignore +@c file eg/test-programs/gen-float-table.awk +function eq(left, right) +{ + return left == right +} + +function ne(left, right) +{ + return left != right +} + +function lt(left, right) +{ + return left < right +} + +function le(left, right) +{ + return left <= right +} + +function gt(left, right) +{ + return left > right +} + +function ge(left, right) +{ + return left >= right +} + +BEGIN { + nan = sqrt(-1) + inf = -log(0) + split("== != < <= > >=", names) + names[3] = names[3] " " + names[5] = names[5] " " + split("eq ne lt le gt ge", funcs) + + compare[1] = 2.0 + compare[2] = values[1] = -sqrt(-1.0) # nan + compare[3] = values[2] = sqrt(-1.0) # -nan + compare[4] = values[3] = -log(0.0) # inf + compare[5] = values[4] = log(0.0) # -inf + + for (i = 1; i in values; i++) { + for (j = 1; j in compare; j++) { + for (k = 1; k in names; k++) { + the_func = funcs[k] + printf("%g %s %g -> %s\n", + values[i], + names[k], + compare[j], + @the_func(values[i], compare[j]) ? + "true" : "false"); + } + printf("\n"); + } + } +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.c +#include <stdio.h> +#include <math.h> +#include <stdbool.h> + +#define def_func(name, op) \ + bool name(double left, double right) { \ + return left op right; \ + } + +def_func(eq, ==) +def_func(ne, !=) +def_func(lt, <) +def_func(le, <=) +def_func(gt, >) +def_func(ge, >=) + +struct { + const char *name; + bool (*func)(double left, double right); +} functions[] = { + { "==", eq }, + { "!=", ne }, + { "< ", lt }, + { "<=", le }, + { "> ", gt }, + { ">=", ge }, + { 0, 0 } +}; + +int main() +{ + double values[] = { + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + double compare[] = { 2.0, + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + + int i, j, k; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 5; j++) { + for (k = 0; functions[k].name != NULL; k++) { + printf("%g %s %g -> %s\n", values[i], + functions[k].name, + compare[j], + functions[k].func(values[i], compare[j]) ? "true" : "false"); + } + printf("\n"); + } + } + + return 0; +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.py +from math import * + +nan = float('NaN') +inf = float('Inf') + +def eq(left, right): + return left == right + +def ne(left, right): + return left != right + +def lt(left, right): + return left < right + +def le(left, right): + return left <= right + +def gt(left, right): + return left > right + +def ge(left, right): + return left >= right + +func_map = { + "==": eq, + "!=": ne, + "< ": lt, + "<=": le, + "> ": gt, + ">=": ge, +} + +compare = [2.0, nan, -nan, inf, -inf] +values = [nan, -nan, inf, -inf] + +for i in range(len(values)): + for j in range(len(compare)): + for op in func_map: + print("%g %s %g -> %s" % + (values[i], op, compare[j], func_map[op](values[i], compare[j]))) + + print("") +@c endfile +@end ignore + @node Getting Accuracy @subsection Getting the Accuracy You Need diff --git a/doc/gawktexi.in b/doc/gawktexi.in index ceacad6b..5a4e7479 100644 --- a/doc/gawktexi.in +++ b/doc/gawktexi.in @@ -915,6 +915,7 @@ particular records in a file and perform operations upon them. * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. * Getting Accuracy:: Getting more accuracy takes some work. * Try To Round:: Add digits and round. * Setting precision:: How to set the precision. @@ -3046,11 +3047,12 @@ column means that the person is a friend. An @samp{R} means that the person is a relative: @example -@c system if test ! -d eg ; then mkdir eg ; fi -@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi -@c system if test ! -d eg/data ; then mkdir eg/data ; fi -@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi -@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg ; then mkdir eg ; fi +@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi +@c system if test ! -d eg/data ; then mkdir eg/data ; fi +@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi +@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg/test-programs ; then mkdir eg/test-programs ; fi @c file eg/data/mail-list Amelia 555-5553 amelia.zodiacusque@@gmail.com F Anthony 555-3412 anthony.asserturo@@hotmail.com A @@ -7967,7 +7969,7 @@ FPAT = "([^,]*)|(\"[^\"]+\")" @c Per email from Ed Morton <mortoneccc@comcast.net> @c @c WONTFIX: 10/2020 -@c This is too much work. FPAT and CSV files are very flakey and +@c This is too much work. FPAT and CSV files are very flaky and @c fragile. Doing something like this is merely inviting trouble. As with @code{FS}, the @code{IGNORECASE} variable (@pxref{User-modified}) @@ -9617,7 +9619,7 @@ infinity are formatted as and positive infinity as @samp{inf} or @samp{infinity}. The special ``not a number'' value formats as @samp{-nan} or @samp{nan} -(@pxref{Math Definitions}). +(@pxref{Strange values}). @item @code{%F} Like @samp{%f}, but the infinity and ``not a number'' values are spelled @@ -17525,7 +17527,7 @@ compatibility mode (@pxref{Options}). @cindexawkfunc{log} @cindex logarithm Return the natural logarithm of @var{x}, if @var{x} is positive; -otherwise, return @code{NaN} (``not a number'') on IEEE 754 systems. +otherwise, return NaN (``not a number'') on IEEE 754 systems. Additionally, @command{gawk} prints a warning message when @code{x} is negative. @@ -32613,21 +32615,9 @@ A special value representing infinity. Operations involving another number and infinity produce infinity. @item NaN -``Not a number.''@footnote{Thanks to Michael Brennan for this description, -which we have paraphrased, and for the examples.} A special value that -results from attempting a calculation that has no answer as a real number. -In such a case, programs can either receive a floating-point exception, -or get @code{NaN} back as the result. The IEEE 754 standard recommends -that systems return @code{NaN}. Some examples: - -@table @code -@item sqrt(-1) -This makes sense in the range of complex numbers, but not in the -range of real numbers, so the result is @code{NaN}. - -@item log(-8) -@minus{}8 is out of the domain of @code{log()}, so the result is @code{NaN}. -@end table +``Not a number.'' A special value that results from attempting a +calculation that has no answer as a real number. @xref{Strange values}, +for more information about infinity and not-a-number values. @item Normalized How the significand (see later in this list) is usually stored. The @@ -32796,6 +32786,7 @@ decimal places in the final result. * Inexact representation:: Numbers are not exactly represented. * Comparing FP Values:: How to compare floating point values. * Errors accumulate:: Errors get bigger as they go. +* Strange values:: A few words about infinities and NaNs. @end menu @node Inexact representation @@ -32917,6 +32908,242 @@ $ @kbd{gawk 'BEGIN @{} @print{} 4 @end example +@node Strange values +@subsubsection Floating Point Values They Didn't Talk About In School + +Both IEEE 754 floating-point hardware, and MPFR, support two kinds of +values that you probably didn't learn about in school. The first is +@dfn{infinity}, a special value, that can be either negative or positive, +and which is either smaller than any other value (negative infinity), +or larger than any other value (positive infinity). When such values +are generated, @command{gawk} prints them as either @samp{-inf} or +@samp{+inf}, respectively. It accepts those strings as data input and +converts them to the proper floating-point values internally. + +Infinity values of the same sign compare as equal to each other. +Otherwise, operations (addition, subtraction, etc.) involving another +number and infinity produce mathematically reasonable results. + +The second kind of value is ``not a number'', or NaN for +short.@footnote{Thanks to Michael Brennan for this description, which we +have paraphrased, and for the examples.} This is a special value that results +from attempting a calculation that has no answer as a real number. +In such a case, programs can either receive a floating-point exception, +or get NaN back as the result. The IEEE 754 standard recommends +that systems return NaN. Some examples: + +@table @code +@item sqrt(-1) +@iftex +The @math{\sqrt{-1}} +@end iftex +@ifnottex +This +@end ifnottex +makes sense in the range of complex numbers, but not in the +range of real numbers, so the result is NaN. + +@item log(-8) +@minus{}8 is out of the domain of @code{log()}, so the result is NaN. +@end table + +NaN values are strange. In particular, they cannot be compared with other +floating point values; any such comparison, except for ``is not equal +to'', returns false. NaN values are so much unequal to other values that +even comparing two identical NaN values with @code{!=} returns true! + +NaN values can also be signed, although it depends upon the implementation +as to which sign you get for any operation that returns a NaN. For +example, on some systems, @code{sqrt(-1)} returns a negative NaN. On +others, it returns a positive NaN. + +When such values are generated, @command{gawk} prints them as either +@samp{-nan} or @samp{+nan}, respectively. Here too, @command{gawk} +accepts those strings as data input and converts them to the proper +floating-point values internally. + +If you want to dive more deeply into this topic, you can find +test programs in C, @command{awk} and Python in the directory +@file{awklib/eg/test-programs} in the @command{gawk} distribution. +These programs enable comparison among programming languages as to how +they handle NaN and infinity values. + +@ignore +@c file eg/test-programs/gen-float-table.awk +function eq(left, right) +{ + return left == right +} + +function ne(left, right) +{ + return left != right +} + +function lt(left, right) +{ + return left < right +} + +function le(left, right) +{ + return left <= right +} + +function gt(left, right) +{ + return left > right +} + +function ge(left, right) +{ + return left >= right +} + +BEGIN { + nan = sqrt(-1) + inf = -log(0) + split("== != < <= > >=", names) + names[3] = names[3] " " + names[5] = names[5] " " + split("eq ne lt le gt ge", funcs) + + compare[1] = 2.0 + compare[2] = values[1] = -sqrt(-1.0) # nan + compare[3] = values[2] = sqrt(-1.0) # -nan + compare[4] = values[3] = -log(0.0) # inf + compare[5] = values[4] = log(0.0) # -inf + + for (i = 1; i in values; i++) { + for (j = 1; j in compare; j++) { + for (k = 1; k in names; k++) { + the_func = funcs[k] + printf("%g %s %g -> %s\n", + values[i], + names[k], + compare[j], + @the_func(values[i], compare[j]) ? + "true" : "false"); + } + printf("\n"); + } + } +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.c +#include <stdio.h> +#include <math.h> +#include <stdbool.h> + +#define def_func(name, op) \ + bool name(double left, double right) { \ + return left op right; \ + } + +def_func(eq, ==) +def_func(ne, !=) +def_func(lt, <) +def_func(le, <=) +def_func(gt, >) +def_func(ge, >=) + +struct { + const char *name; + bool (*func)(double left, double right); +} functions[] = { + { "==", eq }, + { "!=", ne }, + { "< ", lt }, + { "<=", le }, + { "> ", gt }, + { ">=", ge }, + { 0, 0 } +}; + +int main() +{ + double values[] = { + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + double compare[] = { 2.0, + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + + int i, j, k; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 5; j++) { + for (k = 0; functions[k].name != NULL; k++) { + printf("%g %s %g -> %s\n", values[i], + functions[k].name, + compare[j], + functions[k].func(values[i], compare[j]) ? "true" : "false"); + } + printf("\n"); + } + } + + return 0; +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.py +from math import * + +nan = float('NaN') +inf = float('Inf') + +def eq(left, right): + return left == right + +def ne(left, right): + return left != right + +def lt(left, right): + return left < right + +def le(left, right): + return left <= right + +def gt(left, right): + return left > right + +def ge(left, right): + return left >= right + +func_map = { + "==": eq, + "!=": ne, + "< ": lt, + "<=": le, + "> ": gt, + ">=": ge, +} + +compare = [2.0, nan, -nan, inf, -inf] +values = [nan, -nan, inf, -inf] + +for i in range(len(values)): + for j in range(len(compare)): + for op in func_map: + print("%g %s %g -> %s" % + (values[i], op, compare[j], func_map[op](values[i], compare[j]))) + + print("") +@c endfile +@end ignore + @node Getting Accuracy @subsection Getting the Accuracy You Need diff --git a/doc/it/ChangeLog b/doc/it/ChangeLog index 863eef47..5a6bd03d 100644 --- a/doc/it/ChangeLog +++ b/doc/it/ChangeLog @@ -1,3 +1,7 @@ +2020-11-20 Antonio Giovanni Colombo <azc100@gmail.com> + + * gawktexi.in: Updated. + 2020-11-01 Antonio Giovanni Colombo <azc100@gmail.com> * texinfo.tex: Updated. diff --git a/doc/it/gawktexi.in b/doc/it/gawktexi.in index fe7c6b29..a8c0874f 100644..100755 --- a/doc/it/gawktexi.in +++ b/doc/it/gawktexi.in @@ -1065,13 +1065,14 @@ Copyright dell'edizione italiana @copyright{} 2016 -- Free Software Foundation, esattamente. * Confronti tra valori in VM:: Come confrontare valori in virgola mobile. * Gli errori si sommano:: Gli errori diventano sempre maggiori. +* Valori strani:: Un cenno riguardo ai valori infiniti e a NaN [``non @`e un numero'']. * Ottenere la precisione:: Ottenere la precisione voluta. * Tentare di arrotondare:: Tentare di aggiungere bit di precisione e arrotondare. * Impostare la precisione:: Impostare la precisione. * Impostare modo di arrotondare:: Impostare la modalit@`a di arrotondamento. -* Controllare disponibilit@`a MPFR:: Come controllare se MPFR @`e disponibile. +* Controllare disponibilit@`a MPFR:: Come controllare se MPFR @`e disponibile. * Interi a precisione arbitraria:: Aritmetica dei numeri interi a precisione arbitraria con @command{gawk}. * Problemi virgola mobile POSIX:: Confronto tra standard e uso corrente. @@ -3338,11 +3339,12 @@ persona @`e un amico [Friend]. Una @samp{R} vuol dire che quella persona @`e un parente [Relative]: @example -@c system if test ! -d eg ; then mkdir eg ; fi -@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi -@c system if test ! -d eg/data ; then mkdir eg/data ; fi -@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi -@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg ; then mkdir eg ; fi +@c system if test ! -d eg/lib ; then mkdir eg/lib ; fi +@c system if test ! -d eg/data ; then mkdir eg/data ; fi +@c system if test ! -d eg/prog ; then mkdir eg/prog ; fi +@c system if test ! -d eg/misc ; then mkdir eg/misc ; fi +@c system if test ! -d eg/test-programs ; then mkdir eg/test-programs ; fi @c file eg/data/mail-list Amelia 555-5553 amelia.zodiacusque@@gmail.com F Anthony 555-3412 anthony.asserturo@@hotmail.com A @@ -8710,7 +8712,7 @@ FPAT = "([^,]*)|(\"[^\"]+\")" @c Per email from Ed Morton <mortoneccc@comcast.net> @c @c WONTFIX: 10/2020 -@c This is too much work. FPAT and CSV files are very flakey and +@c This is too much work. FPAT and CSV files are very flaky and @c fragile. Doing something like this is merely inviting trouble. Come per @code{FS}, la variabile @code{IGNORECASE} @@ -10473,9 +10475,9 @@ dallo standard IEEE 754, il valore infinito negativo @`e rappresentato come @samp{-inf} o @samp{-infinity}, e l'infinito positivo come @samp{inf} o @samp{infinity}. -Il valore speciale ``not a number'' [non @`e un numero] viene scritto come +Il valore speciale ``not a number'' ["non @`e un numero"] viene scritto come @samp{-nan} o @samp{nan} -(@pxref{Definizioni matematiche}). +(@pxref{Valori strani}). @item @code{%F} Come @samp{%f}, ma i valori di infinito e di ``not a number'' sono scritti @@ -16060,16 +16062,17 @@ risultato. In @command{gawk}, l'esecuzione di @code{nextfile} produce ulteriori effetti: le eventuali regole @code{ENDFILE} sono eseguite se @command{gawk} non -si trova correntemente all'interno di una regola @code{END} o -@code{BEGINFILE}; @code{ARGIND} @`e +si trova correntemente all'interno di una regola @code{END}, +@code{ARGIND} @`e incrementato e le eventuali regole @code{BEGINFILE} sono eseguite. (@code{ARGIND} non @`e stato ancora trattato. @xref{Variabili predefinite}.) -In @command{gawk}, @code{nextfile} @`e utile all'interno di una regola +C'@`e un ulteriore caso speciale di utilizzo in @command{gawk}. +@code{nextfile} @`e utile all'interno di una regola @code{BEGINFILE} per evitare di elaborare un file che altrimenti causerebbe un errore fatale in @command{gawk}. -In questo caso, le regole @code{ENDFILE} non vengono eseguite. +In questo caso speciale, le regole @code{ENDFILE} non vengono eseguite. @xref{BEGINFILE/ENDFILE}. Sebbene possa sembrare che @samp{close(FILENAME)} ottenga lo stesso @@ -19051,8 +19054,8 @@ modalit@`a compatibile (@pxref{Opzioni}). @cindexawkfunc{log} @cindex logaritmo Restituisce il logaritmo naturale di @var{x}, se @var{x} @`e positivo; -altrimenti, restituisce @code{NaN} (``not a number'') sui sistemi che -implementano lo standard IEEE 754. +altrimenti, restituisce NaN (``not a number'',[``non @`e un numero'']) +sui sistemi che implementano lo standard IEEE 754. Inoltre, @command{gawk} stampa un messaggio di avvertimento qualora @code{x} sia negativo. @@ -35366,23 +35369,10 @@ Come i numeri vanno arrotondati, per eccesso o per difetto, quando necessario. Maggiori dettagli verranno forniti in seguito. @item NaN -``Not a number'' (Non un Numero).@footnote{Grazie a Michael -Brennan per questa descrizione, che abbiamo parafrasato, e per gli esempi.} Un -valore speciale che risulta da un calcolo che non ha risposta come numero -reale. In tal caso, i programmi possono o ricevere un'eccezione di virgola -mobile, o restituire @code{NaN} come risultato. Lo standard IEEE 754 -consiglia che i sistemi restituiscano @code{NaN}. Alcuni esempi: - -@table @code -@item sqrt(-1) -La radice quadrata di @minus{}1 ha senso nell'insieme dei numeri complessi, -ma non nell'insieme dei numeri reali, -per cui il risultato @`e @code{NaN}. - -@item log(-8) -Il logaritmo di @minus{}8 @`e fuori dal dominio di @code{log()}, -per cui il risultato @`e @code{NaN}. -@end table +``Not a number'' (``non @`e un numero''). Un valore speciale +che risulta da un calcolo che non ha come risposta un numero +reale. @xref{Valori strani}, per maggiori informazioni riguardo +ai valori infiniti e ai valori ``non-un-numero''. @item Normalizzato (formato) Come la mantissa (vedi oltre in questa lista) @`e usualmente memorizzata. Il @@ -35534,8 +35524,7 @@ ottenere ulteriori informazioni, e non basarsi solo su quanto qui detto. * Ottenere la precisione:: Ottenere pi@`u precisione richiede qualche sforzo. * Tentare di arrotondare:: Aggiungere cifre di precisione e arrotondare. -* Impostare la precisione:: Come impostare la precisione. -* Impostare modo di arrotondare:: Impostare le modalit@`a di arrotondamento. +* Valori strani:: Un cenno riguardo ai valori infiniti e a NaN [``non @`e un numero'']. @end menu @node Inesattezza nei calcoli @@ -35682,6 +35671,257 @@ $ @kbd{gawk 'BEGIN @{} @print{} 4 @end example +@node Valori strani +@subsubsection Valori in virgola mobile non spiegati a scuola + +Sia l'hardware che implementa lo standard per i numeri in virgola +mobili IEEE 754, che la libreria matematica MPFR, prevedono due +tipi di valori di cui probabilmente non vi hanno parlato a scuola. +Il primo @`e il valore @dfn{infinity} (``infinito''), un valore speciale +che pu@`o avere un segno sia negativo che positivo, e che @`e pi@`u +piccolo di ogni altro valore (infinito negativo), o maggiore di ogni +altro valore (infinito positivo). Quando vengono generati tali valori +@command{gawk} li stampa come @samp{-inf} o @samp{+inf}, rispettivamente. +@command{gawk} accetta queste stringhe come dati in input, e li converte +internamente all'appropriato valore in virgola mobile. + +Valori di infinito che abbiano lo stesso segno risultano uguali +quando sono confrontati fra loro. +Per il resto, altre operazioni (addizione, sottrazione, etc.) +che hanno come operando un infinito e un altro numero producono +risultati matematicamente ragionevoli. + +Il secondo tipo di valore @`e ``not a number'' [``non @`e un numero''] +scritto in forma abbreviata come NaN.@footnote{Grazie a Michael Brennan +per questa descrizione, che abbiamo parafrasato, e per gli esempi.} + +Questo @`e un valore speciale che risulta da un calcolo che non ha come +risposta un numero reale. In tal caso, i programmi possono o ricevere +un’eccezione di virgola mobile, o restituire NaN come risultato. +Lo standard IEEE 754 consiglia che i sistemi restituiscano NaN. +Alcuni esempi: + +@table @code +@item sqrt(-1) +@iftex +La funzione @math{\sqrt{-1}} +@end iftex +@ifnottex +Questa funzione +@end ifnottex +ha senso nell'insieme dei numeri complessi, +ma non nell'insieme dei numeri reali, +per cui il risultato @`e @code{NaN}. + +@item log(-8) +Il logaritmo di @minus{}8 @`e fuori dal dominio di @code{log()}, +per cui il risultato @`e @code{NaN}. +@end table + +I valori Nan sono strani. In particolare, non possono essere confrontati +con altri numeri in virgola mobile; ogni confronto di questo tipo, eccetto +quello ``non uguale a'', restituisce il valore ``falso''. +I valori NaN sono talmente differenti da altri valori che perfino il +confronto di due valori NaN identici fra loro con @code{!=} restituisce +il valore ``vero''! + +I valori NaN possono anche avere un segno (positivo o negativo), +anche se dipende dall'implementazione quale segno viene restituito +da qualsiasi operazione il cui risultato sia un valore NaN. +Per esempio, su alcuni sistemi la funzione @code{sqrt(-1)} +restituisce un NaN negativo. Su altri sistemi il NaN restituito +@`e invece positivo. + +Quando tali valori vengono generati, @command{gawk} li stampa +come @samp{-nan} o @samp{+nan}, rispettivamente. Anche per +questi valori, @command{gawk} accetta queste stringhe come +dati in input e le converte internamente ai valori loro +assegnati come numeri in virgola mobile. + +Se si desidera approfondire ulteriormente questo argomento, si possono +trovare programmi di test scritti in C, @command{awk} e Python +nella directory @file{awklib/eg/test-programs} disponibile +nella distribuzione di @command{gawk}. +Tali programmi permettono un confronto tra i linguaggi di +programmazione, riguardo al modo con cui vengono trattati +i valori di infinito e quelli NaN. + +@ignore +@c file eg/test-programs/gen-float-table.awk +function eq(left, right) +{ + return left == right +} + +function ne(left, right) +{ + return left != right +} + +function lt(left, right) +{ + return left < right +} + +function le(left, right) +{ + return left <= right +} + +function gt(left, right) +{ + return left > right +} + +function ge(left, right) +{ + return left >= right +} + +BEGIN { + nan = sqrt(-1) + inf = -log(0) + split("== != < <= > >=", names) + names[3] = names[3] " " + names[5] = names[5] " " + split("eq ne lt le gt ge", funcs) + + compare[1] = 2.0 + compare[2] = values[1] = -sqrt(-1.0) # nan + compare[3] = values[2] = sqrt(-1.0) # -nan + compare[4] = values[3] = -log(0.0) # inf + compare[5] = values[4] = log(0.0) # -inf + + for (i = 1; i in values; i++) { + for (j = 1; j in compare; j++) { + for (k = 1; k in names; k++) { + the_func = funcs[k] + printf("%g %s %g -> %s\n", + values[i], + names[k], + compare[j], + @the_func(values[i], compare[j]) ? + "true" : "false"); + } + printf("\n"); + } + } +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.c +#include <stdio.h> +#include <math.h> +#include <stdbool.h> + +#define def_func(name, op) \ + bool name(double left, double right) { \ + return left op right; \ + } + +def_func(eq, ==) +def_func(ne, !=) +def_func(lt, <) +def_func(le, <=) +def_func(gt, >) +def_func(ge, >=) + +struct { + const char *name; + bool (*func)(double left, double right); +} functions[] = { + { "==", eq }, + { "!=", ne }, + { "< ", lt }, + { "<=", le }, + { "> ", gt }, + { ">=", ge }, + { 0, 0 } +}; + +int main() +{ + double values[] = { + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + double compare[] = { 2.0, + -sqrt(-1), // nan + sqrt(-1), // -nan + -log(0.0), // inf + log(0.0) // -inf + }; + + int i, j, k; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 5; j++) { + for (k = 0; functions[k].name != NULL; k++) { + printf("%g %s %g -> %s\n", values[i], + functions[k].name, + compare[j], + functions[k].func(values[i], compare[j]) ? "true" : "false"); + } + printf("\n"); + } + } + + return 0; +} +@c endfile +@end ignore + +@ignore +@c file eg/test-programs/gen-float-table.py +from math import * + +nan = float('NaN') +inf = float('Inf') + +def eq(left, right): + return left == right + +def ne(left, right): + return left != right + +def lt(left, right): + return left < right + +def le(left, right): + return left <= right + +def gt(left, right): + return left > right + +def ge(left, right): + return left >= right + +func_map = { + "==": eq, + "!=": ne, + "< ": lt, + "<=": le, + "> ": gt, + ">=": ge, +} + +compare = [2.0, nan, -nan, inf, -inf] +values = [nan, -nan, inf, -inf] + +for i in range(len(values)): + for j in range(len(compare)): + for op in func_map: + print("%g %s %g -> %s" % + (values[i], op, compare[j], func_map[op](values[i], compare[j]))) + + print("") +@c endfile +@end ignore + @node Ottenere la precisione @subsection Ottenere la precisione voluta diff --git a/doc/wordlist b/doc/wordlist index c93eda53..9ce31188 100644 --- a/doc/wordlist +++ b/doc/wordlist @@ -316,6 +316,7 @@ NR NT NUMCUR NaN +NaNs Nachum Neacsu Neacsu's @@ -483,6 +484,7 @@ VT Versioning Vinschen WIPO +WONTFIX Walamazoo Wallin Watchpoint @@ -24,10 +24,8 @@ */ #include "awk.h" +#include <math.h> -extern double pow(double x, double y); -extern double modf(double x, double *yp); -extern double fmod(double x, double y); NODE **fcall_list = NULL; long fcall_count = 0; int currule = 0; @@ -1520,18 +1518,17 @@ eval_condition(NODE *t) return boolval(t); } -typedef enum { - SCALAR_EQ_NEQ, - SCALAR_RELATIONAL -} scalar_cmp_t; +static bool cmp_doubles(const NODE *t1, const NODE *t2, scalar_cmp_t comparison_type); +extern bool mpg_cmp_as_numbers(const NODE *t1, const NODE *t2, scalar_cmp_t comparison_type); /* cmp_scalars -- compare two nodes on the stack */ -static inline int +static bool cmp_scalars(scalar_cmp_t comparison_type) { NODE *t1, *t2; int di; + bool ret; t2 = POP_SCALAR(); t1 = TOP(); @@ -1539,12 +1536,91 @@ cmp_scalars(scalar_cmp_t comparison_type) DEREF(t2); fatal(_("attempt to use array `%s' in a scalar context"), array_vname(t1)); } - di = cmp_nodes(t1, t2, comparison_type == SCALAR_EQ_NEQ); + + if ((t1->flags & STRING) != 0 || (t2->flags & STRING) != 0) { + bool use_strcmp = (comparison_type == SCALAR_EQ || comparison_type == SCALAR_NEQ); + di = cmp_nodes(t1, t2, use_strcmp); + + switch (comparison_type) { + case SCALAR_EQ: + ret = (di == 0); + break; + case SCALAR_NEQ: + ret = (di != 0); + break; + case SCALAR_LT: + ret = (di < 0); + break; + case SCALAR_LE: + ret = (di <= 0); + break; + case SCALAR_GT: + ret = (di > 0); + break; + case SCALAR_GE: + ret = (di >= 0); + break; + } + } else { + fixtype(t1); + fixtype(t2); + +#ifdef HAVE_MPFR + if (do_mpfr) + ret = mpg_cmp_as_numbers(t1, t2, comparison_type); + else +#endif + ret = cmp_doubles(t1, t2, comparison_type); + } + DEREF(t1); DEREF(t2); - return di; + return ret; } + +/* cmp_doubles --- compare two doubles */ + +static bool +cmp_doubles(const NODE *t1, const NODE *t2, scalar_cmp_t comparison_type) +{ + /* + * This routine provides numeric comparisons that should work + * the same as in C. It should NOT be used for sorting. + */ + + bool t1_nan = isnan(t1->numbr); + bool t2_nan = isnan(t2->numbr); + int ret; + + if ((t1_nan || t2_nan) && comparison_type != SCALAR_NEQ) + return false; + + switch (comparison_type) { + case SCALAR_EQ: + ret = (t1->numbr == t2->numbr); + break; + case SCALAR_NEQ: + ret = (t1->numbr != t2->numbr); + break; + case SCALAR_LT: + ret = (t1->numbr < t2->numbr); + break; + case SCALAR_LE: + ret = (t1->numbr <= t2->numbr); + break; + case SCALAR_GT: + ret = (t1->numbr > t2->numbr); + break; + case SCALAR_GE: + ret = (t1->numbr >= t2->numbr); + break; + } + + return ret; +} + + /* op_assign --- assignment operators excluding = */ static void diff --git a/interpret.h b/interpret.h index fedf5255..40dd39d6 100644 --- a/interpret.h +++ b/interpret.h @@ -486,37 +486,37 @@ uninitialized_scalar: break; case Op_equal: - r = node_Boolean[cmp_scalars(SCALAR_EQ_NEQ) == 0]; + r = node_Boolean[cmp_scalars(SCALAR_EQ)]; UPREF(r); REPLACE(r); break; case Op_notequal: - r = node_Boolean[cmp_scalars(SCALAR_EQ_NEQ) != 0]; + r = node_Boolean[cmp_scalars(SCALAR_NEQ)]; UPREF(r); REPLACE(r); break; case Op_less: - r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) < 0]; + r = node_Boolean[cmp_scalars(SCALAR_LT)]; UPREF(r); REPLACE(r); break; case Op_greater: - r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) > 0]; + r = node_Boolean[cmp_scalars(SCALAR_GT)]; UPREF(r); REPLACE(r); break; case Op_leq: - r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) <= 0]; + r = node_Boolean[cmp_scalars(SCALAR_LE)]; UPREF(r); REPLACE(r); break; case Op_geq: - r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) >= 0]; + r = node_Boolean[cmp_scalars(SCALAR_GE)]; UPREF(r); REPLACE(r); break; @@ -433,6 +433,50 @@ mpg_cmp(const NODE *t1, const NODE *t2) return cmp_awknums(t1, t2); } +/* mpg_cmp_as_numbers --- compare two numbers, similar to doubles */ + +bool +mpg_cmp_as_numbers(const NODE *t1, const NODE *t2, scalar_cmp_t comparison_type) +{ + /* + * This routine provides numeric comparisons that should work + * the same as in C. It should NOT be used for sorting. + */ + + bool t1_nan = mpfr_nan_p(t1->mpg_numbr); + bool t2_nan = mpfr_nan_p(t2->mpg_numbr); + int ret; + + // MPFR is different than native doubles... + if (t1_nan || t2_nan) + return comparison_type == SCALAR_NEQ; + + int di = mpg_cmp(t1, t2); + + switch (comparison_type) { + case SCALAR_EQ: + ret = (di == 0); + break; + case SCALAR_NEQ: + ret = (di != 0); + break; + case SCALAR_LT: + ret = (di < 0); + break; + case SCALAR_LE: + ret = (di <= 0); + break; + case SCALAR_GT: + ret = (di > 0); + break; + case SCALAR_GE: + ret = (di >= 0); + break; + } + + return ret; +} + /* * mpg_update_var --- update NR or FNR. @@ -25,7 +25,7 @@ */ #include "awk.h" -#include "math.h" +#include <math.h> #include "floatmagic.h" /* definition of isnan */ static int is_ieee_magic_val(const char *val); @@ -367,7 +367,7 @@ int cmp_awknums(const NODE *t1, const NODE *t2) { /* - * This routine is also used to sort numeric array indices or values. + * This routine is used to sort numeric array indices or values. * For the purposes of sorting, NaN is considered greater than * any other value, and all NaN values are considered equivalent and equal. * This isn't in compliance with IEEE standard, but compliance w.r.t. NaN @@ -387,7 +387,6 @@ cmp_awknums(const NODE *t1, const NODE *t2) return 1; } - /* make_str_node --- make a string node */ NODE * |