- Timestamp:
- Nov 6, 2015, 10:24:47 AM (11 years ago)
- Location:
- trunk/Ohana/src/libohana
- Files:
-
- 1 added
- 1 edited
- 2 moved
-
Makefile (modified) (2 diffs)
-
doc/sprintf_floats.txt (added)
-
src/sprintf_floats.c (moved) (moved from trunk/Ohana/src/libohana/src/print_floats.c ) (7 diffs)
-
test/sprintf_floats.c (moved) (moved from trunk/Ohana/src/libohana/test/print_floats.c ) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/libohana/Makefile
r39040 r39058 51 51 $(SRC)/errors.$(ARCH).o \ 52 52 $(SRC)/gprint.$(ARCH).o \ 53 $(SRC)/ print_floats.$(ARCH).o \53 $(SRC)/sprintf_floats.$(ARCH).o \ 54 54 $(SRC)/version.$(ARCH).o 55 55 … … 65 65 $(DESTLIB)/libohana.$(DLLTYPE): $(LIB)/libohana.$(ARCH).$(DLLTYPE) 66 66 67 TESTPROG = print_floats67 TESTPROG = sprintf_floats 68 68 # TESTPROG = memtest typetest string 69 69 -
trunk/Ohana/src/libohana/src/sprintf_floats.c
r39046 r39058 1 1 # include <ohana.h> 2 2 3 # define USE_LOGS 1 3 4 # define NSIGFIG_FLT 7 4 5 # define NEXPFIG_FLT 2 6 # define M_LOG2_10 (M_LN10 / M_LN2) 7 # define M_LOG10_2 (M_LN2 / M_LN10) 5 8 6 9 // returns the number of bytes written EXCLUDING the ending NULL char … … 22 25 double pvalue = fabs(value); 23 26 24 int isPositiveExp = (pvalue >= 1.0); 25 27 # if (USE_LOGS) 26 28 // get the exponent 27 29 double Exponent = log10(pvalue); … … 34 36 35 37 double Mantissa = pvalue / power; 38 39 # else 40 41 // p = frexpf (x,n) returns (p,n) where x = p*2^n 42 // we want f = q*10^m 43 // s = n * log10(2) 44 // m = floor(s) 45 // r = s - m 46 // t = r * log2(10) 47 // q = p * 2^t 48 49 int n; 50 double p = frexp (pvalue, &n); 51 double s = n * M_LOG10_2; 52 int m = floor(s); 53 double r = s - m; 54 double t = r * M_LOG2_10; 55 double q = p * pow(2.0, t); 56 57 if (q < 1.0) { 58 q *= 10.0; 59 m --; 60 } 61 62 int iExponent = m; 63 double Mantissa = q; 64 # endif 36 65 37 66 // multiplier here must be 10^(NSIGFIG_FLT + 1) … … 53 82 iMantissa = iMantissa / 10; 54 83 if (roundUp) iMantissa ++; 84 if (iMantissa > 99999999) { 85 iMantissa = iMantissa / 10; 86 iExponent ++; 87 } 88 int isPositiveExp = (iExponent >= 0); 55 89 56 90 // now write out the Mantissa backwards as an integer, but include a decimal point … … 100 134 double pvalue = fabs(value); 101 135 102 int isPositiveExp = (pvalue >= 1.0); 103 136 # if (USE_LOGS) 104 137 // get the exponent 105 138 double Exponent = log10(pvalue); … … 112 145 113 146 double Mantissa = pvalue / power; 147 148 # else 149 150 // p = frexpf (x,n) returns (p,n) where x = p*2^n 151 // we want f = q*10^m 152 // s = n * log10(2) 153 // m = floor(s) 154 // r = s - m 155 // t = r * log2(10) 156 // q = p * 2^t 157 158 int n; 159 double p = frexp (pvalue, &n); 160 double s = n * M_LOG10_2; 161 int m = floor(s); 162 double r = s - m; 163 double t = r * M_LOG2_10; 164 double q = p * pow(2.0, t); 165 166 if (q < 1.0) { 167 q *= 10.0; 168 m --; 169 } 170 171 int iExponent = m; 172 double Mantissa = q; 173 # endif 114 174 115 175 // multiplier here must be 10^(NSIGFIG_DBL + 1) … … 131 191 iMantissa = iMantissa / 10; 132 192 if (roundUp) iMantissa ++; 193 194 // 1000000000000000 195 // 999999999999999 196 197 if (iMantissa > 999999999999999) { 198 iMantissa = iMantissa / 10; 199 iExponent ++; 200 } 201 202 int isPositiveExp = (iExponent >= 0); 133 203 134 204 // now write out the Mantissa backwards as an integer, but include a decimal point -
trunk/Ohana/src/libohana/test/sprintf_floats.c
r39046 r39058 2 2 # include "tap_ohana.h" 3 3 4 # define DO_FLOATS 1 5 # define DO_DOUBLES 1 4 6 # define NTESTS 300000 5 7 … … 34 36 int main (void) { 35 37 36 plan_tests (20); 38 // plan_tests (2013); 39 plan_tests (4026); 37 40 38 41 diag ("libohana print_float.c tests"); 39 42 40 43 /*** sprint_float ***/ 41 if ( 1) {44 if (DO_FLOATS) { 42 45 int Noutput; 43 46 char output[128]; … … 81 84 82 85 /*** sprint_double ***/ 83 if ( 1) {86 if (DO_DOUBLES) { 84 87 int Noutput; 85 88 char output[128];
Note:
See TracChangeset
for help on using the changeset viewer.
