Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational

    r561f730 rf621a148  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Sun May 14 16:49:13 2017
    15 // Update Count     : 78
     14// Last Modified On : Mon May  1 08:25:06 2017
     15// Update Count     : 33
    1616//
    1717
     
    2121#include "iostream"
    2222
    23 trait scalar( otype T ) {
    24 };
    25 
    26 trait arithmetic( otype T | scalar( T ) ) {
    27         int !?( T );
    28         int ?==?( T, T );
    29         int ?!=?( T, T );
    30         int ?<?( T, T );
    31         int ?<=?( T, T );
    32         int ?>?( T, T );
    33         int ?>=?( T, T );
    34         void ?{}( T *, zero_t );
    35         void ?{}( T *, one_t );
    36         T +?( T );
    37         T -?( T );
    38         T ?+?( T, T );
    39         T ?-?( T, T );
    40         T ?*?( T, T );
    41         T ?/?( T, T );
    42         T ?%?( T, T );
    43         T ?/=?( T *, T );
    44         T abs( T );
    45 };
    46 
    4723// implementation
    48 
    49 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     24typedef long int RationalImpl;
    5025struct Rational {
    51         RationalImpl numerator, denominator;                            // invariant: denominator > 0
     26        RationalImpl numerator, denominator;                                    // invariant: denominator > 0
    5227}; // Rational
    5328
     29// constants
     30extern struct Rational 0;
     31extern struct Rational 1;
     32
    5433// constructors
    55 
    56 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    57 void ?{}( Rational(RationalImpl) * r );
    58 
    59 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    60 void ?{}( Rational(RationalImpl) * r, RationalImpl n );
    61 
    62 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    63 void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d );
    64 
    65 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    66 void ?{}( Rational(RationalImpl) * r, zero_t );
    67 
    68 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    69 void ?{}( Rational(RationalImpl) * r, one_t );
     34void ?{}( Rational * r );
     35void ?{}( Rational * r, RationalImpl n );
     36void ?{}( Rational * r, RationalImpl n, RationalImpl d );
    7037
    7138// getter for numerator/denominator
    72 
    73 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    74 RationalImpl numerator( Rational(RationalImpl) r );
    75 
    76 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    77 RationalImpl denominator( Rational(RationalImpl) r );
    78 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
    80 
     39RationalImpl numerator( Rational r );
     40RationalImpl denominator( Rational r );
     41[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
    8142// setter for numerator/denominator
    82 
    83 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    84 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
    85 
    86 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    87 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
     43RationalImpl numerator( Rational r, RationalImpl n );
     44RationalImpl denominator( Rational r, RationalImpl d );
    8845
    8946// comparison
    90 
    91 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    92 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    93 
    94 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    95 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    96 
    97 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    98 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    99 
    100 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    101 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    102 
    103 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    104 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    105 
    106 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    107 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     47int ?==?( Rational l, Rational r );
     48int ?!=?( Rational l, Rational r );
     49int ?<?( Rational l, Rational r );
     50int ?<=?( Rational l, Rational r );
     51int ?>?( Rational l, Rational r );
     52int ?>=?( Rational l, Rational r );
    10853
    10954// arithmetic
    110 
    111 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    112 Rational(RationalImpl) +?( Rational(RationalImpl) r );
    113 
    114 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    115 Rational(RationalImpl) -?( Rational(RationalImpl) r );
    116 
    117 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    118 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    119 
    120 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    121 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    122 
    123 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    124 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    125 
    126 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    127 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     55Rational -?( Rational r );
     56Rational ?+?( Rational l, Rational r );
     57Rational ?-?( Rational l, Rational r );
     58Rational ?*?( Rational l, Rational r );
     59Rational ?/?( Rational l, Rational r );
    12860
    12961// conversion
    130 // forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    131 // double widen( Rational(RationalImpl) r );
    132 // forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    133 // Rational(RationalImpl) narrow( double f, RationalImpl md );
     62double widen( Rational r );
     63Rational narrow( double f, RationalImpl md );
    13464
    13565// I/O
    136 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    137 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
    138 istype * ?|?( istype *, Rational(RationalImpl) * );
    139 
    140 forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    141 forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
    142 ostype * ?|?( ostype *, Rational(RationalImpl ) );
     66forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
     67forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
    14368
    14469#endif // RATIONAL_H
Note: See TracChangeset for help on using the changeset viewer.