source: libcfa/src/rational.hfa@ a017ee7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since a017ee7 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[bb82c03]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.
[bb82c03]6//
[630a82a]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.
[bb82c03]10//
[53ba273]11// Author : Peter A. Buhr
12// Created On : Wed Apr 6 17:56:25 2016
13// Last Modified By : Peter A. Buhr
[0087e0e]14// Last Modified On : Tue Mar 26 23:16:10 2019
15// Update Count : 109
[bb82c03]16//
[f621a148]17
[53a6c2a]18#pragma once
[53ba273]19
[58b6d1b]20#include "iostream.hfa"
[53ba273]21
[fd54fef]22trait scalar( T ) {
[561f730]23};
24
[fd54fef]25trait arithmetic( T | scalar( T ) ) {
[561f730]26 int !?( T );
27 int ?==?( T, T );
28 int ?!=?( T, T );
29 int ?<?( T, T );
30 int ?<=?( T, T );
31 int ?>?( T, T );
32 int ?>=?( T, T );
[a493682]33 void ?{}( T &, zero_t );
34 void ?{}( T &, one_t );
[561f730]35 T +?( T );
36 T -?( T );
37 T ?+?( T, T );
38 T ?-?( T, T );
39 T ?*?( T, T );
40 T ?/?( T, T );
41 T ?%?( T, T );
[53a8e68]42 T ?/=?( T &, T );
[561f730]43 T abs( T );
44};
45
[630a82a]46// implementation
[561f730]47
[fd54fef]48forall( RationalImpl | arithmetic( RationalImpl ) ) {
[3ce0d440]49 struct Rational {
50 RationalImpl numerator, denominator; // invariant: denominator > 0
51 }; // Rational
[53ba273]52
[3ce0d440]53 // constructors
[561f730]54
[3ce0d440]55 void ?{}( Rational(RationalImpl) & r );
56 void ?{}( Rational(RationalImpl) & r, RationalImpl n );
57 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d );
58 void ?{}( Rational(RationalImpl) & r, zero_t );
59 void ?{}( Rational(RationalImpl) & r, one_t );
[561f730]60
[3ce0d440]61 // numerator/denominator getter
[561f730]62
[3ce0d440]63 RationalImpl numerator( Rational(RationalImpl) r );
64 RationalImpl denominator( Rational(RationalImpl) r );
65 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
[561f730]66
[3ce0d440]67 // numerator/denominator setter
[561f730]68
[3ce0d440]69 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
70 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
[630a82a]71
[3ce0d440]72 // comparison
[561f730]73
[3ce0d440]74 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
75 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
76 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
77 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
78 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
79 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
[561f730]80
[3ce0d440]81 // arithmetic
[53a6c2a]82
[3ce0d440]83 Rational(RationalImpl) +?( Rational(RationalImpl) r );
84 Rational(RationalImpl) -?( Rational(RationalImpl) r );
85 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
86 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
87 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
88 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
[561f730]89
[3ce0d440]90 // I/O
[fd54fef]91 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
[3ce0d440]92 istype & ?|?( istype &, Rational(RationalImpl) & );
[561f730]93
[fd54fef]94 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
[200fcb3]95 ostype & ?|?( ostype &, Rational(RationalImpl) );
96 void ?|?( ostype &, Rational(RationalImpl) );
97 } // distribution
[3ce0d440]98} // distribution
[630a82a]99
[fd54fef]100forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} )
[0087e0e]101Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y );
102
[630a82a]103// conversion
[fd54fef]104forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
[6c6455f]105double widen( Rational(RationalImpl) r );
[fd54fef]106forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double );} )
[6c6455f]107Rational(RationalImpl) narrow( double f, RationalImpl md );
[630a82a]108
[53ba273]109// Local Variables: //
110// mode: c //
111// tab-width: 4 //
112// End: //
Note: See TracBrowser for help on using the repository browser.