-
Notifications
You must be signed in to change notification settings - Fork 3
/
fraction.c
181 lines (156 loc) · 5.45 KB
/
fraction.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
** find rational approximation to given real number
** David Eppstein / UC Irvine / 8 Aug 1993
**
** With corrections from Arno Formella, May 2008
**
** Made into a ruby module by Christopher Lord, Nov 2009
**
** usage: require 'fraction'
**
** n,d,err = 0.33.fraction
**
** based on the theory of continued fractions
** if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...)))
** then best approximation is found by truncating this series
** (with some adjustments in the last term).
**
** Note the fraction can be recovered as the first column of the matrix
** ( a1 1 ) ( a2 1 ) ( a3 1 ) ...
** ( 1 0 ) ( 1 0 ) ( 1 0 )
** Instead of keeping the sequence of continued fraction terms,
** we just keep the last partial product of these matrices.
*/
#include "ruby.h"
#include <stdio.h>
VALUE method_html_form_for(VALUE self)
{
VALUE res = rb_str_new2("<span class='fraction'><span class='above'>");
// This is an array. to make a string form, we want to
VALUE num = rb_obj_as_string(rb_ary_entry(self, 0));
VALUE den = rb_obj_as_string(rb_ary_entry(self, 1));
rb_str_concat(res, rb_obj_as_string(num));
rb_str_cat2(res, "</span>⁄<span class='below'>");
rb_str_concat(res, rb_obj_as_string(den));
rb_str_cat2(res, "</span></span>");
// CSS for the above html (From http://www.cs.tut.fi/~jkorpela/math/#fractions)
// .above, .below { font-size: 70%;
// font-family: Verdana, Arial, sans-serif; }
// .above { vertical-align: 0.7ex; }
// .below { vertical-align: -0.3ex; }
return res;
}
VALUE method_string_form_for(VALUE self)
{
VALUE res = rb_str_new2("");
// This is an array. to make a string form, we want to
VALUE num = rb_obj_as_string(rb_ary_entry(self, 0));
VALUE den = rb_obj_as_string(rb_ary_entry(self, 1));
rb_str_concat(res, rb_obj_as_string(num));
rb_str_cat2(res, "/");
rb_str_concat(res, rb_obj_as_string(den));
return res;
}
void core_fraction(double val, long maxden, long * n, long * d, double * e)
{
int ai;
long sign = 1;
long m11 = 1, m22 = 1;
long m12 = 0, m21 = 0;
if (val < 0.0) {
// work in positive space, it seems we can get confused by negatives
sign = -1;
val *= -1.0;
}
double x = val;
long count = 0;
// loop finding terms until denom gets too big
while (m21 * ( ai = (long)x ) + m22 <= maxden) {
if (++count > 50000000) break; // break after 'too many' iterations
long t = m11 * ai + m12;
m12 = m11;
m11 = t;
t = m21 * ai + m22;
m22 = m21;
m21 = t;
if(x==(double)ai) break;
x = 1/(x - (double) ai);
if(x>(double)0x7FFFFFFF) break;
}
*n = m11 * sign;
*d = m21;
*e = val - ((double)m11 / (double)m21);
}
VALUE method_fraction_for(int argc, VALUE * argv, VALUE self)
{
VALUE res = rb_ary_new2(3);
VALUE maxdenr;
long maxden = 10; // the default
rb_scan_args(argc, argv, "01", &maxdenr);
if (!NIL_P(maxdenr))
maxden = NUM2INT(maxdenr);
double x = NUM2DBL(self);
long n, d;
double e;
core_fraction(x, maxden, &n, &d, &e);
VALUE numer1 = INT2NUM(n);
VALUE denom1 = INT2NUM(d);
VALUE err1 = rb_float_new(e);
rb_ary_store(res, 0, numer1);
rb_ary_store(res, 1, denom1);
rb_ary_store(res, 2, err1);
// Although the below is very cool, it's also quite slow to execute.
//rb_define_singleton_method(res, "to_s", method_string_form_for, 0);
//rb_define_singleton_method(res, "to_html", method_html_form_for, 0);
/* We can go one more step to find another candidate:
m11 = m11 * ai + m12;
m21 = m21 * ai + m22;
m11/m21, err= startx - ((double) m11 / (double) m21))
*/
return res;
}
/// Same as above, but implements whole fraction simplification.
/// the return value is: h, n, d, e = 3.5.whole_fraction
VALUE method_whole_fraction_for(int argc, VALUE * argv, VALUE self)
{
long m11, m12,
m21, m22;
VALUE res = rb_ary_new2(4);
VALUE maxdenr;
long maxden = 10; // the default
rb_scan_args(argc, argv, "01", &maxdenr);
if (!NIL_P(maxdenr))
maxden = NUM2INT(maxdenr);
if (rb_equal(INT2FIX(0), self)){
rb_ary_store(res, 0, INT2FIX(0));
rb_ary_store(res, 1, INT2FIX(0));
rb_ary_store(res, 2, INT2FIX(1));
rb_ary_store(res, 3, INT2FIX(0));
return res;
}
VALUE wholen = rb_funcall(self, rb_intern("truncate"), 0);
VALUE subval = rb_funcall(rb_funcall(self, rb_intern("-"), 1, wholen), rb_intern("abs"), 0);
double x = NUM2DBL(subval);
long n, d;
double e;
core_fraction(x, maxden, &n, &d, &e);
VALUE numer1 = INT2NUM(n);
VALUE denom1 = INT2NUM(d);
VALUE err1 = rb_float_new(e);
rb_ary_store(res, 0, wholen);
rb_ary_store(res, 1, numer1);
rb_ary_store(res, 2, denom1);
rb_ary_store(res, 3, err1);
return res;
}
void Init_fraction() {
rb_define_method(rb_cNumeric, "to_whole_fraction", method_whole_fraction_for, -1);
rb_define_method(rb_cFloat, "to_whole_fraction", method_whole_fraction_for, -1);
rb_define_method(rb_cNumeric, "to_fraction", method_fraction_for, -1);
rb_define_method(rb_cFloat, "to_fraction", method_fraction_for, -1);
// The following are legacy support methods. they are named a bit badly
rb_define_method(rb_cNumeric, "fraction", method_fraction_for, -1);
rb_define_method(rb_cFloat, "fraction", method_fraction_for, -1);
rb_define_method(rb_cNumeric, "whole_fraction", method_whole_fraction_for, -1);
rb_define_method(rb_cFloat, "whole_fraction", method_whole_fraction_for, -1);
}