IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39058 for trunk


Ignore:
Timestamp:
Nov 6, 2015, 10:24:47 AM (11 years ago)
Author:
eugene
Message:

try frexp instead of log & pow in sprintf_float,double (not an improvement)

Location:
trunk/Ohana/src/libohana
Files:
1 added
1 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/Makefile

    r39040 r39058  
    5151$(SRC)/errors.$(ARCH).o  \
    5252$(SRC)/gprint.$(ARCH).o  \
    53 $(SRC)/print_floats.$(ARCH).o    \
     53$(SRC)/sprintf_floats.$(ARCH).o  \
    5454$(SRC)/version.$(ARCH).o
    5555
     
    6565$(DESTLIB)/libohana.$(DLLTYPE): $(LIB)/libohana.$(ARCH).$(DLLTYPE)
    6666
    67 TESTPROG = print_floats
     67TESTPROG = sprintf_floats
    6868# TESTPROG = memtest typetest string
    6969
  • trunk/Ohana/src/libohana/src/sprintf_floats.c

    r39046 r39058  
    11# include <ohana.h>
    22
     3# define USE_LOGS 1
    34# define NSIGFIG_FLT  7
    45# define NEXPFIG_FLT  2
     6# define M_LOG2_10 (M_LN10 / M_LN2)
     7# define M_LOG10_2 (M_LN2 / M_LN10)
    58
    69// returns the number of bytes written EXCLUDING the ending NULL char
     
    2225  double pvalue = fabs(value);
    2326
    24   int isPositiveExp = (pvalue >= 1.0);
    25 
     27# if (USE_LOGS)
    2628  // get the exponent
    2729  double Exponent = log10(pvalue);
     
    3436
    3537  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
    3665
    3766  // multiplier here must be 10^(NSIGFIG_FLT + 1)
     
    5382  iMantissa = iMantissa / 10;
    5483  if (roundUp) iMantissa ++;
     84  if (iMantissa > 99999999) {
     85    iMantissa = iMantissa / 10;
     86    iExponent ++;
     87  }
     88  int isPositiveExp = (iExponent >= 0);
    5589
    5690  // now write out the Mantissa backwards as an integer, but include a decimal point
     
    100134  double pvalue = fabs(value);
    101135
    102   int isPositiveExp = (pvalue >= 1.0);
    103 
     136# if (USE_LOGS)
    104137  // get the exponent
    105138  double Exponent = log10(pvalue);
     
    112145
    113146  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
    114174
    115175  // multiplier here must be 10^(NSIGFIG_DBL + 1)
     
    131191  iMantissa = iMantissa / 10;
    132192  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);
    133203
    134204  // now write out the Mantissa backwards as an integer, but include a decimal point
  • trunk/Ohana/src/libohana/test/sprintf_floats.c

    r39046 r39058  
    22# include "tap_ohana.h"
    33
     4# define DO_FLOATS 1
     5# define DO_DOUBLES 1
    46# define NTESTS 300000
    57
     
    3436int main (void) {
    3537
    36   plan_tests (20);
     38  // plan_tests (2013);
     39  plan_tests (4026);
    3740
    3841  diag ("libohana print_float.c tests");
    3942
    4043  /*** sprint_float ***/
    41   if (1) {
     44  if (DO_FLOATS) {
    4245    int Noutput;
    4346    char output[128];
     
    8184 
    8285  /*** sprint_double ***/
    83   if (1) {
     86  if (DO_DOUBLES) {
    8487    int Noutput;
    8588    char output[128];
Note: See TracChangeset for help on using the changeset viewer.