source: src/libcfa/rational @ b72d4ed

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since b72d4ed was 6c6455f, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

second attempt at generic rational type with conversions to/from floating point

  • Property mode set to 100644
File size: 5.0 KB
Line 
1//
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.
6//
7// rational -- Rational numbers are numbers written as a ratio, i.e., as a fraction, where the numerator (top number)
8//     and the denominator (bottom number) are whole numbers. When creating and computing with rational numbers, results
9//     are constantly reduced to keep the numerator and denominator as small as possible.
10//
11// Author           : Peter A. Buhr
12// Created On       : Wed Apr  6 17:56:25 2016
13// Last Modified By : Peter A. Buhr
14// Last Modified On : Mon May 15 21:30:12 2017
15// Update Count     : 90
16//
17
18#ifndef RATIONAL_H
19#define RATIONAL_H
20
21#include "iostream"
22
23trait scalar( otype T ) {
24};
25
26trait arithmetic( otype T | scalar( T ) ) {
27        int !?( T );
28        int ?==?( T, T );
29        int ?!=?( T, T );
30        int ?<?( T, T );
31        int ?<=?( T, T );
32        int ?>?( T, T );
33        int ?>=?( T, T );
34        void ?{}( T *, zero_t );
35        void ?{}( T *, one_t );
36        T +?( T );
37        T -?( T );
38        T ?+?( T, T );
39        T ?-?( T, T );
40        T ?*?( T, T );
41        T ?/?( T, T );
42        T ?%?( T, T );
43        T ?/=?( T *, T );
44        T abs( T );
45};
46
47// implementation
48
49forall ( otype RationalImpl | arithmetic( RationalImpl ) )
50struct Rational {
51        RationalImpl numerator, denominator;                            // invariant: denominator > 0
52}; // Rational
53
54// constructors
55
56forall ( otype RationalImpl | arithmetic( RationalImpl ) )
57void ?{}( Rational(RationalImpl) * r );
58
59forall ( otype RationalImpl | arithmetic( RationalImpl ) )
60void ?{}( Rational(RationalImpl) * r, RationalImpl n );
61
62forall ( otype RationalImpl | arithmetic( RationalImpl ) )
63void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d );
64
65forall ( otype RationalImpl | arithmetic( RationalImpl ) )
66void ?{}( Rational(RationalImpl) * r, zero_t );
67
68forall ( otype RationalImpl | arithmetic( RationalImpl ) )
69void ?{}( Rational(RationalImpl) * r, one_t );
70
71// getter for numerator/denominator
72
73forall ( otype RationalImpl | arithmetic( RationalImpl ) )
74RationalImpl numerator( Rational(RationalImpl) r );
75
76forall ( otype RationalImpl | arithmetic( RationalImpl ) )
77RationalImpl denominator( Rational(RationalImpl) r );
78forall ( otype RationalImpl | arithmetic( RationalImpl ) )
79[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
80
81// setter for numerator/denominator
82
83forall ( otype RationalImpl | arithmetic( RationalImpl ) )
84RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
85
86forall ( otype RationalImpl | arithmetic( RationalImpl ) )
87RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
88
89// comparison
90
91forall ( otype RationalImpl | arithmetic( RationalImpl ) )
92int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
93
94forall ( otype RationalImpl | arithmetic( RationalImpl ) )
95int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
96
97forall ( otype RationalImpl | arithmetic( RationalImpl ) )
98int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
99
100forall ( otype RationalImpl | arithmetic( RationalImpl ) )
101int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
102
103forall ( otype RationalImpl | arithmetic( RationalImpl ) )
104int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
105
106forall ( otype RationalImpl | arithmetic( RationalImpl ) )
107int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
108
109// arithmetic
110
111forall ( otype RationalImpl | arithmetic( RationalImpl ) )
112Rational(RationalImpl) +?( Rational(RationalImpl) r );
113
114forall ( otype RationalImpl | arithmetic( RationalImpl ) )
115Rational(RationalImpl) -?( Rational(RationalImpl) r );
116
117forall ( otype RationalImpl | arithmetic( RationalImpl ) )
118Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
119
120forall ( otype RationalImpl | arithmetic( RationalImpl ) )
121Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
122
123forall ( otype RationalImpl | arithmetic( RationalImpl ) )
124Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
125
126forall ( otype RationalImpl | arithmetic( RationalImpl ) )
127Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
128
129// conversion
130forall ( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
131double widen( Rational(RationalImpl) r );
132forall ( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} )
133Rational(RationalImpl) narrow( double f, RationalImpl md );
134
135// I/O
136forall ( otype RationalImpl | arithmetic( RationalImpl ) )
137forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
138istype * ?|?( istype *, Rational(RationalImpl) * );
139
140forall ( otype RationalImpl | arithmetic( RationalImpl ) )
141forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
142ostype * ?|?( ostype *, Rational(RationalImpl ) );
143
144#endif // RATIONAL_H
145
146// Local Variables: //
147// mode: c //
148// tab-width: 4 //
149// End: //
Note: See TracBrowser for help on using the repository browser.