source: libcfa/src/rational.hfa @ 96ef156

Last change on this file since 96ef156 was eae8b37, checked in by JiadaL <j82liang@…>, 2 weeks ago

Move enum.hfa/enum.cfa to prelude

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