source: src/libcfa/rational @ f621a148

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 f621a148 was f621a148, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change to implementation type for rational and add to test suite

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