-
Notifications
You must be signed in to change notification settings - Fork 0
/
RRFractionNumber.m
200 lines (161 loc) · 4.11 KB
/
RRFractionNumber.m
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// RRFraction.m
// Lignarius
//
// Created by Timothy Ritchey on Fri Nov 01 2002.
// Copyright (c) 2002 Red Rome Logic. All rights reserved.
//
#import <math.h>
#import "RRFractionNumber.h"
// c functions for converting to fraction from float
int ggt(int a, int b)
{
if (b==0) { return(a); }
return(ggt(b, a % b));
}
double ggtd(double a, double b)
{
if ((a<32768) && (b<32768)) {
return((double)ggt((int)lround(a),(int)lround(b)));
} else {
if ((int)b<0.5) {
return(a);
} else {
return(ggtd(b,a-b*floor(a/b)));
}
}
}
void kr(double *a, double *b)
{
double t;
t=ggtd(*a,*b);
*a=*a/t;
*b=*b/t;
}
int emb(double q, double *a, double *b, double eps)
{
double q0,qabs,qvor,qnk,am,amm,bmm,bm;
int i;
qabs=fabs(q);
q0=qabs;
*a=1.0;
*b=0.0;
am=0.0;
bm=1.0;
i=0;
do {
i++;
if (i>30) break;
qvor=floor(q0+eps);
qnk=q0-qvor;
amm=am;
bmm=bm;
am=*a;
bm=*b;
*a=qvor*am+amm;
*b=qvor*bm+bmm;
kr(a,b);
if (qnk<eps) break;
q0=1/qnk;
} while (fabs(qabs-(*a)/(*b))>=eps);
if (fabs(qabs-(*a)/(*b))>eps) return(0);
if (q<0) *a=-*a;
return 0;
}
@implementation RRFractionNumber
- (const char *)objCType
{
return "f";
}
- (void)getValue:(void *)buffer
{
*((float*)(buffer)) = (float)(integerPart + numerator/denominator);
}
+ (RRFractionNumber*)fractionNumberWithIntegerPart:(int)i numerator:(int)n denominator:(int)d
{
return [[[[RRFractionNumber alloc] initWithIntegerPart:i numerator:n denominator:d] retain] autorelease];
}
+ (RRFractionNumber*)fractionNumberWithFloat:(float)f
{
double a, d;
int n;
emb(f, &a, &d, 1e-6);
n=(int)a-(int)f*d;
return [self fractionNumberWithIntegerPart:(int)f
numerator:n
denominator:d];
}
+ (RRFractionNumber*)fractionNumberWithString:(NSString*)s
{
//hmmm, this should be good...
NSScanner *scan = [NSScanner scannerWithString:s];
float f;
int i = 0, n = 0, d = 0;
NSString *tmpString;
if([scan scanInt:&i]) {
// we have an integer
if([scan scanInt:&n]) {
if([scan scanString:@"/" intoString:&tmpString]) {
if([scan scanInt:&d]) {
return [self fractionNumberWithIntegerPart:i
numerator:n
denominator:d];
}
}
} else if([scan scanString:@"/" intoString:&tmpString]) {
n = i; i = 0;
if([scan scanInt:&d]) {
return [self fractionNumberWithIntegerPart:i
numerator:n
denominator:d];
}
} else if([scan scanString:@"." intoString:&tmpString]) {
// do nothing drop out and scan fraction
} else {
return [self fractionNumberWithIntegerPart:i
numerator:0
denominator:0];
}
}
// okay, finally, try to see if a float will do it
[scan setScanLocation:0];
if([scan scanFloat:&f])
return [self fractionNumberWithFloat:f];
return [self fractionNumberWithFloat:0.0];
}
- (id)initWithIntegerPart:(int)i numerator:(int)n denominator:(int)d
{
if (self = [super init])
{
integerPart = i;
numerator = n;
denominator = d;
}
return self;
}
- (int)integerPart
{
return integerPart;
}
- (int)numerator
{
return numerator;
}
- (int)denominator
{
return denominator;
}
- (float)floatValue
{
return (float)(integerPart + numerator/denominator);
}
- (NSString *)descriptionWithLocale:(NSDictionary *)locale
{
return [[[NSString alloc] initWithFormat:@"%d %d/%d"
locale:locale, integerPart, numerator, denominator] autorelease];
}
- (id)copyWithZone:(NSZone *)zone
{
return [self retain];
}
@end