source: libcfa/src/rational.hfa @ f5e37a4

Last change on this file since f5e37a4 was f5e37a4, checked in by Peter A. Buhr <pabuhr@…>, 3 weeks ago

small changes to rational

  • Property mode set to 100644
File size: 3.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
[f5e37a4]14// Last Modified On : Wed Nov 27 18:11:07 2024
15// Update Count     : 128
[bb82c03]16//
[f621a148]17
[53a6c2a]18#pragma once
[53ba273]19
[58b6d1b]20#include "iostream.hfa"
[541dbc09]21#include "math.trait.hfa"                                                               // arithmetic
[561f730]22
[f5e37a4]23// Implementation
[561f730]24
[44e2a5a]25forall( T ) {
[541dbc09]26        struct rational {
[5dc4c7e]27                T numerator, denominator;                                               // invariant: denominator > 0
[541dbc09]28        }; // rational
[44e2a5a]29}
[53ba273]30
[f5e37a4]31// Arithmetic, Relational
32
[44e2a5a]33forall( T | arithmetic( T ) ) {
[3ce0d440]34        // constructors
[561f730]35
[541dbc09]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 );
[561f730]41
[3ce0d440]42        // numerator/denominator getter
[561f730]43
[541dbc09]44        T numerator( rational(T) r );
45        T denominator( rational(T) r );
[92211d9]46        [ T, T ] ?=?( & [ T, T ] dst, rational(T) src );
[561f730]47
[3ce0d440]48        // numerator/denominator setter
[561f730]49
[541dbc09]50        T numerator( rational(T) r, T n );
51        T denominator( rational(T) r, T d );
[630a82a]52
[3ce0d440]53        // comparison
[561f730]54
[541dbc09]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 );
[561f730]62
[3ce0d440]63        // arithmetic
[53a6c2a]64
[541dbc09]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 );
[71f3d45]77} // distribution
[561f730]78
[f5e37a4]79// I/O
80
[71f3d45]81forall( T ) {
[f5e37a4]82        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | arithmetic( T ) )
83        istype & ?|?( istype &, rational(T) & );
84
[5dc4c7e]85        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
[541dbc09]86                ostype & ?|?( ostype &, rational(T) );
[5454d77]87                OSTYPE_VOID( rational(T) );
[200fcb3]88        } // distribution
[3ce0d440]89} // distribution
[630a82a]90
[f5e37a4]91// Exponentiation
92
[541dbc09]93forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {
94        rational(T) ?\?( rational(T) x, long int y );
95        rational(T) ?\=?( rational(T) & x, long int y );
[5dc4c7e]96} // distribution
[0087e0e]97
[f5e37a4]98// Conversion
99
[541dbc09]100forall( T | arithmetic( T ) | { double convert( T ); } )
101double widen( rational(T) r );
102forall( T | arithmetic( T ) | { double convert( T );  T convert( double );} )
103rational(T) narrow( double f, T md );
[630a82a]104
[53ba273]105// Local Variables: //
106// mode: c //
107// tab-width: 4 //
108// End: //
Note: See TracBrowser for help on using the repository browser.