Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /**************************************************************** |
michael@0 | 2 | * |
michael@0 | 3 | * The author of this software is David M. Gay. |
michael@0 | 4 | * |
michael@0 | 5 | * Copyright (c) 1991, 1996 by Lucent Technologies. |
michael@0 | 6 | * |
michael@0 | 7 | * Permission to use, copy, modify, and distribute this software for any |
michael@0 | 8 | * purpose without fee is hereby granted, provided that this entire notice |
michael@0 | 9 | * is included in all copies of any software which is or includes a copy |
michael@0 | 10 | * or modification of this software and in all copies of the supporting |
michael@0 | 11 | * documentation for such software. |
michael@0 | 12 | * |
michael@0 | 13 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED |
michael@0 | 14 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY |
michael@0 | 15 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY |
michael@0 | 16 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. |
michael@0 | 17 | * |
michael@0 | 18 | ***************************************************************/ |
michael@0 | 19 | |
michael@0 | 20 | /* g_fmt(buf,x) stores the closest decimal approximation to x in buf; |
michael@0 | 21 | * it suffices to declare buf |
michael@0 | 22 | * char buf[32]; |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | #include "dmg_fp.h" |
michael@0 | 26 | |
michael@0 | 27 | namespace dmg_fp { |
michael@0 | 28 | |
michael@0 | 29 | char * |
michael@0 | 30 | g_fmt(register char *b, double x) |
michael@0 | 31 | { |
michael@0 | 32 | register int i, k; |
michael@0 | 33 | register char *s; |
michael@0 | 34 | int decpt, j, sign; |
michael@0 | 35 | char *b0, *s0, *se; |
michael@0 | 36 | |
michael@0 | 37 | b0 = b; |
michael@0 | 38 | #ifdef IGNORE_ZERO_SIGN |
michael@0 | 39 | if (!x) { |
michael@0 | 40 | *b++ = '0'; |
michael@0 | 41 | *b = 0; |
michael@0 | 42 | goto done; |
michael@0 | 43 | } |
michael@0 | 44 | #endif |
michael@0 | 45 | s = s0 = dtoa(x, 0, 0, &decpt, &sign, &se); |
michael@0 | 46 | if (sign) |
michael@0 | 47 | *b++ = '-'; |
michael@0 | 48 | if (decpt == 9999) /* Infinity or Nan */ { |
michael@0 | 49 | while((*b++ = *s++)) {} |
michael@0 | 50 | goto done0; |
michael@0 | 51 | } |
michael@0 | 52 | if (decpt <= -4 || decpt > se - s + 5) { |
michael@0 | 53 | *b++ = *s++; |
michael@0 | 54 | if (*s) { |
michael@0 | 55 | *b++ = '.'; |
michael@0 | 56 | while((*b = *s++)) |
michael@0 | 57 | b++; |
michael@0 | 58 | } |
michael@0 | 59 | *b++ = 'e'; |
michael@0 | 60 | /* sprintf(b, "%+.2d", decpt - 1); */ |
michael@0 | 61 | if (--decpt < 0) { |
michael@0 | 62 | *b++ = '-'; |
michael@0 | 63 | decpt = -decpt; |
michael@0 | 64 | } |
michael@0 | 65 | else |
michael@0 | 66 | *b++ = '+'; |
michael@0 | 67 | for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10) {} |
michael@0 | 68 | for(;;) { |
michael@0 | 69 | i = decpt / k; |
michael@0 | 70 | *b++ = i + '0'; |
michael@0 | 71 | if (--j <= 0) |
michael@0 | 72 | break; |
michael@0 | 73 | decpt -= i*k; |
michael@0 | 74 | decpt *= 10; |
michael@0 | 75 | } |
michael@0 | 76 | *b = 0; |
michael@0 | 77 | } |
michael@0 | 78 | else if (decpt <= 0) { |
michael@0 | 79 | *b++ = '.'; |
michael@0 | 80 | for(; decpt < 0; decpt++) |
michael@0 | 81 | *b++ = '0'; |
michael@0 | 82 | while((*b++ = *s++)) {} |
michael@0 | 83 | } |
michael@0 | 84 | else { |
michael@0 | 85 | while((*b = *s++)) { |
michael@0 | 86 | b++; |
michael@0 | 87 | if (--decpt == 0 && *s) |
michael@0 | 88 | *b++ = '.'; |
michael@0 | 89 | } |
michael@0 | 90 | for(; decpt > 0; decpt--) |
michael@0 | 91 | *b++ = '0'; |
michael@0 | 92 | *b = 0; |
michael@0 | 93 | } |
michael@0 | 94 | done0: |
michael@0 | 95 | freedtoa(s0); |
michael@0 | 96 | #ifdef IGNORE_ZERO_SIGN |
michael@0 | 97 | done: |
michael@0 | 98 | #endif |
michael@0 | 99 | return b0; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | } // namespace dmg_fp |