source: libcfa/src/rational.hfa @ ed96731

Last change on this file since ed96731 was 44e2a5a, checked in by Peter A. Buhr <pabuhr@…>, 11 days ago

remove assertions from rational structure declaration

  • Property mode set to 100644
File size: 3.1 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
[44e2a5a]14// Last Modified On : Fri Nov  8 17:02:09 2024
15// Update Count     : 126
[bb82c03]16//
[f621a148]17
[53a6c2a]18#pragma once
[53ba273]19
[58b6d1b]20#include "iostream.hfa"
[541dbc09]21#include "math.trait.hfa"                                                               // arithmetic
[561f730]22
[630a82a]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
[44e2a5a]31forall( T | arithmetic( T ) ) {
[3ce0d440]32        // constructors
[561f730]33
[541dbc09]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 );
[561f730]39
[3ce0d440]40        // numerator/denominator getter
[561f730]41
[541dbc09]42        T numerator( rational(T) r );
43        T denominator( rational(T) r );
[92211d9]44        [ T, T ] ?=?( & [ T, T ] dst, rational(T) src );
[561f730]45
[3ce0d440]46        // numerator/denominator setter
[561f730]47
[541dbc09]48        T numerator( rational(T) r, T n );
49        T denominator( rational(T) r, T d );
[630a82a]50
[3ce0d440]51        // comparison
[561f730]52
[541dbc09]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 );
[561f730]60
[3ce0d440]61        // arithmetic
[53a6c2a]62
[541dbc09]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 );
[561f730]75
[3ce0d440]76        // I/O
[5dc4c7e]77        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
[541dbc09]78        istype & ?|?( istype &, rational(T) & );
[561f730]79
[5dc4c7e]80        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
[541dbc09]81                ostype & ?|?( ostype &, rational(T) );
[5454d77]82                OSTYPE_VOID( rational(T) );
[200fcb3]83        } // distribution
[3ce0d440]84} // distribution
[630a82a]85
[541dbc09]86forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {
87        rational(T) ?\?( rational(T) x, long int y );
88        rational(T) ?\=?( rational(T) & x, long int y );
[5dc4c7e]89} // distribution
[0087e0e]90
[630a82a]91// conversion
[541dbc09]92forall( T | arithmetic( T ) | { double convert( T ); } )
93double widen( rational(T) r );
94forall( T | arithmetic( T ) | { double convert( T );  T convert( double );} )
95rational(T) narrow( double f, T md );
[630a82a]96
[53ba273]97// Local Variables: //
98// mode: c //
99// tab-width: 4 //
100// End: //
Note: See TracBrowser for help on using the repository browser.