Changeset 630a82a for src/libcfa/rational
 Timestamp:
 Apr 8, 2016, 5:22:29 PM (9 years ago)
 Branches:
 ADT, aaronthesis, armeh, astexperimental, cleanupdtors, ctor, deferred_resn, demangler, enum, forallpointerdecay, gc_noraii, jacob/cs343translation, jenkinssandbox, master, memory, newast, newastuniqueexpr, newenv, no_list, persistentindexer, pthreademulation, qualifiedEnum, resolvnew, string, with_gc
 Children:
 3da470c
 Parents:
 3d9b5da
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/libcfa/rational
r3d9b5da r630a82a 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // rational  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. 8 10 // 9 11 // Author : Peter A. Buhr 10 12 // Created On : Wed Apr 6 17:56:25 2016 11 13 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 7 17:23:36201613 // Update Count : 914 // Last Modified On : Fri Apr 8 11:38:27 2016 15 // Update Count : 15 14 16 // 15 17 16 18 #include "iostream" 17 19 20 // implementation 18 21 struct Rational { 19 22 long int numerator, denominator; // invariant: denominator > 0 20 23 }; // Rational 21 24 25 // constants 22 26 extern struct Rational 0; 23 27 extern struct Rational 1; 24 28 25 long int gcd( long int a, long int b ); 26 long int simplify( long int *n, long int *d ); 27 Rational rational(); // constructor 28 Rational rational( long int n ); // constructor 29 Rational rational( long int n, long int d ); // constructor 29 // constructors 30 Rational rational(); 31 Rational rational( long int n ); 32 Rational rational( long int n, long int d ); 33 34 // getter/setter for numerator/denominator 30 35 long int numerator( Rational r ); 31 36 long int numerator( Rational r, long int n ); 37 long int denominator( Rational r ); 32 38 long int denominator( Rational r, long int d ); 39 40 // comparison 33 41 int ?==?( Rational l, Rational r ); 34 42 int ?!=?( Rational l, Rational r ); … … 37 45 int ?>?( Rational l, Rational r ); 38 46 int ?>=?( Rational l, Rational r ); 47 48 // arithmetic 39 49 Rational ?( Rational r ); 40 50 Rational ?+?( Rational l, Rational r ); … … 42 52 Rational ?*?( Rational l, Rational r ); 43 53 Rational ?/?( Rational l, Rational r ); 54 55 // conversion 44 56 double widen( Rational r ); 45 57 Rational narrow( double f, long int md ); 58 59 // I/O 46 60 forall( dtype istype  istream( istype ) ) istype * ??( istype *, Rational * ); 47 61 forall( dtype ostype  ostream( ostype ) ) ostype * ??( ostype *, Rational );
Note: See TracChangeset
for help on using the changeset viewer.