source: libcfa/src/rational.hfa @ c8238c0

ADTast-experimental
Last change on this file since c8238c0 was 5dc4c7e, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

formatting, use new math trait in rational numbers

  • 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
[5dc4c7e]14// Last Modified On : Tue Jul 20 17:45:29 2021
15// Update Count     : 118
[bb82c03]16//
[f621a148]17
[53a6c2a]18#pragma once
[53ba273]19
[58b6d1b]20#include "iostream.hfa"
[5dc4c7e]21#include "math.trait.hfa"                                                               // Arithmetic
[561f730]22
[630a82a]23// implementation
[561f730]24
[5dc4c7e]25forall( T | Arithmetic( T ) ) {
[3ce0d440]26        struct Rational {
[5dc4c7e]27                T numerator, denominator;                                               // invariant: denominator > 0
[3ce0d440]28        }; // Rational
[53ba273]29
[3ce0d440]30        // constructors
[561f730]31
[5dc4c7e]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 );
[561f730]37
[3ce0d440]38        // numerator/denominator getter
[561f730]39
[5dc4c7e]40        T numerator( Rational(T) r );
41        T denominator( Rational(T) r );
42        [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src );
[561f730]43
[3ce0d440]44        // numerator/denominator setter
[561f730]45
[5dc4c7e]46        T numerator( Rational(T) r, T n );
47        T denominator( Rational(T) r, T d );
[630a82a]48
[3ce0d440]49        // comparison
[561f730]50
[5dc4c7e]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 );
[561f730]58
[3ce0d440]59        // arithmetic
[53a6c2a]60
[5dc4c7e]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 );
[561f730]73
[3ce0d440]74        // I/O
[5dc4c7e]75        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
76        istype & ?|?( istype &, Rational(T) & );
[561f730]77
[5dc4c7e]78        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
79                ostype & ?|?( ostype &, Rational(T) );
80                void ?|?( ostype &, Rational(T) );
[200fcb3]81        } // distribution
[3ce0d440]82} // distribution
[630a82a]83
[5dc4c7e]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
[0087e0e]88
[630a82a]89// conversion
[5dc4c7e]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 );
[630a82a]94
[53ba273]95// Local Variables: //
96// mode: c //
97// tab-width: 4 //
98// End: //
Note: See TracBrowser for help on using the repository browser.