source: libcfa/src/rational.hfa @ 71f3d45

Last change on this file since 71f3d45 was 71f3d45, checked in by Michael Brooks <mlbrooks@…>, 3 weeks ago

Remove unnecessary assertion: printing a rational doesn't require the component type to be arithmetic.

May help (tbd) effort to move enum.hfa to builtins.

  • 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 Nov  8 17:02:09 2024
15// Update Count     : 126
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
31forall( T | arithmetic( T ) ) {
32        // constructors
33
34        void ?{}( rational(T) & r );
35        void ?{}( rational(T) & r, zero_t );
36        void ?{}( rational(T) & r, one_t );
37        void ?{}( rational(T) & r, T n );
38        void ?{}( rational(T) & r, T n, T d );
39
40        // numerator/denominator getter
41
42        T numerator( rational(T) r );
43        T denominator( rational(T) r );
44        [ T, T ] ?=?( & [ T, T ] dst, rational(T) src );
45
46        // numerator/denominator setter
47
48        T numerator( rational(T) r, T n );
49        T denominator( rational(T) r, T d );
50
51        // comparison
52
53        int ?==?( rational(T) l, rational(T) r );
54        int ?!=?( rational(T) l, rational(T) r );
55        int ?!=?( rational(T) l, zero_t );                                      // => !
56        int ?<?( rational(T) l, rational(T) r );
57        int ?<=?( rational(T) l, rational(T) r );
58        int ?>?( rational(T) l, rational(T) r );
59        int ?>=?( rational(T) l, rational(T) r );
60
61        // arithmetic
62
63        rational(T) +?( rational(T) r );
64        rational(T) -?( rational(T) r );
65        rational(T) ?+?( rational(T) l, rational(T) r );
66        rational(T) ?+=?( rational(T) & l, rational(T) r );
67        rational(T) ?+=?( rational(T) & l, one_t );                     // => ++?, ?++
68        rational(T) ?-?( rational(T) l, rational(T) r );
69        rational(T) ?-=?( rational(T) & l, rational(T) r );
70        rational(T) ?-=?( rational(T) & l, one_t );                     // => --?, ?--
71        rational(T) ?*?( rational(T) l, rational(T) r );
72        rational(T) ?*=?( rational(T) & l, rational(T) r );
73        rational(T) ?/?( rational(T) l, rational(T) r );
74        rational(T) ?/=?( rational(T) & l, rational(T) r );
75
76        // I/O
77        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
78        istype & ?|?( istype &, rational(T) & );
79} // distribution
80
81forall( T ) {
82        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
83                ostype & ?|?( ostype &, rational(T) );
84                OSTYPE_VOID( rational(T) );
85        } // distribution
86} // distribution
87
88forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {
89        rational(T) ?\?( rational(T) x, long int y );
90        rational(T) ?\=?( rational(T) & x, long int y );
91} // distribution
92
93// conversion
94forall( T | arithmetic( T ) | { double convert( T ); } )
95double widen( rational(T) r );
96forall( T | arithmetic( T ) | { double convert( T );  T convert( double );} )
97rational(T) narrow( double f, T md );
98
99// Local Variables: //
100// mode: c //
101// tab-width: 4 //
102// End: //
Note: See TracBrowser for help on using the repository browser.