summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-10-28 16:27:25 -0400
committerTavian Barnes <tavianator@gmail.com>2010-10-28 16:27:25 -0400
commit4c22411c7c540c4cba06c1941d084c00da764807 (patch)
tree31238b418ca29da5ef5d8c0955845c970e54ba5f /libdimension
parent34dec8c16e5bb765cf4e920b0743e87ec90b77e8 (diff)
downloaddimension-4c22411c7c540c4cba06c1941d084c00da764807.tar.xz
Fix dmnsn_print_polynomial().
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/polynomial.c29
1 files changed, 9 insertions, 20 deletions
diff --git a/libdimension/polynomial.c b/libdimension/polynomial.c
index 77e561d..31d8309 100644
--- a/libdimension/polynomial.c
+++ b/libdimension/polynomial.c
@@ -302,26 +302,15 @@ dmnsn_solve_polynomial(double poly[], size_t degree, double x[])
void
dmnsn_print_polynomial(FILE *file, double poly[], size_t degree)
{
- fprintf(file, "%g*x^%zu", poly[degree], degree);
- for (size_t i = degree - 1; i > 1; --i) {
- if (poly[i] > 0.0) {
- fprintf(file, " + %g*x^%zu", poly[i], i);
- } else if (poly[i] < 0.0) {
- fprintf(file, " - %g*x^%zu", -poly[i], i);
+ for (ssize_t i = degree; i >= 0; --i) {
+ if (i < degree) {
+ fprintf(file, (poly[i] >= 0.0) ? " + " : " - ");
+ }
+ fprintf(file, "%g", poly[i]);
+ if (i >= 2) {
+ fprintf(file, "*x^%zu", i);
+ } else if (i == 1) {
+ fprintf(file, "*x");
}
}
-
- if (poly[1] > 0.0) {
- fprintf(file, " + %g*x", poly[1]);
- } else if (poly[1] < 0.0) {
- fprintf(file, " - %g*x", -poly[1]);
- }
-
- if (poly[0] > 0.0) {
- fprintf(file, " + %g", poly[0]);
- } else if (poly[0] < 0.0) {
- fprintf(file, " - %g", -poly[0]);
- }
-
- fprintf(file, "\n");
}