source: libcfa/src/rational.cfa@ 9af00d23

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9af00d23 was 8a25be9, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

change exits to aborts to get stack trace

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[a493682]1//
[53ba273]2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
[a493682]6//
7// rational.c --
8//
[53ba273]9// Author : Peter A. Buhr
10// Created On : Wed Apr 6 17:54:28 2016
11// Last Modified By : Peter A. Buhr
[8a25be9]12// Last Modified On : Thu Mar 28 17:33:03 2019
13// Update Count : 181
[a493682]14//
[53ba273]15
[58b6d1b]16#include "rational.hfa"
17#include "fstream.hfa"
18#include "stdlib.hfa"
[53ba273]19
[3ce0d440]20forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
21 // helper routines
22
23 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce
24 // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
25 static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
26 for ( ;; ) { // Euclid's algorithm
27 RationalImpl r = a % b;
28 if ( r == (RationalImpl){0} ) break;
29 a = b;
30 b = r;
31 } // for
32 return b;
33 } // gcd
34
35 static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
36 if ( d == (RationalImpl){0} ) {
[8a25be9]37 abort( "Invalid rational number construction: denominator cannot be equal to 0.\n" );
[3ce0d440]38 } // exit
39 if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator
40 return gcd( abs( n ), d ); // simplify
41 } // Rationalnumber::simplify
42
43 // constructors
44
45 void ?{}( Rational(RationalImpl) & r ) {
46 r{ (RationalImpl){0}, (RationalImpl){1} };
47 } // rational
48
49 void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
50 r{ n, (RationalImpl){1} };
51 } // rational
52
53 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
54 RationalImpl t = simplify( n, d ); // simplify
[0087e0e]55 r.[numerator, denominator] = [n / t, d / t];
[3ce0d440]56 } // rational
57
58
59 // getter for numerator/denominator
60
61 RationalImpl numerator( Rational(RationalImpl) r ) {
62 return r.numerator;
63 } // numerator
64
65 RationalImpl denominator( Rational(RationalImpl) r ) {
66 return r.denominator;
67 } // denominator
68
69 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
70 return dest = src.[ numerator, denominator ];
71 } // ?=?
72
73 // setter for numerator/denominator
74
75 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
76 RationalImpl prev = r.numerator;
77 RationalImpl t = gcd( abs( n ), r.denominator ); // simplify
[0087e0e]78 r.[numerator, denominator] = [n / t, r.denominator / t];
[3ce0d440]79 return prev;
80 } // numerator
81
82 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
83 RationalImpl prev = r.denominator;
84 RationalImpl t = simplify( r.numerator, d ); // simplify
[0087e0e]85 r.[numerator, denominator] = [r.numerator / t, d / t];
[3ce0d440]86 return prev;
87 } // denominator
88
89 // comparison
90
91 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
92 return l.numerator * r.denominator == l.denominator * r.numerator;
93 } // ?==?
94
95 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
96 return ! ( l == r );
97 } // ?!=?
98
99 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
100 return l.numerator * r.denominator < l.denominator * r.numerator;
101 } // ?<?
102
103 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
104 return l.numerator * r.denominator <= l.denominator * r.numerator;
105 } // ?<=?
106
107 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
108 return ! ( l <= r );
109 } // ?>?
110
111 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
112 return ! ( l < r );
113 } // ?>=?
114
115 // arithmetic
116
117 Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
[0087e0e]118 return (Rational(RationalImpl)){ r.numerator, r.denominator };
[3ce0d440]119 } // +?
[53ba273]120
[3ce0d440]121 Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
[0087e0e]122 return (Rational(RationalImpl)){ -r.numerator, r.denominator };
[3ce0d440]123 } // -?
124
125 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
126 if ( l.denominator == r.denominator ) { // special case
[0087e0e]127 return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator };
[3ce0d440]128 } else {
[0087e0e]129 return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
[3ce0d440]130 } // if
131 } // ?+?
132
133 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
134 if ( l.denominator == r.denominator ) { // special case
[0087e0e]135 return (Rational(RationalImpl)){ l.numerator - r.numerator, l.denominator };
[3ce0d440]136 } else {
[0087e0e]137 return (Rational(RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
[3ce0d440]138 } // if
139 } // ?-?
140
141 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
[0087e0e]142 return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator };
[3ce0d440]143 } // ?*?
144
145 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
146 if ( r.numerator < (RationalImpl){0} ) {
[0087e0e]147 r.[numerator, denominator] = [-r.numerator, -r.denominator];
[3ce0d440]148 } // if
[0087e0e]149 return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator };
[3ce0d440]150 } // ?/?
151
152 // I/O
153
154 forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
155 istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
156 is | r.numerator | r.denominator;
[0087e0e]157 RationalImpl t = simplify( r.numerator, r.denominator );
[3ce0d440]158 r.numerator /= t;
159 r.denominator /= t;
160 return is;
161 } // ?|?
162
[200fcb3]163 forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
164 ostype & ?|?( ostype & os, Rational(RationalImpl) r ) {
165 return os | r.numerator | '/' | r.denominator;
166 } // ?|?
167
168 void ?|?( ostype & os, Rational(RationalImpl) r ) {
[ef346f7c]169 (ostype &)(os | r); nl( os );
[200fcb3]170 } // ?|?
171 } // distribution
[3ce0d440]172} // distribution
[630a82a]173
[0087e0e]174forall( otype RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
175Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) {
176 if ( y < 0 ) {
177 return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y };
178 } else {
179 return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y };
180 } // if
181}
182
[630a82a]183// conversion
184
[53a6c2a]185forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
[6c6455f]186double widen( Rational(RationalImpl) r ) {
187 return convert( r.numerator ) / convert( r.denominator );
188} // widen
189
[53a6c2a]190forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
[6c6455f]191Rational(RationalImpl) narrow( double f, RationalImpl md ) {
[3ce0d440]192 // http://www.ics.uci.edu/~eppstein/numth/frap.c
[6c6455f]193 if ( md <= (RationalImpl){1} ) { // maximum fractional digits too small?
194 return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction
195 } // if
196
197 // continued fraction coefficients
198 RationalImpl m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };
199 RationalImpl ai, t;
200
201 // find terms until denom gets too big
202 for ( ;; ) {
203 ai = convert( f );
204 if ( ! (m10 * ai + m11 <= md) ) break;
205 t = m00 * ai + m01;
206 m01 = m00;
207 m00 = t;
208 t = m10 * ai + m11;
209 m11 = m10;
210 m10 = t;
211 double temp = convert( ai );
212 if ( f == temp ) break; // prevent division by zero
213 f = 1 / (f - temp);
214 if ( f > (double)0x7FFFFFFF ) break; // representation failure
215 } // for
216 return (Rational(RationalImpl)){ m00, m10 };
217} // narrow
[53ba273]218
219// Local Variables: //
220// tab-width: 4 //
221// End: //
Note: See TracBrowser for help on using the repository browser.