Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Issue #38 #39 #40
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Galbreath committed Sep 23, 2016
1 parent df76c34 commit 390d7be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/modp_numtoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ size_t modp_dtoa(double value, char* str, int prec)
// given 0.05, prec=1
// whole = 0
// tmp = (0.05)* 10 = 0.5
// frac = 0
// frac = 0
// diff = tmp -frac == 0.5 - 0.0 = 0.5
//
int whole = (int) value;
Expand All @@ -138,16 +138,20 @@ size_t modp_dtoa(double value, char* str, int prec)
frac = 0;
++whole;
}
} else if (diff == 0.5 && ((frac & 1))) {
} else if (diff == 0.5 && prec > 0 && (frac & 1)) {
/* if halfway, round up if odd, OR
if last digit is 0. That last part is strange */
++frac;
if (frac >= powers_of_10[prec]) {
frac = 0;
++whole;
}
} else if (diff == 0.5 && (prec > 0 && frac == 0)) {
} else if (diff == 0.5 && prec == 0 && (whole & 1)) {
++frac;
if (frac >= powers_of_10[prec]) {
frac = 0;
++whole;
}
}

/* for very large numbers switch back to native sprintf for exponentials.
Expand Down Expand Up @@ -238,16 +242,20 @@ size_t modp_dtoa2(double value, char* str, int prec)
frac = 0;
++whole;
}
} else if (diff == 0.5 && ((frac & 1))) {
} else if (diff == 0.5 && prec > 0 && (frac & 1)) {
/* if halfway, round up if odd, OR
if last digit is 0. That last part is strange */
++frac;
if (frac >= powers_of_10[prec]) {
frac = 0;
++whole;
}
} else if (diff == 0.5 && (prec > 0 && frac == 0)) {
} else if (diff == 0.5 && prec == 0 && (whole & 1)) {
++frac;
if (frac >= powers_of_10[prec]) {
frac = 0;
++whole;
}
}

/* for very large numbers switch back to native sprintf for exponentials.
Expand Down
12 changes: 10 additions & 2 deletions test/modp_numtoa_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,23 @@ static char* testDoubleToA(void)
/* test each combination of whole number + fraction,
at every precision */
/* and test negative version */
double wholes[] = {0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,
double wholes[] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,
67.0,101.0, 10000, 99999};
double frac[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.49, 0.5, 0.51, 0.6, 0.7,
0.9, 0.01, 0.25, 0.125, 0.03, 0.05, 0.005, 0.0005, 0.00005,
0.9, 0.01, 0.25, 0.125, 0.03, 0.0625, 0.0078125,
0.001, 0.00001, 0.99, 0.999, 0.9999, 0.99999, 0.999999,
// 0.95, 0.995, 0.9995, 0.99995, 0.999995, 0.9999995,
0.09, 0.099, 0.0999, 0.09999, 0.099999, 0.0999999,
0.09999999
};

/* TBD
* 0.95, 0.995, 0.9995, 0.99995, 0.999995, 0.9999995
* since not exactly represented by floating point
* printf uses some tricks that we do not use
* causing test issues
*/


const char* formats[] = {"%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f",
"%.6f", "%.7f", "%.8f", "%.9f"};
Expand Down

0 comments on commit 390d7be

Please sign in to comment.