1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg@acm.org). */
36 g_ddfmt(buf
, dd
, ndig
, bufsize
) char *buf
; double *dd
; int ndig
; size_t bufsize
;
38 g_ddfmt(char *buf
, double *dd
, int ndig
, size_t bufsize
)
43 ULong
*L
, bits0
[4], *bits
, *zx
;
44 int bx
, by
, decpt
, ex
, ey
, i
, j
, mode
;
47 #ifdef Honor_FLT_ROUNDS /*{{*/
49 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
50 Rounding
= Flt_Rounds
;
53 switch(fegetround()) {
54 case FE_TOWARDZERO
: Rounding
= 0; break;
55 case FE_UPWARD
: Rounding
= 2; break;
56 case FE_DOWNWARD
: Rounding
= 3;
60 #define Rounding FPI_Round_near
63 if (bufsize
< 10 || bufsize
< ndig
+ 8)
67 if ((L
[_0
] & 0x7ff00000L
) == 0x7ff00000L
) {
69 if (L
[_0
] & 0xfffff || L
[_1
]) {
71 return strcp(buf
, "NaN");
73 if ((L
[2+_0
] & 0x7ff00000) == 0x7ff00000) {
74 if (L
[2+_0
] & 0xfffff || L
[2+_1
])
76 if ((L
[_0
] ^ L
[2+_0
]) & 0x80000000L
)
77 goto nanret
; /* Infinity - Infinity */
81 if (L
[_0
] & 0x80000000L
)
83 return strcp(b
, "Infinity");
85 if ((L
[2+_0
] & 0x7ff00000) == 0x7ff00000) {
87 if (L
[_0
] & 0xfffff || L
[_1
])
91 if (dd
[0] + dd
[1] == 0.) {
93 #ifndef IGNORE_ZERO_SIGN
94 if (L
[_0
] & L
[2+_0
] & 0x80000000L
)
101 if ((L
[_0
] & 0x7ff00000L
) < (L
[2+_0
] & 0x7ff00000L
)) {
107 z
= d2b(dd
[0], &ex
, &bx
);
111 y
= d2b(dd
[1], &ey
, &by
);
112 if ( (i
= ex
- ey
) !=0) {
120 if ((L
[_0
] ^ L
[2+_0
]) & 0x80000000L
) {
122 if (L
[_0
] & 0x80000000L
)
123 z
->sign
= 1 - z
->sign
;
127 if (L
[_0
] & 0x80000000L
)
134 for(i
= 0; !*zx
; zx
++)
141 fpi
.nbits
= z
->wds
* 32 - hi0bits(z
->x
[j
= z
->wds
-1]);
142 if (fpi
.nbits
< 106) {
145 for(i
= 0; i
<= j
; i
++)
154 if (bufsize
< (int)(fpi
.nbits
* .301029995664) + 10) {
160 fpi
.emin
= 1-1023-53+1;
161 fpi
.emax
= 2046-1023-106+1;
162 fpi
.rounding
= Rounding
;
163 fpi
.sudden_underflow
= 0;
165 s
= gdtoa(&fpi
, ex
, bits
, &i
, mode
, ndig
, &decpt
, &se
);
166 b
= g__fmt(buf
, s
, se
, decpt
, z
->sign
, bufsize
);