From 4a9e7da4421619ed7a0ab9a02d808b77df93e867 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 15:02:07 -0400 Subject: modernize dbltobig() --- big.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'big.c') diff --git a/big.c b/big.c index 1c7aac3..1e4eb33 100644 --- a/big.c +++ b/big.c @@ -638,26 +638,31 @@ double bigtodbl(word x) /* note: can return oo, -oo but is used without surrounding sto_/set)dbl() only in compare() */ -word dbltobig(x) /* entier */ -double x; +/* entier */ +word dbltobig(double x) { word s = (x < 0); word r = make(INT, 0, 0); word *p = &r; double y = floor(x); -/*if(fabs(y-x+1.0)<1e-9)y += 1.0; /* trick due to Peter Bartke, see note */ + for (y = fabs(y);;) { double n = fmod(y, (double)IBASE); digit(*p) = (word) n; y = (y - n) / (double)IBASE; - if (y > 0.0) - rest(*p) = make(INT, 0, 0), p = &rest(*p); - else + if (y > 0.0) { + rest(*p) = make(INT, 0, 0); + p = &rest(*p); + } else { break; + } } - if (s) + + if (s) { digit(r) = SIGNBIT | digit(r); - return (r); + } + + return r; } /* produces junk in low order digits if x exceeds range in which integer -- cgit v1.2.1