[beast/devel: 24/35] BSE: change bse_string_from_double() API, test math to_string functions
- From: Tim Janik <timj src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [beast/devel: 24/35] BSE: change bse_string_from_double() API, test math to_string functions
- Date: Tue, 14 May 2013 19:39:55 +0000 (UTC)
commit 6345e8e931e3360ef9529b1dc988e41c277b509a
Author: Tim Janik <timj gnu org>
Date: Tue May 14 00:50:27 2013 +0200
BSE: change bse_string_from_double() API, test math to_string functions
bse/bsemath.cc | 41 ++++++++++++++++++++++-------------------
bse/bsemath.hh | 1 +
bse/tests/misctests.cc | 24 ++++++++++++++++++++++++
3 files changed, 47 insertions(+), 19 deletions(-)
---
diff --git a/bse/bsemath.cc b/bse/bsemath.cc
index 3cddc23..e6fbe17 100644
--- a/bse/bsemath.cc
+++ b/bse/bsemath.cc
@@ -6,19 +6,21 @@
#define RING_BUFFER_LENGTH (256) // FIXME: simlpy dup strings in the API
#define PRINTF_DIGITS "1270"
#define FLOAT_STRING_SIZE (2048)
+
/* --- functions --- */
-static inline char*
-pretty_print_double (char *str,
- double d)
+std::string
+bse_string_from_double (long double value)
{
- char *s= str;
- sprintf (s, "%." PRINTF_DIGITS "f", d);
+ std::string str;
+ str.resize (FLOAT_STRING_SIZE, 0);
+ char *s = &str[0];
+ sprintf (s, "%." PRINTF_DIGITS "Lf", value);
while (*s)
s++;
- while (s[-1] == '0' && s[-2] != '.')
+ while (s > &str[2] && s[-1] == '0' && s[-2] != '.')
s--;
*s = 0;
- return s;
+ return std::string (str.c_str());
}
char*
@@ -40,10 +42,11 @@ bse_complex_list (uint n_points,
*s = 0;
if (indent)
strcat (s, indent);
- while (*s) s++;
- s = pretty_print_double (s, points[i].re);
+ while (*s)
+ s++;
+ *s = 0; strcat (s, bse_string_from_double (points[i].re).c_str()); s += strlen (s);
*s++ = ' ';
- s = pretty_print_double (s, points[i].im);
+ *s = 0; strcat (s, bse_string_from_double (points[i].im).c_str()); s += strlen (s);
*s++ = '\n';
}
*s++ = 0;
@@ -63,10 +66,10 @@ bse_complex_str (BseComplex c)
g_free (rbuffer[rbi]);
s = tbuffer;
*s++ = '{';
- s = pretty_print_double (s, c.re);
+ *s = 0; strcat (s, bse_string_from_double (c.re).c_str()); s += strlen (s);
*s++ = ',';
*s++ = ' ';
- s = pretty_print_double (s, c.im);
+ *s = 0; strcat (s, bse_string_from_double (c.im).c_str()); s += strlen (s);
*s++ = '}';
*s++ = 0;
rbuffer[rbi] = g_strdup (tbuffer);
@@ -90,14 +93,14 @@ bse_poly_str (uint degree,
g_free (rbuffer[rbi]);
s = tbuffer;
*s++ = '(';
- s = pretty_print_double (s, a[0]);
+ *s = 0; strcat (s, bse_string_from_double (a[0]).c_str()); s += strlen (s);
for (i = 1; i <= degree; i++)
{
*s++ = '+';
*s = 0; strcat (s, var); while (*s) s++;
*s++ = '*';
*s++ = '(';
- s = pretty_print_double (s, a[i]);
+ *s = 0; strcat (s, bse_string_from_double (a[i]).c_str()); s += strlen (s);
}
while (i--)
*s++ = ')';
@@ -125,7 +128,7 @@ bse_poly_str1 (uint degree,
*s++ = '(';
if (a[0] != 0.0)
{
- s = pretty_print_double (s, a[0]);
+ *s = 0; strcat (s, bse_string_from_double (a[0]).c_str()); s += strlen (s);
need_plus = 1;
}
for (i = 1; i <= degree; i++)
@@ -140,7 +143,7 @@ bse_poly_str1 (uint degree,
}
if (a[i] != 1.0)
{
- s = pretty_print_double (s, a[i]);
+ *s = 0; strcat (s, bse_string_from_double (a[i]).c_str()); s += strlen (s);
*s++ = '*';
}
*s = 0;
@@ -183,9 +186,9 @@ bse_float_gnuplot (const char *file_name,
uint i;
for (i = 0; i < n_ypoints; i++)
{
- char xstr[FLOAT_STRING_SIZE], ystr[FLOAT_STRING_SIZE];
- pretty_print_double (xstr, xstart + i * xstep);
- pretty_print_double (ystr, ypoints[i]);
+ char xbuf[FLOAT_STRING_SIZE], *xstr = xbuf, ybuf[FLOAT_STRING_SIZE], *ystr = ybuf;
+ *xstr = 0; strcat (xstr, bse_string_from_double (xstart + i * xstep).c_str()); xstr += strlen (xstr);
+ *ystr = 0; strcat (ystr, bse_string_from_double (ypoints[i]).c_str()); ystr += strlen (ystr);
fprintf (fout, "%s %s\n", xstr, ystr);
}
fclose (fout);
diff --git a/bse/bsemath.hh b/bse/bsemath.hh
index f37d4b3..92e2eec 100644
--- a/bse/bsemath.hh
+++ b/bse/bsemath.hh
@@ -85,6 +85,7 @@ char* bse_complex_list (uint n_points,
void bse_complex_gnuplot (const char *file_name,
uint n_points,
BseComplex *points);
+std::string bse_string_from_double (long double value);
/* --- polynomials --- */
/* example, degree=2: 5+2x+7x^2 => a[0..degree] = { 5, 2, 7 } */
diff --git a/bse/tests/misctests.cc b/bse/tests/misctests.cc
index cccaadf..95a6c21 100644
--- a/bse/tests/misctests.cc
+++ b/bse/tests/misctests.cc
@@ -105,11 +105,35 @@ check_freq_vs_notes (BseMusicalTuningType musical_tuning)
}
TDONE();
}
+
+static void
+test_math_bits ()
+{
+ TCMP (bse_string_from_double (-1.0), ==, "-1.0");
+ TCMP (bse_string_from_double (0.0), ==, "0.0");
+ TCMP (bse_string_from_double (1000000000000000.75000000000000), ==, "1000000000000000.75");
+ BseComplex c[] = { { 0, 0 }, { -1, +1 }, { +2, -3 } };
+ const std::string c0 = bse_complex_str (c[0]);
+ TCMP (c0, ==, "{0.0, 0.0}");
+ const std::string c1 = bse_complex_str (c[1]);
+ TCMP (c1, ==, "{-1.0, 1.0}");
+ const std::string c2 = bse_complex_str (c[2]);
+ TCMP (c2, ==, "{2.0, -3.0}");
+ const std::string complex_list = bse_complex_list (RAPICORN_ARRAY_SIZE (c), c, "");
+ TCMP (complex_list, ==, "0.0 0.0\n-1.0 1.0\n2.0 -3.0\n");
+ double a[] = { 0, -1, +2, -3, +7.75, -1000000000000000.75 };
+ const std::string poly1 = bse_poly_str (RAPICORN_ARRAY_SIZE (a) - 1, a, "x");
+ TCMP (poly1, ==, "(0.0+x*(-1.0+x*(2.0+x*(-3.0+x*(7.75+x*(-1000000000000000.75))))))");
+ const std::string poly2 = bse_poly_str1 (RAPICORN_ARRAY_SIZE (a) - 1, a, "y");
+ TCMP (poly2, ==, "(-1.0*y + 2.0*y**2 + -3.0*y**3 + 7.75*y**4 + -1000000000000000.75*y**5)");
+}
+
int
main (gint argc,
gchar *argv[])
{
bse_init_test (&argc, argv);
+ test_math_bits();
check_cent_tune();
check_cent_tune_fast();
check_equal_tempered_tuning();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]