Ignore:
Timestamp:
Jul 20, 2021, 6:30:29 PM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
d30804a
Parents:
8477fc4
Message:

formatting, use new math trait in rational numbers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/rational.hfa

    r8477fc4 r5dc4c7e  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Tue Mar 26 23:16:10 2019
    15 // Update Count     : 109
     14// Last Modified On : Tue Jul 20 17:45:29 2021
     15// Update Count     : 118
    1616//
    1717
     
    1919
    2020#include "iostream.hfa"
    21 
    22 trait scalar( T ) {
    23 };
    24 
    25 trait arithmetic( T | scalar( T ) ) {
    26         int !?( T );
    27         int ?==?( T, T );
    28         int ?!=?( T, T );
    29         int ?<?( T, T );
    30         int ?<=?( T, T );
    31         int ?>?( T, T );
    32         int ?>=?( T, T );
    33         void ?{}( T &, zero_t );
    34         void ?{}( T &, one_t );
    35         T +?( T );
    36         T -?( T );
    37         T ?+?( T, T );
    38         T ?-?( T, T );
    39         T ?*?( T, T );
    40         T ?/?( T, T );
    41         T ?%?( T, T );
    42         T ?/=?( T &, T );
    43         T abs( T );
    44 };
     21#include "math.trait.hfa"                                                               // Arithmetic
    4522
    4623// implementation
    4724
    48 forall( RationalImpl | arithmetic( RationalImpl ) ) {
     25forall( T | Arithmetic( T ) ) {
    4926        struct Rational {
    50                 RationalImpl numerator, denominator;                    // invariant: denominator > 0
     27                T numerator, denominator;                                               // invariant: denominator > 0
    5128        }; // Rational
    5229
    5330        // constructors
    5431
    55         void ?{}( Rational(RationalImpl) & r );
    56         void ?{}( Rational(RationalImpl) & r, RationalImpl n );
    57         void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d );
    58         void ?{}( Rational(RationalImpl) & r, zero_t );
    59         void ?{}( Rational(RationalImpl) & r, one_t );
     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 );
    6037
    6138        // numerator/denominator getter
    6239
    63         RationalImpl numerator( Rational(RationalImpl) r );
    64         RationalImpl denominator( Rational(RationalImpl) r );
    65         [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
     40        T numerator( Rational(T) r );
     41        T denominator( Rational(T) r );
     42        [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src );
    6643
    6744        // numerator/denominator setter
    6845
    69         RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
    70         RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
     46        T numerator( Rational(T) r, T n );
     47        T denominator( Rational(T) r, T d );
    7148
    7249        // comparison
    7350
    74         int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    75         int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    76         int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    77         int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    78         int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    79         int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     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 );
    8058
    8159        // arithmetic
    8260
    83         Rational(RationalImpl) +?( Rational(RationalImpl) r );
    84         Rational(RationalImpl) -?( Rational(RationalImpl) r );
    85         Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    86         Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    87         Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    88         Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     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 );
    8973
    9074        // I/O
    91         forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
    92         istype & ?|?( istype &, Rational(RationalImpl) & );
     75        forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
     76        istype & ?|?( istype &, Rational(T) & );
    9377
    94         forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
    95                 ostype & ?|?( ostype &, Rational(RationalImpl) );
    96                 void ?|?( ostype &, Rational(RationalImpl) );
     78        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
     79                ostype & ?|?( ostype &, Rational(T) );
     80                void ?|?( ostype &, Rational(T) );
    9781        } // distribution
    9882} // distribution
    9983
    100 forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} )
    101 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y );
     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
    10288
    10389// conversion
    104 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
    105 double widen( Rational(RationalImpl) r );
    106 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} )
    107 Rational(RationalImpl) narrow( double f, RationalImpl md );
     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 );
    10894
    10995// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.