diff options
author | Wim Taymans <wim.taymans@gmail.com> | 2004-07-27 16:45:30 +0000 |
---|---|---|
committer | Wim Taymans <wim.taymans@gmail.com> | 2004-07-27 16:45:30 +0000 |
commit | 36b41f4666ffad371cabdebec8011412bd40a747 (patch) | |
tree | 7d54ef3de779445d11792a6a4bf759b537d7310f | |
parent | f8d6750a3627ed746d81ba9a2cb026a8e61b6a82 (diff) |
Added transform functions between double and fraction.
Original commit message from CVS:
* gst/gstvalue.c: (gst_value_transform_double_fraction),
(gst_value_transform_fraction_double), (_gst_value_initialize):
* testsuite/caps/Makefile.am:
* testsuite/caps/fraction-convert.c: (check_from_double_convert),
(check_from_fraction_convert), (transform_test), (main):
Added transform functions between double and fraction.
Added testcase to verify transforms
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | gst/gstvalue.c | 94 | ||||
-rw-r--r-- | tests/old/testsuite/caps/Makefile.am | 1 | ||||
-rw-r--r-- | tests/old/testsuite/caps/fraction-convert.c | 115 | ||||
-rw-r--r-- | testsuite/caps/Makefile.am | 1 | ||||
-rw-r--r-- | testsuite/caps/fraction-convert.c | 115 |
6 files changed, 336 insertions, 0 deletions
@@ -1,3 +1,13 @@ +2004-07-27 Wim Taymans <wim@fluendo.com> + + * gst/gstvalue.c: (gst_value_transform_double_fraction), + (gst_value_transform_fraction_double), (_gst_value_initialize): + * testsuite/caps/Makefile.am: + * testsuite/caps/fraction-convert.c: (check_from_double_convert), + (check_from_fraction_convert), (transform_test), (main): + Added transform functions between double and fraction. + Added testcase to verify transforms + 2004-07-26 Steve Lhomme <steve.lhomme@free.fr> * win32/GStreamer.vcproj: diff --git a/gst/gstvalue.c b/gst/gstvalue.c index 59a04b874..37612921f 100644 --- a/gst/gstvalue.c +++ b/gst/gstvalue.c @@ -20,6 +20,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include <math.h> #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -2544,6 +2545,95 @@ gst_value_transform_string_fraction (const GValue * src_value, gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer); } +#define MAX_TERMS 30 +#define MIN_DIVISOR 1.0e-10 +#define MAX_ERROR 1.0e-20 + +/* use continued fractions to transform a double into a fraction, + * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac. + * This algorithm takes care of overflows. + */ +static void +gst_value_transform_double_fraction (const GValue * src_value, + GValue * dest_value) +{ + gdouble V, F; /* double being converted */ + gint N, D; /* will contain the result */ + gint A; /* current term in continued fraction */ + gint64 N1, D1; /* numerator, denominator of last approx */ + gint64 N2, D2; /* numerator, denominator of previous approx */ + gint i; + gboolean negative = FALSE; + + /* initialize fraction being converted */ + F = src_value->data[0].v_double; + if (F < 0.0) { + F = -F; + negative = TRUE; + } + + V = F; + /* initialize fractions with 1/0, 0/1 */ + N1 = 1; + D1 = 0; + N2 = 0; + D2 = 1; + N = 1; + D = 1; + + for (i = 0; i < MAX_TERMS; i++) { + /* get next term */ + A = floor (F); + /* get new divisor */ + F = F - A; + + /* calculate new fraction in temp */ + N2 = N1 * A + N2; + D2 = D1 * A + D2; + + /* guard against overflow */ + if (N2 > G_MAXINT || D2 > G_MAXINT) { + break; + } + + N = N2; + D = D2; + + /* save last two fractions */ + N2 = N1; + D2 = D1; + N1 = N; + D1 = D; + + /* quit if dividing by zero or close enough to target */ + if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) { + break; + } + + /* Take reciprocal */ + F = 1 / F; + } + /* fix for overflow */ + if (D == 0) { + N = G_MAXINT; + D = 1; + } + /* fix for negative */ + if (negative) + N = -N; + + /* will also simplify */ + gst_value_set_fraction (dest_value, N, D); +} + +static void +gst_value_transform_fraction_double (const GValue * src_value, + GValue * dest_value) +{ + dest_value->data[0].v_double = ((double) src_value->data[0].v_int) / + ((double) src_value->data[1].v_int); +} + static int gst_value_compare_fraction (const GValue * value1, const GValue * value2) { @@ -2817,6 +2907,10 @@ _gst_value_initialize (void) gst_value_transform_fraction_string); g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION, gst_value_transform_string_fraction); + g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE, + gst_value_transform_fraction_double); + g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION, + gst_value_transform_double_fraction); gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE, gst_value_intersect_int_int_range); diff --git a/tests/old/testsuite/caps/Makefile.am b/tests/old/testsuite/caps/Makefile.am index 42efbd4b2..8d961d626 100644 --- a/tests/old/testsuite/caps/Makefile.am +++ b/tests/old/testsuite/caps/Makefile.am @@ -10,6 +10,7 @@ tests_pass = \ union \ string-conversions \ fixed \ + fraction-convert \ fraction-multiply-and-zero \ intersect2 \ caps \ diff --git a/tests/old/testsuite/caps/fraction-convert.c b/tests/old/testsuite/caps/fraction-convert.c new file mode 100644 index 000000000..41aae4047 --- /dev/null +++ b/tests/old/testsuite/caps/fraction-convert.c @@ -0,0 +1,115 @@ +/* GStreamer + * + * fraction-convert.c: test for GstFraction transform + * + * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <math.h> +#include <gst/gst.h> +#include <glib.h> + +static void +check_from_double_convert (gdouble value, gint nom, gint denom, gdouble prec) +{ + GValue value1 = { 0 }; + GValue value2 = { 0 }; + gdouble check; + + g_value_init (&value1, G_TYPE_DOUBLE); + g_value_init (&value2, GST_TYPE_FRACTION); + + g_value_set_double (&value1, value); + g_value_transform (&value1, &value2); + g_print ("%s = %s\n", + gst_value_serialize (&value1), gst_value_serialize (&value2)); + g_assert (gst_value_get_fraction_numerator (&value2) == nom); + g_assert (gst_value_get_fraction_denominator (&value2) == denom); + g_value_transform (&value2, &value1); + g_print ("%s = %s\n", + gst_value_serialize (&value2), gst_value_serialize (&value1)); + check = g_value_get_double (&value1); + g_assert (fabs (value - check) <= prec); +} + +static void +check_from_fraction_convert (gint nom, gint denom, gdouble prec) +{ + GValue value1 = { 0 }; + GValue value2 = { 0 }; + gdouble value; + + g_value_init (&value1, GST_TYPE_FRACTION); + g_value_init (&value2, G_TYPE_DOUBLE); + + gst_value_set_fraction (&value1, nom, denom); + g_value_transform (&value1, &value2); + + value = g_value_get_double (&value2); + g_assert (fabs (value - ((gdouble) nom) / denom) < prec); + + g_print ("%s = %s, %2.50lf as double\n", + gst_value_serialize (&value1), gst_value_serialize (&value2), value); + + g_value_transform (&value2, &value1); + g_print ("%s = %s\n", + gst_value_serialize (&value2), gst_value_serialize (&value1)); + value = g_value_get_double (&value2); + g_assert (gst_value_get_fraction_numerator (&value1) == nom); + g_assert (gst_value_get_fraction_denominator (&value1) == denom); + + g_value_unset (&value2); + g_value_unset (&value1); +} + +static void +transform_test (void) +{ + check_from_fraction_convert (30000, 1001, 1.0e-12); + check_from_fraction_convert (1, G_MAXINT, 1.0e-12); + check_from_fraction_convert (G_MAXINT, 1, 1.0e-12); + + check_from_double_convert (0.0, 0, 1, 1.0e-12); + check_from_double_convert (1.0, 1, 1, 1.0e-12); + check_from_double_convert (-1.0, -1, 1, 1.0e-12); + check_from_double_convert (M_PI, 1881244168, 598818617, 1.0e-12); + check_from_double_convert (-M_PI, -1881244168, 598818617, 1.0e-12); + + check_from_double_convert (G_MAXDOUBLE, G_MAXINT, 1, G_MAXDOUBLE); + check_from_double_convert (G_MINDOUBLE, 0, 1, G_MAXDOUBLE); + check_from_double_convert (-G_MAXDOUBLE, -G_MAXINT, 1, G_MAXDOUBLE); + check_from_double_convert (-G_MINDOUBLE, 0, 1, G_MAXDOUBLE); + + check_from_double_convert (((gdouble) G_MAXINT) + 1, G_MAXINT, 1, + G_MAXDOUBLE); + check_from_double_convert (((gdouble) G_MININT) - 1, G_MININT + 1, 1, + G_MAXDOUBLE); + + check_from_double_convert (G_MAXINT - 1, G_MAXINT - 1, 1, 0); + check_from_double_convert (G_MININT + 1, G_MININT + 1, 1, 0); +} + +int +main (int argc, char *argv[]) +{ + gst_init (&argc, &argv); + + transform_test (); + + return 0; +} diff --git a/testsuite/caps/Makefile.am b/testsuite/caps/Makefile.am index 42efbd4b2..8d961d626 100644 --- a/testsuite/caps/Makefile.am +++ b/testsuite/caps/Makefile.am @@ -10,6 +10,7 @@ tests_pass = \ union \ string-conversions \ fixed \ + fraction-convert \ fraction-multiply-and-zero \ intersect2 \ caps \ diff --git a/testsuite/caps/fraction-convert.c b/testsuite/caps/fraction-convert.c new file mode 100644 index 000000000..41aae4047 --- /dev/null +++ b/testsuite/caps/fraction-convert.c @@ -0,0 +1,115 @@ +/* GStreamer + * + * fraction-convert.c: test for GstFraction transform + * + * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <math.h> +#include <gst/gst.h> +#include <glib.h> + +static void +check_from_double_convert (gdouble value, gint nom, gint denom, gdouble prec) +{ + GValue value1 = { 0 }; + GValue value2 = { 0 }; + gdouble check; + + g_value_init (&value1, G_TYPE_DOUBLE); + g_value_init (&value2, GST_TYPE_FRACTION); + + g_value_set_double (&value1, value); + g_value_transform (&value1, &value2); + g_print ("%s = %s\n", + gst_value_serialize (&value1), gst_value_serialize (&value2)); + g_assert (gst_value_get_fraction_numerator (&value2) == nom); + g_assert (gst_value_get_fraction_denominator (&value2) == denom); + g_value_transform (&value2, &value1); + g_print ("%s = %s\n", + gst_value_serialize (&value2), gst_value_serialize (&value1)); + check = g_value_get_double (&value1); + g_assert (fabs (value - check) <= prec); +} + +static void +check_from_fraction_convert (gint nom, gint denom, gdouble prec) +{ + GValue value1 = { 0 }; + GValue value2 = { 0 }; + gdouble value; + + g_value_init (&value1, GST_TYPE_FRACTION); + g_value_init (&value2, G_TYPE_DOUBLE); + + gst_value_set_fraction (&value1, nom, denom); + g_value_transform (&value1, &value2); + + value = g_value_get_double (&value2); + g_assert (fabs (value - ((gdouble) nom) / denom) < prec); + + g_print ("%s = %s, %2.50lf as double\n", + gst_value_serialize (&value1), gst_value_serialize (&value2), value); + + g_value_transform (&value2, &value1); + g_print ("%s = %s\n", + gst_value_serialize (&value2), gst_value_serialize (&value1)); + value = g_value_get_double (&value2); + g_assert (gst_value_get_fraction_numerator (&value1) == nom); + g_assert (gst_value_get_fraction_denominator (&value1) == denom); + + g_value_unset (&value2); + g_value_unset (&value1); +} + +static void +transform_test (void) +{ + check_from_fraction_convert (30000, 1001, 1.0e-12); + check_from_fraction_convert (1, G_MAXINT, 1.0e-12); + check_from_fraction_convert (G_MAXINT, 1, 1.0e-12); + + check_from_double_convert (0.0, 0, 1, 1.0e-12); + check_from_double_convert (1.0, 1, 1, 1.0e-12); + check_from_double_convert (-1.0, -1, 1, 1.0e-12); + check_from_double_convert (M_PI, 1881244168, 598818617, 1.0e-12); + check_from_double_convert (-M_PI, -1881244168, 598818617, 1.0e-12); + + check_from_double_convert (G_MAXDOUBLE, G_MAXINT, 1, G_MAXDOUBLE); + check_from_double_convert (G_MINDOUBLE, 0, 1, G_MAXDOUBLE); + check_from_double_convert (-G_MAXDOUBLE, -G_MAXINT, 1, G_MAXDOUBLE); + check_from_double_convert (-G_MINDOUBLE, 0, 1, G_MAXDOUBLE); + + check_from_double_convert (((gdouble) G_MAXINT) + 1, G_MAXINT, 1, + G_MAXDOUBLE); + check_from_double_convert (((gdouble) G_MININT) - 1, G_MININT + 1, 1, + G_MAXDOUBLE); + + check_from_double_convert (G_MAXINT - 1, G_MAXINT - 1, 1, 0); + check_from_double_convert (G_MININT + 1, G_MININT + 1, 1, 0); +} + +int +main (int argc, char *argv[]) +{ + gst_init (&argc, &argv); + + transform_test (); + + return 0; +} |