source: libcfa/src/rational.hfa @ fa761c2

Last change on this file since fa761c2 was 92211d9, checked in by Peter A. Buhr <pabuhr@…>, 13 months ago

formatting

  • Property mode set to 100644
File size: 3.1 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 : Fri Oct  6 07:52:20 2023
15// Update Count     : 122
16//
17
18#pragma once
19
20#include "iostream.hfa"
21#include "math.trait.hfa"                                                               // arithmetic
22
23// implementation
24
25forall( T | arithmetic( T ) ) {
26        struct rational {
27                T numerator, denominator;                                               // invariant: denominator > 0
28        }; // rational
29
30        // constructors
31
32        void ?{}( rational(T) & r );
33        void ?{}( rational(T) & r, zero_t );
34        void ?{}( rational(T) & r, one_t );
35        void ?{}( rational(T) & r, T n );
36        void ?{}( rational(T) & r, T n, T d );
37
38        // numerator/denominator getter
39
40        T numerator( rational(T) r );
41        T denominator( rational(T) r );
42        [ T, T ] ?=?( & [ T, T ] dst, rational(T) src );
43
44        // numerator/denominator setter
45
46        T numerator( rational(T) r, T n );
47        T denominator( rational(T) r, T d );
48
49        // comparison
50
51        int ?==?( rational(T) l, rational(T) r );
52        int ?!=?( rational(T) l, rational(T) r );
53        int ?!=?( rational(T) l, zero_t );                                      // => !
54        int ?<?( rational(T) l, rational(T) r );
55        int ?<=?( rational(T) l, rational(T) r );
56        int ?>?( rational(T) l, rational(T) r );
57        int ?>=?( rational(T) l, rational(T) r );
58
59        // arithmetic
60
61        rational(T) +?( rational(T) r );
62        rational(T) -?( rational(T) r );
63        rational(T) ?+?( rational(T) l, rational(T) r );
64        rational(T) ?+=?( rational(T) & l, rational(T) r );
65        rational(T) ?+=?( rational(T) & l, one_t );                     // => ++?, ?++
66        rational(T) ?-?( rational(T) l, rational(T) r );
67        rational(T) ?-=?( rational(T) & l, rational(T) r );
68        rational(T) ?-=?( rational(T) & l, one_t );                     // => --?, ?--
69        rational(T) ?*?( rational(T) l, rational(T) r );
70        rational(T) ?*=?( rational(T) & l, rational(T) r );
71        rational(T) ?/?( rational(T) l, rational(T) r );
72        rational(T) ?/=?( rational(T) & l, rational(T) r );
73
74        // I/O
75        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
76        istype & ?|?( istype &, rational(T) & );
77
78        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
79                ostype & ?|?( ostype &, rational(T) );
80                OSTYPE_VOID( rational(T) );
81        } // distribution
82} // distribution
83
84forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {
85        rational(T) ?\?( rational(T) x, long int y );
86        rational(T) ?\=?( rational(T) & x, long int y );
87} // distribution
88
89// conversion
90forall( T | arithmetic( T ) | { double convert( T ); } )
91double widen( rational(T) r );
92forall( T | arithmetic( T ) | { double convert( T );  T convert( double );} )
93rational(T) narrow( double f, T md );
94
95// Local Variables: //
96// mode: c //
97// tab-width: 4 //
98// End: //
Note: See TracBrowser for help on using the repository browser.