[recipes/rational-approximation: 3/4] Always store numbers as doubles
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes/rational-approximation: 3/4] Always store numbers as doubles
- Date: Sat, 24 Jun 2017 22:07:49 +0000 (UTC)
commit c11e40d1a985718b28f284dec7ce967d494be69c
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Jun 24 17:35:35 2017 -0400
Always store numbers as doubles
We can now format doubles as mixed fractions, so no need
for this extra complication anymore.
src/gr-number.c | 66 ++++++++++---------------------------------------------
src/gr-number.h | 2 -
2 files changed, 12 insertions(+), 56 deletions(-)
---
diff --git a/src/gr-number.c b/src/gr-number.c
index d1e22e0..b520486 100644
--- a/src/gr-number.c
+++ b/src/gr-number.c
@@ -25,25 +25,6 @@
#include "gr-number.h"
#include "gr-utils.h"
-static int
-gcd (int m, int n)
-{
- int r;
-
- if (m == 0 && n == 0)
- return -1;
-
- if (m < 0) m = -m;
- if (n < 0) n = -n;
-
- while (n) {
- r = m % n;
- m = n;
- n = r;
- }
-
- return m;
-}
static gboolean
rational_approximation (double input,
@@ -105,16 +86,6 @@ gr_number_set_fraction (GrNumber *number,
int num,
int denom)
{
- int g;
-
- if (denom < 0) {
- num = -num;
- denom = -denom;
- }
- g = gcd (num, denom);
- number->fraction = TRUE;
- number->num = num / g;
- number->denom = denom / g;
number->value = ((double) num) / ((double) denom);
}
@@ -122,9 +93,6 @@ void
gr_number_set_float (GrNumber *number,
double value)
{
- number->fraction = FALSE;
- number->num = 0;
- number->denom = 0;
number->value = value;
}
@@ -155,10 +123,7 @@ gr_number_multiply (GrNumber *a1,
GrNumber *a2,
GrNumber *b)
{
- if (a1->fraction && a2->fraction)
- gr_number_set_fraction (b, a1->num * a2->num, a1->denom * a2->denom);
- else
- gr_number_set_float (b, a1->value * a2->value);
+ gr_number_set_float (b, a1->value * a2->value);
}
void
@@ -166,10 +131,7 @@ gr_number_add (GrNumber *a1,
GrNumber *a2,
GrNumber *b)
{
- if (a1->fraction && a2->fraction)
- gr_number_set_fraction (b, a1->num * a2->denom + a2->num * a1->denom, a1->denom * a2->denom);
- else
- gr_number_set_float (b, a1->value + a2->value);
+ gr_number_set_float (b, a1->value + a2->value);
}
typedef struct {
@@ -403,18 +365,14 @@ out:
char *
gr_number_format (GrNumber *number)
{
- if (number->fraction)
- return format_fraction (0, number->num, number->denom);
- else {
- double integral, rem;
- int num, denom;
-
- integral = floor (number->value);
- rem = number->value - integral;
-
- if (rational_approximation (rem, 20, &num, &denom))
- return format_fraction ((int)integral, num, denom);
- else
- return g_strdup_printf ("%g", number->value);
- }
+ double integral, rem;
+ int num, denom;
+
+ integral = floor (number->value);
+ rem = number->value - integral;
+
+ if (rational_approximation (rem, 20, &num, &denom))
+ return format_fraction ((int)integral, num, denom);
+ else
+ return g_strdup_printf ("%g", number->value);
}
diff --git a/src/gr-number.h b/src/gr-number.h
index 6682edf..937d9e8 100644
--- a/src/gr-number.h
+++ b/src/gr-number.h
@@ -23,8 +23,6 @@
G_BEGIN_DECLS
typedef struct {
- gboolean fraction;
- int num, denom;
double value;
} GrNumber;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]