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 1 08:25:06 2017
|
---|
15 | // Update Count : 33
|
---|
16 | //
|
---|
17 |
|
---|
18 | #ifndef RATIONAL_H
|
---|
19 | #define RATIONAL_H
|
---|
20 |
|
---|
21 | #include "iostream"
|
---|
22 |
|
---|
23 | // implementation
|
---|
24 | typedef long int RationalImpl;
|
---|
25 | struct Rational {
|
---|
26 | RationalImpl numerator, denominator; // invariant: denominator > 0
|
---|
27 | }; // Rational
|
---|
28 |
|
---|
29 | // constants
|
---|
30 | extern struct Rational 0;
|
---|
31 | extern struct Rational 1;
|
---|
32 |
|
---|
33 | // constructors
|
---|
34 | void ?{}( Rational * r );
|
---|
35 | void ?{}( Rational * r, RationalImpl n );
|
---|
36 | void ?{}( Rational * r, RationalImpl n, RationalImpl d );
|
---|
37 |
|
---|
38 | // getter for numerator/denominator
|
---|
39 | RationalImpl numerator( Rational r );
|
---|
40 | RationalImpl denominator( Rational r );
|
---|
41 | [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
|
---|
42 | // setter for numerator/denominator
|
---|
43 | RationalImpl numerator( Rational r, RationalImpl n );
|
---|
44 | RationalImpl denominator( Rational r, RationalImpl d );
|
---|
45 |
|
---|
46 | // comparison
|
---|
47 | int ?==?( Rational l, Rational r );
|
---|
48 | int ?!=?( Rational l, Rational r );
|
---|
49 | int ?<?( Rational l, Rational r );
|
---|
50 | int ?<=?( Rational l, Rational r );
|
---|
51 | int ?>?( Rational l, Rational r );
|
---|
52 | int ?>=?( Rational l, Rational r );
|
---|
53 |
|
---|
54 | // arithmetic
|
---|
55 | Rational -?( Rational r );
|
---|
56 | Rational ?+?( Rational l, Rational r );
|
---|
57 | Rational ?-?( Rational l, Rational r );
|
---|
58 | Rational ?*?( Rational l, Rational r );
|
---|
59 | Rational ?/?( Rational l, Rational r );
|
---|
60 |
|
---|
61 | // conversion
|
---|
62 | double widen( Rational r );
|
---|
63 | Rational narrow( double f, RationalImpl md );
|
---|
64 |
|
---|
65 | // I/O
|
---|
66 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
|
---|
67 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
|
---|
68 |
|
---|
69 | #endif // RATIONAL_H
|
---|
70 |
|
---|
71 | // Local Variables: //
|
---|
72 | // mode: c //
|
---|
73 | // tab-width: 4 //
|
---|
74 | // End: //
|
---|