summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2014-07-10 16:32:44 -0700
committerArnold D. Robbins <arnold@skeeve.com>2014-07-10 16:32:44 -0700
commitdf2eaea6a92c7d89d604d0a4e885d064678ce3ed (patch)
tree127610b11382d876d1be2ebce2f770d60147788b
parent21606db0d06b91332b1514f6662f7bc6d414e54e (diff)
downloadgawk-df2eaea6a92c7d89d604d0a4e885d064678ce3ed.tar.gz
Add div() function for integer division & remainder.
-rw-r--r--ChangeLog15
-rw-r--r--NEWS4
-rw-r--r--TODO3
-rw-r--r--awk.h2
-rw-r--r--awkgram.c12
-rw-r--r--awkgram.y12
-rw-r--r--builtin.c66
-rw-r--r--doc/ChangeLog6
-rw-r--r--doc/awkcard.in3
-rw-r--r--doc/gawk.119
-rw-r--r--doc/gawk.info708
-rw-r--r--doc/gawk.texi44
-rw-r--r--doc/gawktexi.in44
-rw-r--r--mpfr.c87
14 files changed, 682 insertions, 343 deletions
diff --git a/ChangeLog b/ChangeLog
index 101a1d1f..3fb8bb98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2014-07-10 Arnold D. Robbins <arnold@skeeve.com>
+ New `div()' function to do integer division and remainder;
+ mainly useful for use with GMP integers. Thanks to
+ Katie Wasserman <katie@wass.net> for the suggestion.
+
+ * awk.h (do_div, do_mpfr_div): Declare new functions.
+ * builtin.c (do_div): New function.
+ * mpfr.c (do_mpfr_div): New function.
+ * awkgram.y (tokentab): New entry.
+ (snode): Add check for do_div/do_mpfr_div to make 3rd arg
+ be an array.
+ * NEWS: Updated.
+ * TODO: Updated.
+
+2014-07-10 Arnold D. Robbins <arnold@skeeve.com>
+
* awkgram.y (check_for_bad): New routine to do the fatal message,
with smarter checking.
(nextc): Call it as appropriate.
diff --git a/NEWS b/NEWS
index 313b065d..c2cf63ac 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,10 @@ Changes from 4.1.x to 4.2.0
4. The igawk script and igawk.1 man page are no longer installed by
`make install'. They have been obsolete since gawk 4.0.0.
+5. Gawk now has a `div()' function to perform integer division; this is
+ primarily useful for the -M option to avoid MPFR division when all
+ values involved are integers.
+
Changes from 4.1.1 to 4.1.2
---------------------------
diff --git a/TODO b/TODO
index 8641def6..35b327bc 100644
--- a/TODO
+++ b/TODO
@@ -45,9 +45,6 @@ Minor Cleanups and Code Improvements
Minor New Features
------------------
- Add a div() function to do integer division result. Needed
- esp for MPFR with large ints.
-
Enhance extension/fork.c waitpid to allow the caller to specify
the options. And add an optional array argument to wait and
waitpid in which to return exit status information.
diff --git a/awk.h b/awk.h
index 9255b45b..cb9f8091 100644
--- a/awk.h
+++ b/awk.h
@@ -1419,6 +1419,7 @@ extern AWKNUM nondec2awknum(char *str, size_t len);
extern NODE *do_dcgettext(int nargs);
extern NODE *do_dcngettext(int nargs);
extern NODE *do_bindtextdomain(int nargs);
+extern NODE *do_div(int nargs);
#if MBS_SUPPORT
extern int strncasecmpmbs(const unsigned char *,
const unsigned char *, size_t);
@@ -1555,6 +1556,7 @@ extern NODE *do_mpfr_and(int);
extern NODE *do_mpfr_atan2(int);
extern NODE *do_mpfr_compl(int);
extern NODE *do_mpfr_cos(int);
+extern NODE *do_mpfr_div(int);
extern NODE *do_mpfr_exp(int);
extern NODE *do_mpfr_int(int);
extern NODE *do_mpfr_log(int);
diff --git a/awkgram.c b/awkgram.c
index 711c2e37..28776494 100644
--- a/awkgram.c
+++ b/awkgram.c
@@ -4199,6 +4199,7 @@ static const struct token tokentab[] = {
{"dcngettext", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3)|A(4)|A(5), do_dcngettext, 0},
{"default", Op_K_default, LEX_DEFAULT, GAWKX, 0, 0},
{"delete", Op_K_delete, LEX_DELETE, NOT_OLD, 0, 0},
+{"div", Op_builtin, LEX_BUILTIN, GAWKX|A(3), do_div, MPF(div)},
{"do", Op_K_do, LEX_DO, NOT_OLD|BREAK|CONTINUE, 0, 0},
{"else", Op_K_else, LEX_ELSE, 0, 0, 0},
{"eval", Op_symbol, LEX_EVAL, 0, 0, 0},
@@ -6244,7 +6245,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
}
#ifdef HAVE_MPFR
- /* N.B.: There isn't any special processing for an alternate function below */
+ /* N.B.: If necessary, add special processing for alternate builtin, below */
if (do_mpfr && tokentab[idx].ptr2)
r->builtin = tokentab[idx].ptr2;
else
@@ -6273,6 +6274,15 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
arg = subn->nexti;
if (arg->nexti == arg->lasti && arg->nexti->opcode == Op_push)
arg->nexti->opcode = Op_push_arg; /* argument may be array */
+ } else if (r->builtin == do_div
+#ifdef HAVE_MPFR
+ || r->builtin == MPF(div)
+#endif
+ ) {
+ arg = subn->nexti->lasti->nexti->lasti->nexti; /* 3rd arg list */
+ ip = arg->lasti;
+ if (ip->opcode == Op_push)
+ ip->opcode = Op_push_array;
} else if (r->builtin == do_match) {
static bool warned = false;
diff --git a/awkgram.y b/awkgram.y
index 1a199469..9e4f82b3 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -1860,6 +1860,7 @@ static const struct token tokentab[] = {
{"dcngettext", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3)|A(4)|A(5), do_dcngettext, 0},
{"default", Op_K_default, LEX_DEFAULT, GAWKX, 0, 0},
{"delete", Op_K_delete, LEX_DELETE, NOT_OLD, 0, 0},
+{"div", Op_builtin, LEX_BUILTIN, GAWKX|A(3), do_div, MPF(div)},
{"do", Op_K_do, LEX_DO, NOT_OLD|BREAK|CONTINUE, 0, 0},
{"else", Op_K_else, LEX_ELSE, 0, 0, 0},
{"eval", Op_symbol, LEX_EVAL, 0, 0, 0},
@@ -3905,7 +3906,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
}
#ifdef HAVE_MPFR
- /* N.B.: There isn't any special processing for an alternate function below */
+ /* N.B.: If necessary, add special processing for alternate builtin, below */
if (do_mpfr && tokentab[idx].ptr2)
r->builtin = tokentab[idx].ptr2;
else
@@ -3934,6 +3935,15 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
arg = subn->nexti;
if (arg->nexti == arg->lasti && arg->nexti->opcode == Op_push)
arg->nexti->opcode = Op_push_arg; /* argument may be array */
+ } else if (r->builtin == do_div
+#ifdef HAVE_MPFR
+ || r->builtin == MPF(div)
+#endif
+ ) {
+ arg = subn->nexti->lasti->nexti->lasti->nexti; /* 3rd arg list */
+ ip = arg->lasti;
+ if (ip->opcode == Op_push)
+ ip->opcode = Op_push_array;
} else if (r->builtin == do_match) {
static bool warned = false;
diff --git a/builtin.c b/builtin.c
index 470f7bbe..6f8aeb36 100644
--- a/builtin.c
+++ b/builtin.c
@@ -3612,6 +3612,72 @@ do_bindtextdomain(int nargs)
return make_string(the_result, strlen(the_result));
}
+/* do_div --- do integer division, return quotient and remainder in dest array */
+
+/*
+ * We define the semantics as:
+ * numerator = int(numerator)
+ * denominator = int(denonmator)
+ * quotient = int(numerator / denomator)
+ * remainder = int(numerator % denomator)
+ */
+
+NODE *
+do_div(int nargs)
+{
+ NODE *numerator, *denominator, *result;
+ double num, denom, quotient, remainder;
+ NODE *sub, **lhs;
+
+ result = POP_PARAM();
+ if (result->type != Node_var_array)
+ fatal(_("div: third argument is not an array"));
+ assoc_clear(result);
+
+ denominator = POP_SCALAR();
+ numerator = POP_SCALAR();
+
+ if (do_lint) {
+ if ((numerator->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("div: received non-numeric first argument"));
+ if ((denominator->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("div: received non-numeric second argument"));
+ }
+
+ (void) force_number(numerator);
+ (void) force_number(denominator);
+ num = double_to_int(get_number_d(numerator));
+ denom = double_to_int(get_number_d(denominator));
+
+ if (denom == 0.0)
+ fatal(_("div: division by zero attempted"));
+
+ quotient = double_to_int(num / denom);
+ /*
+ * FIXME: This code is duplicated, factor it out to a
+ * separate function.
+ */
+#ifdef HAVE_FMOD
+ remainder = fmod(num, denom);
+#else /* ! HAVE_FMOD */
+ (void) modf(num / denom, & remainder);
+ remainder = num - remainder * denom;
+#endif /* ! HAVE_FMOD */
+ remainder = double_to_int(remainder);
+
+ sub = make_string("quotient", 8);
+ lhs = assoc_lookup(result, sub);
+ unref(*lhs);
+ *lhs = make_number((AWKNUM) quotient);
+
+ sub = make_string("remainder", 9);
+ lhs = assoc_lookup(result, sub);
+ unref(*lhs);
+ *lhs = make_number((AWKNUM) remainder);
+
+ return make_number((AWKNUM) 0.0);
+}
+
/* mbc_byte_count --- return number of bytes for corresponding numchars multibyte characters */
diff --git a/doc/ChangeLog b/doc/ChangeLog
index 51704349..f69e401d 100644
--- a/doc/ChangeLog
+++ b/doc/ChangeLog
@@ -1,3 +1,9 @@
+2014-07-10 Arnold D. Robbins <arnold@skeeve.com>
+
+ * gawktexi.in (Numeric Functions): Document new `div()' function.
+ (Arbitrary Precision Integers): Document raison d'etre for div().
+ * gawk.1, awkcard.in: Document `div()'.
+
2014-07-04 Arnold D. Robbins <arnold@skeeve.com>
* gawktexi.in (Bracket Expressions): Add a note about how to
diff --git a/doc/awkcard.in b/doc/awkcard.in
index ca28f0a7..556bdc1e 100644
--- a/doc/awkcard.in
+++ b/doc/awkcard.in
@@ -1609,6 +1609,9 @@ expand;
l lw(2i).
\*(CD\*(FCatan2(\*(FIy\*(FC, \*(FIx\*(FC)\*(FR The arctangent of \*(FIy/x\fP in radians.
\*(FCcos(\*(FIexpr\*(FC)\*(FR The cosine of \*(FIexpr\fP, which is in radians.
+\*(CB\*(FCdiv(\*(FIn\*(FR\*(FC,\*(FI d\*(FR\*(FC,\*(FI res\*(FR\*(FC)\*(FR T{
+Return the result of integer division in \*(FIres\*(FR.\*(CD
+T}
\*(FCexp(\*(FIexpr\*(FC)\*(FR The exponential function (\*(FIe \*(FC^ \*(FIx\*(FR).
\*(FCint(\*(FIexpr\*(FC)\*(FR Truncate to integer.
\*(FClog(\*(FIexpr\*(FC)\*(FR The natural logarithm function (base \*(FIe\^\*(FR).
diff --git a/doc/gawk.1 b/doc/gawk.1
index e3275e87..d5756f08 100644
--- a/doc/gawk.1
+++ b/doc/gawk.1
@@ -13,7 +13,7 @@
. if \w'\(rq' .ds rq "\(rq
. \}
.\}
-.TH GAWK 1 "Apr 17 2014" "Free Software Foundation" "Utility Commands"
+.TH GAWK 1 "Jul 10 2014" "Free Software Foundation" "Utility Commands"
.SH NAME
gawk \- pattern scanning and processing language
.SH SYNOPSIS
@@ -2629,6 +2629,23 @@ Return the cosine of
.IR expr ,
which is in radians.
.TP
+.BI div( num ", " denom ", " result )
+Truncate
+.I num
+and
+.I denom
+to integers. Return the quotient of
+.I num
+divided by
+.I denom
+in \fIresult\fB["quotient"]\fR
+and the remainder in
+in \fIresult\fB["remainder"]\fR.
+This is a
+.I gawk
+extension, primarily of value when working with
+arbitrarily large integers.
+.TP
.BI exp( expr )
The exponential function.
.TP
diff --git a/doc/gawk.info b/doc/gawk.info
index 8326cf82..e7854caf 100644
--- a/doc/gawk.info
+++ b/doc/gawk.info
@@ -11680,6 +11680,20 @@ brackets ([ ]):
`cos(X)'
Return the cosine of X, with X in radians.
+`div(NUMERATOR, DENOMINATOR, RESULT)'
+ Perform integer division, similar to the standard C function of the
+ same name. First, truncate `numerator' and `denominator' to
+ integers. Clear the `result' array, and then set
+ `result["quotient"]' to the result of `numerator / denominator',
+ truncated to an integer, and set `result["remainder"]' to the
+ result of `numerator % denominator', truncated to an integer.
+ This function is primarily intended for use with arbitrary length
+ integers; it avoids creating MPFR arbitrary precision
+ floating-point values (*note Arbitrary Precision Integers::).
+
+ This function is a `gawk' extension. It is not available in
+ compatibility mode (*note Options::).
+
`exp(X)'
Return the exponential of X (`e ^ X') or report an error if X is
out of range. The range of values X can have depends on your
@@ -22082,6 +22096,29 @@ just use the following:
gawk -M 'BEGIN { n = 13; print n % 2 }'
+ When dividing two arbitrary precision integers with either `/' or
+`%', the result is typically an arbitrary precision floating point
+value (unless the denominator evenly divides into the numerator). In
+order to do integer division or remainder with arbitrary precision
+integers, use the built-in `div()' function (*note Numeric Functions::).
+
+ You can simulate the `div()' function in standard `awk' using this
+user-defined function:
+
+ # div --- do integer division
+
+ function div(numerator, denominator, result, i)
+ {
+ split("", result)
+
+ numerator = int(numerator)
+ denominator = int(denominator)
+ result["quotient"] = int(numerator / denominator)
+ result["remainder"] = int(numerator % denominator)
+
+ return 0.0
+ }
+
---------- Footnotes ----------
(1) Weisstein, Eric W. `Sylvester's Sequence'. From MathWorld--A
@@ -32005,6 +32042,7 @@ Index
* display debugger command: Viewing And Changing Data.
(line 8)
* display debugger options: Debugger Info. (line 57)
+* div: Numeric Functions. (line 18)
* division: Arithmetic Ops. (line 44)
* do-while statement: Do Statement. (line 6)
* do-while statement, use of regexps in: Regexp Usage. (line 19)
@@ -32125,10 +32163,10 @@ Index
* exit status, of VMS: VMS Running. (line 29)
* exit the debugger: Miscellaneous Debugger Commands.
(line 99)
-* exp: Numeric Functions. (line 18)
+* exp: Numeric Functions. (line 32)
* expand utility: Very Simple. (line 69)
* Expat XML parser library: gawkextlib. (line 35)
-* exponent: Numeric Functions. (line 18)
+* exponent: Numeric Functions. (line 32)
* expressions: Expressions. (line 6)
* expressions, as patterns: Expression Patterns. (line 6)
* expressions, assignment: Assignment Ops. (line 6)
@@ -32664,7 +32702,7 @@ Index
* installation, VMS: VMS Installation. (line 6)
* installing gawk: Installation. (line 6)
* instruction tracing, in debugger: Debugger Info. (line 89)
-* int: Numeric Functions. (line 23)
+* int: Numeric Functions. (line 37)
* INT signal (MS-Windows): Profiling. (line 214)
* integer array indices: Numeric Array Subscripts.
(line 31)
@@ -32813,9 +32851,9 @@ Index
* localization: I18N and L10N. (line 6)
* localization, See internationalization, localization: I18N and L10N.
(line 6)
-* log: Numeric Functions. (line 30)
+* log: Numeric Functions. (line 44)
* log files, timestamps in: Time Functions. (line 6)
-* logarithm: Numeric Functions. (line 30)
+* logarithm: Numeric Functions. (line 44)
* logical false/true: Truth Values. (line 6)
* logical operators, See Boolean expressions: Boolean Ops. (line 6)
* login information: Passwd Functions. (line 16)
@@ -33272,12 +33310,12 @@ Index
* Rakitzis, Byron: History Sorting. (line 25)
* Ramey, Chet <1>: General Data Types. (line 6)
* Ramey, Chet: Acknowledgments. (line 60)
-* rand: Numeric Functions. (line 34)
+* rand: Numeric Functions. (line 48)
* random numbers, Cliff: Cliff Random Function.
(line 6)
* random numbers, rand()/srand() functions: Numeric Functions.
- (line 34)
-* random numbers, seed of: Numeric Functions. (line 64)
+ (line 48)
+* random numbers, seed of: Numeric Functions. (line 78)
* range expressions (regexps): Bracket Expressions. (line 6)
* range patterns: Ranges. (line 6)
* range patterns, line continuation and: Ranges. (line 65)
@@ -33405,7 +33443,7 @@ Index
* Robbins, Miriam <2>: Getline/Pipe. (line 39)
* Robbins, Miriam: Acknowledgments. (line 82)
* Rommel, Kai Uwe: Contributors. (line 42)
-* round to nearest integer: Numeric Functions. (line 23)
+* round to nearest integer: Numeric Functions. (line 37)
* round() user-defined function: Round Function. (line 16)
* rounding numbers: Round Function. (line 6)
* ROUNDMODE variable: User-modified. (line 128)
@@ -33455,7 +33493,7 @@ Index
* sed utility <2>: Simple Sed. (line 6)
* sed utility: Field Splitting Summary.
(line 46)
-* seeding random number generator: Numeric Functions. (line 64)
+* seeding random number generator: Numeric Functions. (line 78)
* semicolon (;), AWKPATH variable and: PC Using. (line 10)
* semicolon (;), separating statements in actions <1>: Statements.
(line 10)
@@ -33557,8 +33595,8 @@ Index
* SIGUSR1 signal, for dynamic profiling: Profiling. (line 188)
* silent debugger command: Debugger Execution Control.
(line 10)
-* sin: Numeric Functions. (line 75)
-* sine: Numeric Functions. (line 75)
+* sin: Numeric Functions. (line 89)
+* sine: Numeric Functions. (line 89)
* single quote ('): One-shot. (line 15)
* single quote (') in gawk command lines: Long. (line 33)
* single quote ('), in shell commands: Quoting. (line 48)
@@ -33608,10 +33646,10 @@ Index
* sprintf() function, OFMT variable and: User-modified. (line 114)
* sprintf() function, print/printf statements and: Round Function.
(line 6)
-* sqrt: Numeric Functions. (line 78)
+* sqrt: Numeric Functions. (line 92)
* square brackets ([]), regexp operator: Regexp Operators. (line 56)
-* square root: Numeric Functions. (line 78)
-* srand: Numeric Functions. (line 82)
+* square root: Numeric Functions. (line 92)
+* srand: Numeric Functions. (line 96)
* stack frame: Debugging Terms. (line 10)
* Stallman, Richard <1>: Glossary. (line 296)
* Stallman, Richard <2>: Contributors. (line 23)
@@ -34180,325 +34218,325 @@ Node: Functions489801
Node: Built-in490674
Node: Calling Built-in491752
Node: Numeric Functions493740
-Ref: Numeric Functions-Footnote-1497574
-Ref: Numeric Functions-Footnote-2497931
-Ref: Numeric Functions-Footnote-3497979
-Node: String Functions498248
-Ref: String Functions-Footnote-1521259
-Ref: String Functions-Footnote-2521388
-Ref: String Functions-Footnote-3521636
-Node: Gory Details521723
-Ref: table-sub-escapes523392
-Ref: table-sub-posix-92524746
-Ref: table-sub-proposed526097
-Ref: table-posix-sub527451
-Ref: table-gensub-escapes528996
-Ref: Gory Details-Footnote-1530172
-Ref: Gory Details-Footnote-2530223
-Node: I/O Functions530374
-Ref: I/O Functions-Footnote-1537497
-Node: Time Functions537644
-Ref: Time Functions-Footnote-1548108
-Ref: Time Functions-Footnote-2548176
-Ref: Time Functions-Footnote-3548334
-Ref: Time Functions-Footnote-4548445
-Ref: Time Functions-Footnote-5548557
-Ref: Time Functions-Footnote-6548784
-Node: Bitwise Functions549050
-Ref: table-bitwise-ops549612
-Ref: Bitwise Functions-Footnote-1553857
-Node: Type Functions554041
-Node: I18N Functions555183
-Node: User-defined556828
-Node: Definition Syntax557632
-Ref: Definition Syntax-Footnote-1562811
-Node: Function Example562880
-Ref: Function Example-Footnote-1565524
-Node: Function Caveats565546
-Node: Calling A Function566064
-Node: Variable Scope567019
-Node: Pass By Value/Reference570007
-Node: Return Statement573515
-Node: Dynamic Typing576499
-Node: Indirect Calls577428
-Node: Functions Summary587141
-Node: Library Functions589680
-Ref: Library Functions-Footnote-1593298
-Ref: Library Functions-Footnote-2593441
-Node: Library Names593612
-Ref: Library Names-Footnote-1597085
-Ref: Library Names-Footnote-2597305
-Node: General Functions597391
-Node: Strtonum Function598419
-Node: Assert Function601199
-Node: Round Function604525
-Node: Cliff Random Function606066
-Node: Ordinal Functions607082
-Ref: Ordinal Functions-Footnote-1610159
-Ref: Ordinal Functions-Footnote-2610411
-Node: Join Function610622
-Ref: Join Function-Footnote-1612393
-Node: Getlocaltime Function612593
-Node: Readfile Function616329
-Node: Data File Management618168
-Node: Filetrans Function618800
-Node: Rewind Function622869
-Node: File Checking624256
-Ref: File Checking-Footnote-1625388
-Node: Empty Files625589
-Node: Ignoring Assigns627568
-Node: Getopt Function629122
-Ref: Getopt Function-Footnote-1640425
-Node: Passwd Functions640628
-Ref: Passwd Functions-Footnote-1649607
-Node: Group Functions649695
-Ref: Group Functions-Footnote-1657636
-Node: Walking Arrays657849
-Node: Library Functions Summary659452
-Node: Library exercises660840
-Node: Sample Programs662120
-Node: Running Examples662890
-Node: Clones663618
-Node: Cut Program664842
-Node: Egrep Program674710
-Ref: Egrep Program-Footnote-1682681
-Node: Id Program682791
-Node: Split Program686455
-Ref: Split Program-Footnote-1689993
-Node: Tee Program690121
-Node: Uniq Program692928
-Node: Wc Program700358
-Ref: Wc Program-Footnote-1704623
-Node: Miscellaneous Programs704715
-Node: Dupword Program705928
-Node: Alarm Program707959
-Node: Translate Program712773
-Ref: Translate Program-Footnote-1717164
-Ref: Translate Program-Footnote-2717434
-Node: Labels Program717568
-Ref: Labels Program-Footnote-1720939
-Node: Word Sorting721023
-Node: History Sorting725066
-Node: Extract Program726902
-Node: Simple Sed734438
-Node: Igawk Program737500
-Ref: Igawk Program-Footnote-1751811
-Ref: Igawk Program-Footnote-2752012
-Node: Anagram Program752150
-Node: Signature Program755218
-Node: Programs Summary756465
-Node: Programs Exercises757680
-Node: Advanced Features761331
-Node: Nondecimal Data763279
-Node: Array Sorting764856
-Node: Controlling Array Traversal765553
-Node: Array Sorting Functions773833
-Ref: Array Sorting Functions-Footnote-1777740
-Node: Two-way I/O777934
-Ref: Two-way I/O-Footnote-1783450
-Node: TCP/IP Networking783532
-Node: Profiling786376
-Node: Advanced Features Summary793927
-Node: Internationalization795791
-Node: I18N and L10N797271
-Node: Explaining gettext797957
-Ref: Explaining gettext-Footnote-1803097
-Ref: Explaining gettext-Footnote-2803281
-Node: Programmer i18n803446
-Node: Translator i18n807671
-Node: String Extraction808465
-Ref: String Extraction-Footnote-1809426
-Node: Printf Ordering809512
-Ref: Printf Ordering-Footnote-1812294
-Node: I18N Portability812358
-Ref: I18N Portability-Footnote-1814807
-Node: I18N Example814870
-Ref: I18N Example-Footnote-1817592
-Node: Gawk I18N817664
-Node: I18N Summary818302
-Node: Debugger819641
-Node: Debugging820663
-Node: Debugging Concepts821104
-Node: Debugging Terms822960
-Node: Awk Debugging825557
-Node: Sample Debugging Session826449
-Node: Debugger Invocation826969
-Node: Finding The Bug828302
-Node: List of Debugger Commands834784
-Node: Breakpoint Control836116
-Node: Debugger Execution Control839780
-Node: Viewing And Changing Data843140
-Node: Execution Stack846498
-Node: Debugger Info848011
-Node: Miscellaneous Debugger Commands852005
-Node: Readline Support857189
-Node: Limitations858081
-Node: Debugging Summary860355
-Node: Arbitrary Precision Arithmetic861519
-Node: Computer Arithmetic862848
-Ref: Computer Arithmetic-Footnote-1867235
-Node: Math Definitions867292
-Ref: table-ieee-formats870176
-Node: MPFR features870680
-Node: FP Math Caution872322
-Ref: FP Math Caution-Footnote-1873363
-Node: Inexactness of computations873732
-Node: Inexact representation874680
-Node: Comparing FP Values876035
-Node: Errors accumulate876999
-Node: Getting Accuracy878432
-Node: Try To Round881091
-Node: Setting precision881990
-Ref: table-predefined-precision-strings882672
-Node: Setting the rounding mode884465
-Ref: table-gawk-rounding-modes884829
-Ref: Setting the rounding mode-Footnote-1888283
-Node: Arbitrary Precision Integers888462
-Ref: Arbitrary Precision Integers-Footnote-1891465
-Node: POSIX Floating Point Problems891614
-Ref: POSIX Floating Point Problems-Footnote-1895490
-Node: Floating point summary895528
-Node: Dynamic Extensions897745
-Node: Extension Intro899297
-Node: Plugin License900562
-Node: Extension Mechanism Outline901247
-Ref: figure-load-extension901671
-Ref: figure-load-new-function903156
-Ref: figure-call-new-function904158
-Node: Extension API Description906142
-Node: Extension API Functions Introduction907592
-Node: General Data Types912457
-Ref: General Data Types-Footnote-1918150
-Node: Requesting Values918449
-Ref: table-value-types-returned919186
-Node: Memory Allocation Functions920144
-Ref: Memory Allocation Functions-Footnote-1922891
-Node: Constructor Functions922987
-Node: Registration Functions924745
-Node: Extension Functions925430
-Node: Exit Callback Functions927732
-Node: Extension Version String928981
-Node: Input Parsers929631
-Node: Output Wrappers939434
-Node: Two-way processors943950
-Node: Printing Messages946154
-Ref: Printing Messages-Footnote-1947231
-Node: Updating `ERRNO'947383
-Node: Accessing Parameters948122
-Node: Symbol Table Access949352
-Node: Symbol table by name949866
-Node: Symbol table by cookie951842
-Ref: Symbol table by cookie-Footnote-1955975
-Node: Cached values956038
-Ref: Cached values-Footnote-1959542
-Node: Array Manipulation959633
-Ref: Array Manipulation-Footnote-1960731
-Node: Array Data Types960770
-Ref: Array Data Types-Footnote-1963473
-Node: Array Functions963565
-Node: Flattening Arrays967439
-Node: Creating Arrays974291
-Node: Extension API Variables979022
-Node: Extension Versioning979658
-Node: Extension API Informational Variables981559
-Node: Extension API Boilerplate982645
-Node: Finding Extensions986449
-Node: Extension Example987009
-Node: Internal File Description987739
-Node: Internal File Ops991830
-Ref: Internal File Ops-Footnote-11003262
-Node: Using Internal File Ops1003402
-Ref: Using Internal File Ops-Footnote-11005749
-Node: Extension Samples1006017
-Node: Extension Sample File Functions1007541
-Node: Extension Sample Fnmatch1015109
-Node: Extension Sample Fork1016591
-Node: Extension Sample Inplace1017804
-Node: Extension Sample Ord1019479
-Node: Extension Sample Readdir1020315
-Ref: table-readdir-file-types1021171
-Node: Extension Sample Revout1021970
-Node: Extension Sample Rev2way1022561
-Node: Extension Sample Read write array1023302
-Node: Extension Sample Readfile1025181
-Node: Extension Sample API Tests1026281
-Node: Extension Sample Time1026806
-Node: gawkextlib1028121
-Node: Extension summary1030934
-Node: Extension Exercises1034627
-Node: Language History1035349
-Node: V7/SVR3.11036992
-Node: SVR41039312
-Node: POSIX1040754
-Node: BTL1042140
-Node: POSIX/GNU1042874
-Node: Feature History1048473
-Node: Common Extensions1061603
-Node: Ranges and Locales1062915
-Ref: Ranges and Locales-Footnote-11067532
-Ref: Ranges and Locales-Footnote-21067559
-Ref: Ranges and Locales-Footnote-31067793
-Node: Contributors1068014
-Node: History summary1073439
-Node: Installation1074808
-Node: Gawk Distribution1075759
-Node: Getting1076243
-Node: Extracting1077067
-Node: Distribution contents1078709
-Node: Unix Installation1084479
-Node: Quick Installation1085096
-Node: Additional Configuration Options1087538
-Node: Configuration Philosophy1089276
-Node: Non-Unix Installation1091627
-Node: PC Installation1092085
-Node: PC Binary Installation1093396
-Node: PC Compiling1095244
-Ref: PC Compiling-Footnote-11098243
-Node: PC Testing1098348
-Node: PC Using1099524
-Node: Cygwin1103682
-Node: MSYS1104491
-Node: VMS Installation1105005
-Node: VMS Compilation1105801
-Ref: VMS Compilation-Footnote-11107023
-Node: VMS Dynamic Extensions1107081
-Node: VMS Installation Details1108454
-Node: VMS Running1110706
-Node: VMS GNV1113540
-Node: VMS Old Gawk1114263
-Node: Bugs1114733
-Node: Other Versions1118737
-Node: Installation summary1124992
-Node: Notes1126048
-Node: Compatibility Mode1126913
-Node: Additions1127695
-Node: Accessing The Source1128620
-Node: Adding Code1130056
-Node: New Ports1136234
-Node: Derived Files1140715
-Ref: Derived Files-Footnote-11145796
-Ref: Derived Files-Footnote-21145830
-Ref: Derived Files-Footnote-31146426
-Node: Future Extensions1146540
-Node: Implementation Limitations1147146
-Node: Extension Design1148394
-Node: Old Extension Problems1149548
-Ref: Old Extension Problems-Footnote-11151065
-Node: Extension New Mechanism Goals1151122
-Ref: Extension New Mechanism Goals-Footnote-11154482
-Node: Extension Other Design Decisions1154671
-Node: Extension Future Growth1156777
-Node: Old Extension Mechanism1157613
-Node: Notes summary1159375
-Node: Basic Concepts1160561
-Node: Basic High Level1161242
-Ref: figure-general-flow1161514
-Ref: figure-process-flow1162113
-Ref: Basic High Level-Footnote-11165342
-Node: Basic Data Typing1165527
-Node: Glossary1168855
-Node: Copying1194007
-Node: GNU Free Documentation License1231563
-Node: Index1256699
+Ref: Numeric Functions-Footnote-1498318
+Ref: Numeric Functions-Footnote-2498675
+Ref: Numeric Functions-Footnote-3498723
+Node: String Functions498992
+Ref: String Functions-Footnote-1522003
+Ref: String Functions-Footnote-2522132
+Ref: String Functions-Footnote-3522380
+Node: Gory Details522467
+Ref: table-sub-escapes524136
+Ref: table-sub-posix-92525490
+Ref: table-sub-proposed526841
+Ref: table-posix-sub528195
+Ref: table-gensub-escapes529740
+Ref: Gory Details-Footnote-1530916
+Ref: Gory Details-Footnote-2530967
+Node: I/O Functions531118
+Ref: I/O Functions-Footnote-1538241
+Node: Time Functions538388
+Ref: Time Functions-Footnote-1548852
+Ref: Time Functions-Footnote-2548920
+Ref: Time Functions-Footnote-3549078
+Ref: Time Functions-Footnote-4549189
+Ref: Time Functions-Footnote-5549301
+Ref: Time Functions-Footnote-6549528
+Node: Bitwise Functions549794
+Ref: table-bitwise-ops550356
+Ref: Bitwise Functions-Footnote-1554601
+Node: Type Functions554785
+Node: I18N Functions555927
+Node: User-defined557572
+Node: Definition Syntax558376
+Ref: Definition Syntax-Footnote-1563555
+Node: Function Example563624
+Ref: Function Example-Footnote-1566268
+Node: Function Caveats566290
+Node: Calling A Function566808
+Node: Variable Scope567763
+Node: Pass By Value/Reference570751
+Node: Return Statement574259
+Node: Dynamic Typing577243
+Node: Indirect Calls578172
+Node: Functions Summary587885
+Node: Library Functions590424
+Ref: Library Functions-Footnote-1594042
+Ref: Library Functions-Footnote-2594185
+Node: Library Names594356
+Ref: Library Names-Footnote-1597829
+Ref: Library Names-Footnote-2598049
+Node: General Functions598135
+Node: Strtonum Function599163
+Node: Assert Function601943
+Node: Round Function605269
+Node: Cliff Random Function606810
+Node: Ordinal Functions607826
+Ref: Ordinal Functions-Footnote-1610903
+Ref: Ordinal Functions-Footnote-2611155
+Node: Join Function611366
+Ref: Join Function-Footnote-1613137
+Node: Getlocaltime Function613337
+Node: Readfile Function617073
+Node: Data File Management618912
+Node: Filetrans Function619544
+Node: Rewind Function623613
+Node: File Checking625000
+Ref: File Checking-Footnote-1626132
+Node: Empty Files626333
+Node: Ignoring Assigns628312
+Node: Getopt Function629866
+Ref: Getopt Function-Footnote-1641169
+Node: Passwd Functions641372
+Ref: Passwd Functions-Footnote-1650351
+Node: Group Functions650439
+Ref: Group Functions-Footnote-1658380
+Node: Walking Arrays658593
+Node: Library Functions Summary660196
+Node: Library exercises661584
+Node: Sample Programs662864
+Node: Running Examples663634
+Node: Clones664362
+Node: Cut Program665586
+Node: Egrep Program675454
+Ref: Egrep Program-Footnote-1683425
+Node: Id Program683535
+Node: Split Program687199
+Ref: Split Program-Footnote-1690737
+Node: Tee Program690865
+Node: Uniq Program693672
+Node: Wc Program701102
+Ref: Wc Program-Footnote-1705367
+Node: Miscellaneous Programs705459
+Node: Dupword Program706672
+Node: Alarm Program708703
+Node: Translate Program713517
+Ref: Translate Program-Footnote-1717908
+Ref: Translate Program-Footnote-2718178
+Node: Labels Program718312
+Ref: Labels Program-Footnote-1721683
+Node: Word Sorting721767
+Node: History Sorting725810
+Node: Extract Program727646
+Node: Simple Sed735182
+Node: Igawk Program738244
+Ref: Igawk Program-Footnote-1752555
+Ref: Igawk Program-Footnote-2752756
+Node: Anagram Program752894
+Node: Signature Program755962
+Node: Programs Summary757209
+Node: Programs Exercises758424
+Node: Advanced Features762075
+Node: Nondecimal Data764023
+Node: Array Sorting765600
+Node: Controlling Array Traversal766297
+Node: Array Sorting Functions774577
+Ref: Array Sorting Functions-Footnote-1778484
+Node: Two-way I/O778678
+Ref: Two-way I/O-Footnote-1784194
+Node: TCP/IP Networking784276
+Node: Profiling787120
+Node: Advanced Features Summary794671
+Node: Internationalization796535
+Node: I18N and L10N798015
+Node: Explaining gettext798701
+Ref: Explaining gettext-Footnote-1803841
+Ref: Explaining gettext-Footnote-2804025
+Node: Programmer i18n804190
+Node: Translator i18n808415
+Node: String Extraction809209
+Ref: String Extraction-Footnote-1810170
+Node: Printf Ordering810256
+Ref: Printf Ordering-Footnote-1813038
+Node: I18N Portability813102
+Ref: I18N Portability-Footnote-1815551
+Node: I18N Example815614
+Ref: I18N Example-Footnote-1818336
+Node: Gawk I18N818408
+Node: I18N Summary819046
+Node: Debugger820385
+Node: Debugging821407
+Node: Debugging Concepts821848
+Node: Debugging Terms823704
+Node: Awk Debugging826301
+Node: Sample Debugging Session827193
+Node: Debugger Invocation827713
+Node: Finding The Bug829046
+Node: List of Debugger Commands835528
+Node: Breakpoint Control836860
+Node: Debugger Execution Control840524
+Node: Viewing And Changing Data843884
+Node: Execution Stack847242
+Node: Debugger Info848755
+Node: Miscellaneous Debugger Commands852749
+Node: Readline Support857933
+Node: Limitations858825
+Node: Debugging Summary861099
+Node: Arbitrary Precision Arithmetic862263
+Node: Computer Arithmetic863592
+Ref: Computer Arithmetic-Footnote-1867979
+Node: Math Definitions868036
+Ref: table-ieee-formats870920
+Node: MPFR features871424
+Node: FP Math Caution873066
+Ref: FP Math Caution-Footnote-1874107
+Node: Inexactness of computations874476
+Node: Inexact representation875424
+Node: Comparing FP Values876779
+Node: Errors accumulate877743
+Node: Getting Accuracy879176
+Node: Try To Round881835
+Node: Setting precision882734
+Ref: table-predefined-precision-strings883416
+Node: Setting the rounding mode885209
+Ref: table-gawk-rounding-modes885573
+Ref: Setting the rounding mode-Footnote-1889027
+Node: Arbitrary Precision Integers889206
+Ref: Arbitrary Precision Integers-Footnote-1893001
+Node: POSIX Floating Point Problems893150
+Ref: POSIX Floating Point Problems-Footnote-1897026
+Node: Floating point summary897064
+Node: Dynamic Extensions899281
+Node: Extension Intro900833
+Node: Plugin License902098
+Node: Extension Mechanism Outline902783
+Ref: figure-load-extension903207
+Ref: figure-load-new-function904692
+Ref: figure-call-new-function905694
+Node: Extension API Description907678
+Node: Extension API Functions Introduction909128
+Node: General Data Types913993
+Ref: General Data Types-Footnote-1919686
+Node: Requesting Values919985
+Ref: table-value-types-returned920722
+Node: Memory Allocation Functions921680
+Ref: Memory Allocation Functions-Footnote-1924427
+Node: Constructor Functions924523
+Node: Registration Functions926281
+Node: Extension Functions926966
+Node: Exit Callback Functions929268
+Node: Extension Version String930517
+Node: Input Parsers931167
+Node: Output Wrappers940970
+Node: Two-way processors945486
+Node: Printing Messages947690
+Ref: Printing Messages-Footnote-1948767
+Node: Updating `ERRNO'948919
+Node: Accessing Parameters949658
+Node: Symbol Table Access950888
+Node: Symbol table by name951402
+Node: Symbol table by cookie953378
+Ref: Symbol table by cookie-Footnote-1957511
+Node: Cached values957574
+Ref: Cached values-Footnote-1961078
+Node: Array Manipulation961169
+Ref: Array Manipulation-Footnote-1962267
+Node: Array Data Types962306
+Ref: Array Data Types-Footnote-1965009
+Node: Array Functions965101
+Node: Flattening Arrays968975
+Node: Creating Arrays975827
+Node: Extension API Variables980558
+Node: Extension Versioning981194
+Node: Extension API Informational Variables983095
+Node: Extension API Boilerplate984181
+Node: Finding Extensions987985
+Node: Extension Example988545
+Node: Internal File Description989275
+Node: Internal File Ops993366
+Ref: Internal File Ops-Footnote-11004798
+Node: Using Internal File Ops1004938
+Ref: Using Internal File Ops-Footnote-11007285
+Node: Extension Samples1007553
+Node: Extension Sample File Functions1009077
+Node: Extension Sample Fnmatch1016645
+Node: Extension Sample Fork1018127
+Node: Extension Sample Inplace1019340
+Node: Extension Sample Ord1021015
+Node: Extension Sample Readdir1021851
+Ref: table-readdir-file-types1022707
+Node: Extension Sample Revout1023506
+Node: Extension Sample Rev2way1024097
+Node: Extension Sample Read write array1024838
+Node: Extension Sample Readfile1026717
+Node: Extension Sample API Tests1027817
+Node: Extension Sample Time1028342
+Node: gawkextlib1029657
+Node: Extension summary1032470
+Node: Extension Exercises1036163
+Node: Language History1036885
+Node: V7/SVR3.11038528
+Node: SVR41040848
+Node: POSIX1042290
+Node: BTL1043676
+Node: POSIX/GNU1044410
+Node: Feature History1050009
+Node: Common Extensions1063139
+Node: Ranges and Locales1064451
+Ref: Ranges and Locales-Footnote-11069068
+Ref: Ranges and Locales-Footnote-21069095
+Ref: Ranges and Locales-Footnote-31069329
+Node: Contributors1069550
+Node: History summary1074975
+Node: Installation1076344
+Node: Gawk Distribution1077295
+Node: Getting1077779
+Node: Extracting1078603
+Node: Distribution contents1080245
+Node: Unix Installation1086015
+Node: Quick Installation1086632
+Node: Additional Configuration Options1089074
+Node: Configuration Philosophy1090812
+Node: Non-Unix Installation1093163
+Node: PC Installation1093621
+Node: PC Binary Installation1094932
+Node: PC Compiling1096780
+Ref: PC Compiling-Footnote-11099779
+Node: PC Testing1099884
+Node: PC Using1101060
+Node: Cygwin1105218
+Node: MSYS1106027
+Node: VMS Installation1106541
+Node: VMS Compilation1107337
+Ref: VMS Compilation-Footnote-11108559
+Node: VMS Dynamic Extensions1108617
+Node: VMS Installation Details1109990
+Node: VMS Running1112242
+Node: VMS GNV1115076
+Node: VMS Old Gawk1115799
+Node: Bugs1116269
+Node: Other Versions1120273
+Node: Installation summary1126528
+Node: Notes1127584
+Node: Compatibility Mode1128449
+Node: Additions1129231
+Node: Accessing The Source1130156
+Node: Adding Code1131592
+Node: New Ports1137770
+Node: Derived Files1142251
+Ref: Derived Files-Footnote-11147332
+Ref: Derived Files-Footnote-21147366
+Ref: Derived Files-Footnote-31147962
+Node: Future Extensions1148076
+Node: Implementation Limitations1148682
+Node: Extension Design1149930
+Node: Old Extension Problems1151084
+Ref: Old Extension Problems-Footnote-11152601
+Node: Extension New Mechanism Goals1152658
+Ref: Extension New Mechanism Goals-Footnote-11156018
+Node: Extension Other Design Decisions1156207
+Node: Extension Future Growth1158313
+Node: Old Extension Mechanism1159149
+Node: Notes summary1160911
+Node: Basic Concepts1162097
+Node: Basic High Level1162778
+Ref: figure-general-flow1163050
+Ref: figure-process-flow1163649
+Ref: Basic High Level-Footnote-11166878
+Node: Basic Data Typing1167063
+Node: Glossary1170391
+Node: Copying1195543
+Node: GNU Free Documentation License1233099
+Node: Index1258235

End Tag Table
diff --git a/doc/gawk.texi b/doc/gawk.texi
index 3bfeb3f6..d6db2018 100644
--- a/doc/gawk.texi
+++ b/doc/gawk.texi
@@ -51,7 +51,7 @@
@c applies to and all the info about who's publishing this edition
@c These apply across the board.
-@set UPDATE-MONTH June, 2014
+@set UPDATE-MONTH July, 2014
@set VERSION 4.1
@set PATCHLEVEL 1
@@ -16614,6 +16614,22 @@ You can use @samp{pi = atan2(0, -1)} to retrieve the value of
@cindex cosine
Return the cosine of @var{x}, with @var{x} in radians.
+@item @code{div(@var{numerator}, @var{denominator}, @var{result})}
+@cindexawkfunc{div}
+@cindex div
+Perform integer division, similar to the standard C function of the
+same name. First, truncate @code{numerator} and @code{denominator}
+to integers. Clear the @code{result} array, and then set
+@code{result["quotient"]} to the result of @samp{numerator / denominator},
+truncated to an integer, and set @code{result["remainder"]} to the result
+of @samp{numerator % denominator}, truncated to an integer.
+This function is primarily intended for use with arbitrary length
+integers; it avoids creating MPFR arbitrary precision floating-point
+values (@pxref{Arbitrary Precision Integers}).
+
+This function is a @code{gawk} extension. It is not available in
+compatibility mode (@pxref{Options}).
+
@item @code{exp(@var{x})}
@cindexawkfunc{exp}
@cindex exponent
@@ -30523,6 +30539,32 @@ to just use the following:
gawk -M 'BEGIN @{ n = 13; print n % 2 @}'
@end example
+When dividing two arbitrary precision integers with either
+@samp{/} or @samp{%}, the result is typically an arbitrary
+precision floating point value (unless the denominator evenly
+divides into the numerator). In order to do integer division
+or remainder with arbitrary precision integers, use the built-in
+@code{div()} function (@pxref{Numeric Functions}).
+
+You can simulate the @code{div()} function in standard @command{awk}
+using this user-defined function:
+
+@example
+# div --- do integer division
+
+function div(numerator, denominator, result, i)
+@{
+ split("", result)
+
+ numerator = int(numerator)
+ denominator = int(denominator)
+ result["quotient"] = int(numerator / denominator)
+ result["remainder"] = int(numerator % denominator)
+
+ return 0.0
+@}
+@end example
+
@node POSIX Floating Point Problems
@section Standards Versus Existing Practice
diff --git a/doc/gawktexi.in b/doc/gawktexi.in
index ea28861e..2d749e48 100644
--- a/doc/gawktexi.in
+++ b/doc/gawktexi.in
@@ -46,7 +46,7 @@
@c applies to and all the info about who's publishing this edition
@c These apply across the board.
-@set UPDATE-MONTH June, 2014
+@set UPDATE-MONTH July, 2014
@set VERSION 4.1
@set PATCHLEVEL 1
@@ -15919,6 +15919,22 @@ You can use @samp{pi = atan2(0, -1)} to retrieve the value of
@cindex cosine
Return the cosine of @var{x}, with @var{x} in radians.
+@item @code{div(@var{numerator}, @var{denominator}, @var{result})}
+@cindexawkfunc{div}
+@cindex div
+Perform integer division, similar to the standard C function of the
+same name. First, truncate @code{numerator} and @code{denominator}
+to integers. Clear the @code{result} array, and then set
+@code{result["quotient"]} to the result of @samp{numerator / denominator},
+truncated to an integer, and set @code{result["remainder"]} to the result
+of @samp{numerator % denominator}, truncated to an integer.
+This function is primarily intended for use with arbitrary length
+integers; it avoids creating MPFR arbitrary precision floating-point
+values (@pxref{Arbitrary Precision Integers}).
+
+This function is a @code{gawk} extension. It is not available in
+compatibility mode (@pxref{Options}).
+
@item @code{exp(@var{x})}
@cindexawkfunc{exp}
@cindex exponent
@@ -29638,6 +29654,32 @@ to just use the following:
gawk -M 'BEGIN @{ n = 13; print n % 2 @}'
@end example
+When dividing two arbitrary precision integers with either
+@samp{/} or @samp{%}, the result is typically an arbitrary
+precision floating point value (unless the denominator evenly
+divides into the numerator). In order to do integer division
+or remainder with arbitrary precision integers, use the built-in
+@code{div()} function (@pxref{Numeric Functions}).
+
+You can simulate the @code{div()} function in standard @command{awk}
+using this user-defined function:
+
+@example
+# div --- do integer division
+
+function div(numerator, denominator, result, i)
+@{
+ split("", result)
+
+ numerator = int(numerator)
+ denominator = int(denominator)
+ result["quotient"] = int(numerator / denominator)
+ result["remainder"] = int(numerator % denominator)
+
+ return 0.0
+@}
+@end example
+
@node POSIX Floating Point Problems
@section Standards Versus Existing Practice
diff --git a/mpfr.c b/mpfr.c
index 393a2b1a..e9c9d154 100644
--- a/mpfr.c
+++ b/mpfr.c
@@ -1166,6 +1166,93 @@ do_mpfr_srand(int nargs)
return res;
}
+/* do_mpfr_div --- do integer division, return quotient and remainder in dest array */
+
+/*
+ * We define the semantics as:
+ * numerator = int(numerator)
+ * denominator = int(denonmator)
+ * quotient = int(numerator / denomator)
+ * remainder = int(numerator % denomator)
+ */
+
+NODE *
+do_mpfr_div(int nargs)
+{
+ NODE *numerator, *denominator, *result;
+ NODE *num, *denom;
+ NODE *quotient, *remainder;
+ NODE *sub, **lhs;
+
+ result = POP_PARAM();
+ if (result->type != Node_var_array)
+ fatal(_("div: third argument is not an array"));
+ assoc_clear(result);
+
+ denominator = POP_SCALAR();
+ numerator = POP_SCALAR();
+
+ if (do_lint) {
+ if ((numerator->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("div: received non-numeric first argument"));
+ if ((denominator->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("div: received non-numeric second argument"));
+ }
+
+ (void) force_number(numerator);
+ (void) force_number(denominator);
+
+ /* convert numerator and denominator to integer */
+ if (is_mpg_integer(numerator)) {
+ num = mpg_integer();
+ mpz_set(num->mpg_i, numerator->mpg_i);
+ } else {
+ if (! mpfr_number_p(numerator->mpg_numbr)) {
+ /* [+-]inf or NaN */
+ return numerator;
+ }
+
+ num = mpg_integer();
+ mpfr_get_z(num->mpg_i, numerator->mpg_numbr, MPFR_RNDZ);
+ }
+
+ if (is_mpg_integer(denominator)) {
+ denom = mpg_integer();
+ mpz_set(denom->mpg_i, denominator->mpg_i);
+ } else {
+ if (! mpfr_number_p(denominator->mpg_numbr)) {
+ /* [+-]inf or NaN */
+ return denominator;
+ }
+
+ denom = mpg_integer();
+ mpfr_get_z(denom->mpg_i, denominator->mpg_numbr, MPFR_RNDZ);
+ }
+
+ if (mpz_sgn(denom->mpg_i) == 0)
+ fatal(_("div: division by zero attempted"));
+
+ quotient = mpg_integer();
+ remainder = mpg_integer();
+
+ /* do the division */
+ mpz_tdiv_qr(quotient->mpg_i, remainder->mpg_i, num->mpg_i, denom->mpg_i);
+ unref(num);
+ unref(denom);
+
+ sub = make_string("quotient", 8);
+ lhs = assoc_lookup(result, sub);
+ unref(*lhs);
+ *lhs = quotient;
+
+ sub = make_string("remainder", 9);
+ lhs = assoc_lookup(result, sub);
+ unref(*lhs);
+ *lhs = remainder;
+
+ return make_number((AWKNUM) 0.0);
+}
+
/*
* mpg_tofloat --- convert an arbitrary-precision integer operand to
* a float without loss of precision. It is assumed that the