Changes in / [6acd020:3d7d407]


Ignore:
Files:
1 deleted
24 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/andrew_beach_MMath/code/cond-catch.cfa

    r6acd020 r3d7d407  
    1919                throw_exception();
    2020        } catch (empty_exception * exc ; should_catch) {
    21                 asm volatile ("# catch block (conditional)");
     21                // ...
    2222        }
    2323}
     
    3737                        cond_catch();
    3838                } catch (empty_exception * exc) {
    39                         asm volatile ("# catch block (unconditional)");
     39                        // ...
    4040                }
    4141        }
  • doc/theses/andrew_beach_MMath/code/cond-catch.cpp

    r6acd020 r3d7d407  
    2222                        throw;
    2323                }
    24                 asm volatile ("# catch block (conditional)");
    2524        }
    2625}
     
    4039                        cond_catch();
    4140                } catch (EmptyException &) {
    42                         asm volatile ("# catch block (unconditional)");
     41                        // ...
    4342                }
    4443    }
  • doc/theses/andrew_beach_MMath/code/cond-fixup.cfa

    r6acd020 r3d7d407  
    1919                throw_exception();
    2020        } catchResume (empty_exception * exc ; should_catch) {
    21                 asm volatile ("# fixup block (conditional)");
     21                // ...
    2222        }
    2323}
     
    3737                        cond_catch();
    3838                } catchResume (empty_exception * exc) {
    39                         asm volatile ("# fixup block (unconditional)");
     39                        // ...
    4040                }
    4141        }
  • doc/theses/andrew_beach_MMath/code/cross-catch.cfa

    r6acd020 r3d7d407  
    77EHM_EXCEPTION(not_raised_exception)();
    88
    9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt);
    10 
    119int main(int argc, char * argv[]) {
    1210        unsigned int times = 1;
    13         bool should_throw = false;
     11        unsigned int total_frames = 1;
    1412        if (1 < argc) {
    1513                times = strtol(argv[1], 0p, 10);
     14        }
     15        if (2 < argc) {
     16                total_frames = strtol(argv[2], 0p, 10);
    1617        }
    1718
     
    1920        for (unsigned int count = 0 ; count < times ; ++count) {
    2021                try {
    21                         asm volatile ("# try block" : "=rm" (should_throw));
    22                         if (should_throw) {
    23                                 throw (not_raised_exception){&not_vt};
    24                         }
     22                        // ...
    2523                } catch (not_raised_exception *) {
    26                         asm volatile ("# catch block");
     24                        // ...
    2725                }
    2826        }
  • doc/theses/andrew_beach_MMath/code/cross-catch.cpp

    r6acd020 r3d7d407  
    1111int main(int argc, char * argv[]) {
    1212        unsigned int times = 1;
    13         bool should_throw = false;
    1413        if (1 < argc) {
    1514                times = strtol(argv[1], nullptr, 10);
     
    1918        for (unsigned int count = 0 ; count < times ; ++count) {
    2019                try {
    21                         asm volatile ("# try block" : "=rm" (should_throw));
    22                         if (should_throw) {
    23                                 throw NotRaisedException();
    24                         }
     20                        // ...
    2521                } catch (NotRaisedException &) {
    26                         asm volatile ("# catch block");
     22                        // ...
    2723                }
    2824        }
  • doc/theses/andrew_beach_MMath/code/cross-finally.cfa

    r6acd020 r3d7d407  
    55#include <stdlib.hfa>
    66
    7 EHM_EXCEPTION(not_raised_exception)();
    8 
    9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt);
    10 
    117int main(int argc, char * argv[]) {
    128        unsigned int times = 1;
    13         bool should_throw = false;
     9        unsigned int total_frames = 1;
    1410        if (1 < argc) {
    1511                times = strtol(argv[1], 0p, 10);
     12        }
     13        if (2 < argc) {
     14                total_frames = strtol(argv[2], 0p, 10);
    1615        }
    1716
    1817        Time start_time = timeHiRes();
    1918        for (unsigned int count = 0 ; count < times ; ++count) {
    20                 try {
    21                         asm volatile ("# try block" : "=rm" (should_throw));
    22                         if (should_throw) {
    23                                 throw (not_raised_exception){&not_vt};
    24                         }
     19                 try {
     20                        // ...
    2521                } finally {
    26                         asm volatile ("# finally block");
     22                        // ...
    2723                }
    2824        }
  • doc/theses/andrew_beach_MMath/code/cross-resume.cfa

    r6acd020 r3d7d407  
    2020        for (unsigned int count = 0 ; count < times ; ++count) {
    2121                try {
    22                         asm volatile ("");
     22                        // ...
    2323                } catchResume (not_raised_exception *) {
    24                         asm volatile ("");
     24                        // ...
    2525                }
    2626        }
  • doc/theses/andrew_beach_MMath/code/resume-detor.cfa

    r6acd020 r3d7d407  
    1212
    1313void ^?{}(WithDestructor & this) {
    14         asm volatile ("# destructor body");
     14    // ...
    1515}
    1616
    1717void unwind_destructor(unsigned int frames) {
    18         if (frames) {
     18    if (frames) {
    1919
    20                 WithDestructor object;
    21                 unwind_destructor(frames - 1);
    22         } else {
    23                 throwResume (empty_exception){&empty_vt};
    24         }
     20        WithDestructor object;
     21        unwind_destructor(frames - 1);
     22    } else {
     23        throwResume (empty_exception){&empty_vt};
     24    }
    2525}
    2626
     
    3636
    3737        Time start_time = timeHiRes();
    38         for (int count = 0 ; count < times ; ++count) {
    39                 try {
    40                         unwind_destructor(total_frames);
    41                 } catchResume (empty_exception *) {
    42                         asm volatile ("# fixup block");
    43                 }
    44         }
     38    for (int count = 0 ; count < times ; ++count) {
     39        try {
     40            unwind_destructor(total_frames);
     41        } catchResume (empty_exception *) {
     42            // ...
     43        }
     44    }
    4545        Time end_time = timeHiRes();
    4646        sout | "Run-Time (ns): " | (end_time - start_time)`ns;
  • doc/theses/andrew_beach_MMath/code/resume-empty.cfa

    r6acd020 r3d7d407  
    3232                        unwind_empty(total_frames);
    3333                } catchResume (empty_exception *) {
    34                         asm volatile ("# fixup block");
     34                        // ...
    3535                }
    3636        }
  • doc/theses/andrew_beach_MMath/code/resume-finally.cfa

    r6acd020 r3d7d407  
    1414                        unwind_finally(frames - 1);
    1515                } finally {
    16                         asm volatile ("# finally block");
     16                        // ...
    1717                }
    1818        } else {
     
    3636                        unwind_finally(total_frames);
    3737                } catchResume (empty_exception *) {
    38                         asm volatile ("# fixup block");
     38                        // ...
    3939                }
    4040        }
  • doc/theses/andrew_beach_MMath/code/resume-other.cfa

    r6acd020 r3d7d407  
    1616                        unwind_other(frames - 1);
    1717                } catchResume (not_raised_exception *) {
    18                         asm volatile ("# fixup block (stack)");
     18                        // ...
    1919                }
    2020        } else {
     
    3838                        unwind_other(total_frames);
    3939                } catchResume (empty_exception *) {
    40                         asm volatile ("# fixup block (base)");
     40                        // ...
    4141                }
    4242        }
  • doc/theses/andrew_beach_MMath/code/throw-detor.cfa

    r6acd020 r3d7d407  
    1212
    1313void ^?{}(WithDestructor & this) {
    14         asm volatile ("# destructor body");
     14        // ...
    1515}
    1616
     
    3939                        unwind_destructor(total_frames);
    4040                } catch (empty_exception *) {
    41                         asm volatile ("# catch block");
     41                        // ...
    4242                }
    4343        }
  • doc/theses/andrew_beach_MMath/code/throw-detor.cpp

    r6acd020 r3d7d407  
    1010
    1111struct WithDestructor {
    12         ~WithDestructor() {
    13                 asm volatile ("# destructor body");
    14         }
     12        ~WithDestructor() {}
    1513};
    1614
     
    3937                        unwind_destructor(total_frames);
    4038                } catch (EmptyException &) {
    41                         asm volatile ("# catch block");
     39                        // ...
    4240                }
    4341        }
  • doc/theses/andrew_beach_MMath/code/throw-empty.cfa

    r6acd020 r3d7d407  
    3232                        unwind_empty(total_frames);
    3333                } catch (empty_exception *) {
    34                         asm volatile ("# catch block");
     34                        // ...
    3535                }
    3636        }
  • doc/theses/andrew_beach_MMath/code/throw-empty.cpp

    r6acd020 r3d7d407  
    3232                        unwind_empty(total_frames);
    3333                } catch (EmptyException &) {
    34                         asm volatile ("# catch block");
     34                        // ...
    3535                }
    3636        }
  • doc/theses/andrew_beach_MMath/code/throw-finally.cfa

    r6acd020 r3d7d407  
    1414                        unwind_finally(frames - 1);
    1515                } finally {
    16                         asm volatile ("# finally block");
     16                        // ...
    1717                }
    1818        } else {
     
    3636                        unwind_finally(total_frames);
    3737                } catch (empty_exception *) {
    38                         asm volatile ("# catch block");
     38                        // ...
    3939                }
    4040        }
  • doc/theses/andrew_beach_MMath/code/throw-other.cfa

    r6acd020 r3d7d407  
    1616                        unwind_other(frames - 1);
    1717                } catch (not_raised_exception *) {
    18                         asm volatile ("# catch block (stack)");
     18                        // ...
    1919                }
    2020        } else {
     
    3838                        unwind_other(total_frames);
    3939                } catch (empty_exception *) {
    40                         asm volatile ("# catch block (base)");
     40                        // ...
    4141                }
    4242        }
  • doc/theses/andrew_beach_MMath/code/throw-other.cpp

    r6acd020 r3d7d407  
    1616                        unwind_other(frames - 1);
    1717                } catch (NotRaisedException &) {
    18                         asm volatile ("# catch block (stack)");
     18                        // ...
    1919                }
    2020        } else {
     
    3838                        unwind_other(total_frames);
    3939                } catch (EmptyException &) {
    40                         asm volatile ("# catch block (base)");
     40                        // ...
    4141                }
    4242        }
  • libcfa/prelude/builtins.c

    r6acd020 r3d7d407  
    1010// Created On       : Fri Jul 21 16:21:03 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jul 20 17:31:40 2021
    13 // Update Count     : 128
     12// Last Modified On : Tue Apr 13 17:26:32 2021
     13// Update Count     : 117
    1414//
    1515
     
    7878
    7979static inline {
    80         forall( T | { T ?+=?( T &, one_t ); } )
    81         T ++?( T & x ) { return x += 1; }
     80        forall( DT & | { DT & ?+=?( DT &, one_t ); } )
     81        DT & ++?( DT & x ) { return x += 1; }
    8282
    83         forall( T | { T ?+=?( T &, one_t ); } )
    84         T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
     83        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
     84        DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
    8585
    86         forall( T | { T ?-=?( T &, one_t ); } )
    87         T --?( T & x ) { return x -= 1; }
     86        forall( DT & | { DT & ?-=?( DT &, one_t ); } )
     87        DT & --?( DT & x ) { return x -= 1; }
    8888
    89         forall( T | { T ?-=?( T &, one_t ); } )
    90         T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
     89        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
     90        DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
    9191
    92         forall( T | { int ?!=?( T, zero_t ); } )
    93         int !?( T & x ) { return !( x != 0 ); }
     92        forall( DT & | { int ?!=?( const DT &, zero_t ); } )
     93        int !?( const DT & x ) { return !( x != 0 ); }
    9494} // distribution
    9595
  • libcfa/src/Makefile.am

    r6acd020 r3d7d407  
    1111## Created On       : Sun May 31 08:54:01 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Fri Jul 16 16:00:40 2021
    14 ## Update Count     : 255
     13## Last Modified On : Sat Apr 24 09:09:56 2021
     14## Update Count     : 254
    1515###############################################################################
    1616
     
    4545        exception.h \
    4646        gmp.hfa \
    47         math.trait.hfa \
    4847        math.hfa \
    4948        time_t.hfa \
  • libcfa/src/rational.cfa

    r6acd020 r3d7d407  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jul 20 16:30:06 2021
    13 // Update Count     : 193
     12// Last Modified On : Sat Feb  8 17:56:36 2020
     13// Update Count     : 187
    1414//
    1515
     
    1818#include "stdlib.hfa"
    1919
    20 forall( T | Arithmetic( T ) ) {
     20forall( RationalImpl | arithmetic( RationalImpl ) ) {
    2121        // helper routines
    2222
    2323        // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce
    2424        // rationals.  alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
    25         static T gcd( T a, T b ) {
     25        static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
    2626                for ( ;; ) {                                                                    // Euclid's algorithm
    27                         T r = a % b;
    28                   if ( r == (T){0} ) break;
     27                        RationalImpl r = a % b;
     28                  if ( r == (RationalImpl){0} ) break;
    2929                        a = b;
    3030                        b = r;
     
    3333        } // gcd
    3434
    35         static T simplify( T & n, T & d ) {
    36                 if ( d == (T){0} ) {
     35        static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
     36                if ( d == (RationalImpl){0} ) {
    3737                        abort | "Invalid rational number construction: denominator cannot be equal to 0.";
    3838                } // exit
    39                 if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator
     39                if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator
    4040                return gcd( abs( n ), d );                                              // simplify
    4141        } // Rationalnumber::simplify
     
    4343        // constructors
    4444
    45         void ?{}( Rational(T) & r, zero_t ) {
    46                 r{ (T){0}, (T){1} };
    47         } // rational
    48 
    49         void ?{}( Rational(T) & r, one_t ) {
    50                 r{ (T){1}, (T){1} };
    51         } // rational
    52 
    53         void ?{}( Rational(T) & r ) {
    54                 r{ (T){0}, (T){1} };
    55         } // rational
    56 
    57         void ?{}( Rational(T) & r, T n ) {
    58                 r{ n, (T){1} };
    59         } // rational
    60 
    61         void ?{}( Rational(T) & r, T n, T d ) {
    62                 T t = simplify( n, d );                         // simplify
     45        void ?{}( Rational(RationalImpl) & r ) {
     46                r{ (RationalImpl){0}, (RationalImpl){1} };
     47        } // rational
     48
     49        void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
     50                r{ n, (RationalImpl){1} };
     51        } // rational
     52
     53        void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
     54                RationalImpl t = simplify( n, d );                              // simplify
    6355                r.[numerator, denominator] = [n / t, d / t];
    6456        } // rational
    6557
     58        void ?{}( Rational(RationalImpl) & r, zero_t ) {
     59                r{ (RationalImpl){0}, (RationalImpl){1} };
     60        } // rational
     61
     62        void ?{}( Rational(RationalImpl) & r, one_t ) {
     63                r{ (RationalImpl){1}, (RationalImpl){1} };
     64        } // rational
     65
    6666        // getter for numerator/denominator
    6767
    68         T numerator( Rational(T) r ) {
     68        RationalImpl numerator( Rational(RationalImpl) r ) {
    6969                return r.numerator;
    7070        } // numerator
    7171
    72         T denominator( Rational(T) r ) {
     72        RationalImpl denominator( Rational(RationalImpl) r ) {
    7373                return r.denominator;
    7474        } // denominator
    7575
    76         [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) {
     76        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
    7777                return dest = src.[ numerator, denominator ];
    7878        } // ?=?
     
    8080        // setter for numerator/denominator
    8181
    82         T numerator( Rational(T) r, T n ) {
    83                 T prev = r.numerator;
    84                 T t = gcd( abs( n ), r.denominator ); // simplify
     82        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
     83                RationalImpl prev = r.numerator;
     84                RationalImpl t = gcd( abs( n ), r.denominator ); // simplify
    8585                r.[numerator, denominator] = [n / t, r.denominator / t];
    8686                return prev;
    8787        } // numerator
    8888
    89         T denominator( Rational(T) r, T d ) {
    90                 T prev = r.denominator;
    91                 T t = simplify( r.numerator, d );       // simplify
     89        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
     90                RationalImpl prev = r.denominator;
     91                RationalImpl t = simplify( r.numerator, d );    // simplify
    9292                r.[numerator, denominator] = [r.numerator / t, d / t];
    9393                return prev;
     
    9696        // comparison
    9797
    98         int ?==?( Rational(T) l, Rational(T) r ) {
     98        int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    9999                return l.numerator * r.denominator == l.denominator * r.numerator;
    100100        } // ?==?
    101101
    102         int ?!=?( Rational(T) l, Rational(T) r ) {
     102        int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    103103                return ! ( l == r );
    104104        } // ?!=?
    105105
    106         int ?!=?( Rational(T) l, zero_t ) {
    107                 return ! ( l == (Rational(T)){ 0 } );
    108         } // ?!=?
    109 
    110         int ?<?( Rational(T) l, Rational(T) r ) {
     106        int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    111107                return l.numerator * r.denominator < l.denominator * r.numerator;
    112108        } // ?<?
    113109
    114         int ?<=?( Rational(T) l, Rational(T) r ) {
     110        int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    115111                return l.numerator * r.denominator <= l.denominator * r.numerator;
    116112        } // ?<=?
    117113
    118         int ?>?( Rational(T) l, Rational(T) r ) {
     114        int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    119115                return ! ( l <= r );
    120116        } // ?>?
    121117
    122         int ?>=?( Rational(T) l, Rational(T) r ) {
     118        int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    123119                return ! ( l < r );
    124120        } // ?>=?
     
    126122        // arithmetic
    127123
    128         Rational(T) +?( Rational(T) r ) {
    129                 return (Rational(T)){ r.numerator, r.denominator };
     124        Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
     125                return (Rational(RationalImpl)){ r.numerator, r.denominator };
    130126        } // +?
    131127
    132         Rational(T) -?( Rational(T) r ) {
    133                 return (Rational(T)){ -r.numerator, r.denominator };
     128        Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
     129                return (Rational(RationalImpl)){ -r.numerator, r.denominator };
    134130        } // -?
    135131
    136         Rational(T) ?+?( Rational(T) l, Rational(T) r ) {
     132        Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    137133                if ( l.denominator == r.denominator ) {                 // special case
    138                         return (Rational(T)){ l.numerator + r.numerator, l.denominator };
     134                        return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator };
    139135                } else {
    140                         return (Rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
     136                        return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
    141137                } // if
    142138        } // ?+?
    143139
    144         Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) {
    145                 l = l + r;
    146                 return l;
    147         } // ?+?
    148 
    149         Rational(T) ?+=?( Rational(T) & l, one_t ) {
    150                 l = l + (Rational(T)){ 1 };
    151                 return l;
    152         } // ?+?
    153 
    154         Rational(T) ?-?( Rational(T) l, Rational(T) r ) {
     140        Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    155141                if ( l.denominator == r.denominator ) {                 // special case
    156                         return (Rational(T)){ l.numerator - r.numerator, l.denominator };
     142                        return (Rational(RationalImpl)){ l.numerator - r.numerator, l.denominator };
    157143                } else {
    158                         return (Rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
     144                        return (Rational(RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
    159145                } // if
    160146        } // ?-?
    161147
    162         Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) {
    163                 l = l - r;
    164                 return l;
    165         } // ?-?
    166 
    167         Rational(T) ?-=?( Rational(T) & l, one_t ) {
    168                 l = l - (Rational(T)){ 1 };
    169                 return l;
    170         } // ?-?
    171 
    172         Rational(T) ?*?( Rational(T) l, Rational(T) r ) {
    173                 return (Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator };
     148        Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     149                return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator };
    174150        } // ?*?
    175151
    176         Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) {
    177                 return l = l * r;
    178         } // ?*?
    179 
    180         Rational(T) ?/?( Rational(T) l, Rational(T) r ) {
    181                 if ( r.numerator < (T){0} ) {
     152        Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     153                if ( r.numerator < (RationalImpl){0} ) {
    182154                        r.[numerator, denominator] = [-r.numerator, -r.denominator];
    183155                } // if
    184                 return (Rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator };
     156                return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator };
    185157        } // ?/?
    186158
    187         Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) {
    188                 return l = l / r;
    189         } // ?/?
    190 
    191159        // I/O
    192160
    193         forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
    194         istype & ?|?( istype & is, Rational(T) & r ) {
     161        forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     162        istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
    195163                is | r.numerator | r.denominator;
    196                 T t = simplify( r.numerator, r.denominator );
     164                RationalImpl t = simplify( r.numerator, r.denominator );
    197165                r.numerator /= t;
    198166                r.denominator /= t;
     
    200168        } // ?|?
    201169
    202         forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
    203                 ostype & ?|?( ostype & os, Rational(T) r ) {
     170        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
     171                ostype & ?|?( ostype & os, Rational(RationalImpl) r ) {
    204172                        return os | r.numerator | '/' | r.denominator;
    205173                } // ?|?
    206174
    207                 void ?|?( ostype & os, Rational(T) r ) {
     175                void ?|?( ostype & os, Rational(RationalImpl) r ) {
    208176                        (ostype &)(os | r); ends( os );
    209177                } // ?|?
     
    211179} // distribution
    212180
    213 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {
    214         Rational(T) ?\?( Rational(T) x, long int y ) {
    215                 if ( y < 0 ) {
    216                         return (Rational(T)){ x.denominator \ -y, x.numerator \ -y };
    217                 } else {
    218                         return (Rational(T)){ x.numerator \ y, x.denominator \ y };
    219                 } // if
    220         } // ?\?
    221 
    222         Rational(T) ?\=?( Rational(T) & x, long int y ) {
    223                 return x = x \ y;
    224         } // ?\?
    225 } // distribution
     181forall( RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
     182Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) {
     183        if ( y < 0 ) {
     184                return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y };
     185        } else {
     186                return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y };
     187        } // if
     188}
    226189
    227190// conversion
    228191
    229 forall( T | Arithmetic( T ) | { double convert( T ); } )
    230 double widen( Rational(T) r ) {
     192forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
     193double widen( Rational(RationalImpl) r ) {
    231194        return convert( r.numerator ) / convert( r.denominator );
    232195} // widen
    233196
    234 forall( T | Arithmetic( T ) | { double convert( T ); T convert( double ); } )
    235 Rational(T) narrow( double f, T md ) {
     197forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
     198Rational(RationalImpl) narrow( double f, RationalImpl md ) {
    236199        // http://www.ics.uci.edu/~eppstein/numth/frap.c
    237         if ( md <= (T){1} ) {                                   // maximum fractional digits too small?
    238                 return (Rational(T)){ convert( f ), (T){1}}; // truncate fraction
     200        if ( md <= (RationalImpl){1} ) {                                        // maximum fractional digits too small?
     201                return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction
    239202        } // if
    240203
    241204        // continued fraction coefficients
    242         T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };
    243         T ai, t;
     205        RationalImpl m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };
     206        RationalImpl ai, t;
    244207
    245208        // find terms until denom gets too big
     
    258221          if ( f > (double)0x7FFFFFFF ) break;                          // representation failure
    259222        } // for
    260         return (Rational(T)){ m00, m10 };
     223        return (Rational(RationalImpl)){ m00, m10 };
    261224} // narrow
    262225
  • libcfa/src/rational.hfa

    r6acd020 r3d7d407  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Tue Jul 20 17:45:29 2021
    15 // Update Count     : 118
     14// Last Modified On : Tue Mar 26 23:16:10 2019
     15// Update Count     : 109
    1616//
    1717
     
    1919
    2020#include "iostream.hfa"
    21 #include "math.trait.hfa"                                                               // Arithmetic
     21
     22trait scalar( T ) {
     23};
     24
     25trait 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};
    2245
    2346// implementation
    2447
    25 forall( T | Arithmetic( T ) ) {
     48forall( RationalImpl | arithmetic( RationalImpl ) ) {
    2649        struct Rational {
    27                 T numerator, denominator;                                               // invariant: denominator > 0
     50                RationalImpl numerator, denominator;                    // invariant: denominator > 0
    2851        }; // Rational
    2952
    3053        // constructors
    3154
    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 );
     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 );
    3760
    3861        // numerator/denominator getter
    3962
    40         T numerator( Rational(T) r );
    41         T denominator( Rational(T) r );
    42         [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src );
     63        RationalImpl numerator( Rational(RationalImpl) r );
     64        RationalImpl denominator( Rational(RationalImpl) r );
     65        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
    4366
    4467        // numerator/denominator setter
    4568
    46         T numerator( Rational(T) r, T n );
    47         T denominator( Rational(T) r, T d );
     69        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
     70        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
    4871
    4972        // comparison
    5073
    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 );
     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 );
    5880
    5981        // arithmetic
    6082
    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 );
     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 );
    7389
    7490        // I/O
    75         forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } )
    76         istype & ?|?( istype &, Rational(T) & );
     91        forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     92        istype & ?|?( istype &, Rational(RationalImpl) & );
    7793
    78         forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) {
    79                 ostype & ?|?( ostype &, Rational(T) );
    80                 void ?|?( ostype &, Rational(T) );
     94        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
     95                ostype & ?|?( ostype &, Rational(RationalImpl) );
     96                void ?|?( ostype &, Rational(RationalImpl) );
    8197        } // distribution
    8298} // distribution
    8399
    84 forall( 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
     100forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} )
     101Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y );
    88102
    89103// conversion
    90 forall( T | Arithmetic( T ) | { double convert( T ); } )
    91 double widen( Rational(T) r );
    92 forall( T | Arithmetic( T ) | { double convert( T );  T convert( double );} )
    93 Rational(T) narrow( double f, T md );
     104forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
     105double widen( Rational(RationalImpl) r );
     106forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} )
     107Rational(RationalImpl) narrow( double f, RationalImpl md );
    94108
    95109// Local Variables: //
  • tests/.expect/rational.txt

    r6acd020 r3d7d407  
    11constructor
    2 a : 3/1 b : 4/1 c : 0/1 d : 0/1 e : 1/1
    3 a : 1/2 b : 5/7
    4 a : 2/3 b : -3/2
    5 a : -2/3 b : 3/2
    6 
    7 comparison
    8 a : -2/1 b : -3/2
    9 a == 0 : 0
    10 a == 1 : 0
    11 a != 0 : 1
    12 ! a : 0
    13 a != b : 1
    14 a <  b : 1
    15 a <=  b : 1
    16 a >  b : 0
    17 a >=  b : 0
    18 
     23/1 4/1 0/1 0/1 1/1
     31/2 5/7
     42/3 -3/2
     5-2/3 3/2
     6logical
     7-2/1 -3/2
     81
     91
     101
     110
     120
    1913arithmetic
    20 a : -2/1 b : -3/2
    21 a + b : -7/2
    22 a += b : -7/2
    23 ++a : -5/2
    24 a++ : -5/2
    25 a : -3/2
    26 a - b : 0/1
    27 a -= b : 0/1
    28 --a : -1/1
    29 a-- : -1/1
    30 a : -2/1
    31 a * b : 3/1
    32 a / b : 4/3
    33 a \ 2 : 4/1 b \ 2 : 9/4
    34 a \ -2 : 1/4 b \ -2 : 4/9
    35 
     14-2/1 -3/2
     15-7/2
     16-1/2
     173/1
     184/3
    3619conversion
    37200.75
     
    41241/7
    4225355/113
    43 
     26decompose
    4427more tests
    4528-3/2
  • tests/rational.cfa

    r6acd020 r3d7d407  
    1010// Created On       : Mon Mar 28 08:43:12 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jul 20 18:13:40 2021
    13 // Update Count     : 107
     12// Last Modified On : Sat Feb  8 18:46:23 2020
     13// Update Count     : 86
    1414//
    1515
     
    2626        sout | "constructor";
    2727        RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1;
    28         sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : " | e;
     28        sout | a | b | c | d | e;
    2929
    3030        a = (RatInt){ 4, 8 };
    3131        b = (RatInt){ 5, 7 };
    32         sout | "a : " | a | "b : " | b;
     32        sout | a | b;
    3333        a = (RatInt){ -2, -3 };
    3434        b = (RatInt){ 3, -2 };
    35         sout | "a : " | a | "b : " | b;
     35        sout | a | b;
    3636        a = (RatInt){ -2, 3 };
    3737        b = (RatInt){ 3, 2 };
    38         sout | "a : " | a | "b : " | b;
    39         sout | nl;
     38        sout | a | b;
    4039
    41         sout | "comparison";
     40        sout | "logical";
    4241        a = (RatInt){ -2 };
    4342        b = (RatInt){ -3, 2 };
    44         sout | "a : " | a | "b : " | b;
    45         sout | "a == 0 : " | a == (Rational(int)){0}; // FIX ME
    46         sout | "a == 1 : " | a == (Rational(int)){1}; // FIX ME
    47         sout | "a != 0 : " | a != 0;
    48         sout | "! a : " | ! a;
    49         sout | "a != b : " | a != b;
    50         sout | "a <  b : " | a <  b;
    51         sout | "a <=  b : " | a <= b;
    52         sout | "a >  b : " | a >  b;
    53         sout | "a >=  b : " | a >= b;
    54         sout | nl;
     43        sout | a | b;
     44//      sout | a == 1; // FIX ME
     45        sout | a != b;
     46        sout | a <  b;
     47        sout | a <= b;
     48        sout | a >  b;
     49        sout | a >= b;
    5550
    5651        sout | "arithmetic";
    57         sout | "a : " | a | "b : " | b;
    58         sout | "a + b : " | a + b;
    59         sout | "a += b : " | (a += b);
    60         sout | "++a : " | ++a;
    61         sout | "a++ : " | a++;
    62         sout | "a : " | a;
    63         sout | "a - b : " | a - b;
    64         sout | "a -= b : " | (a -= b);
    65         sout | "--a : " | --a;
    66         sout | "a-- : " | a--;
    67         sout | "a : " | a;
    68         sout | "a * b : " | a * b;
    69         sout | "a / b : " | a / b;
    70         sout | "a \\ 2 : " | a \ 2u | "b \\ 2 : " | b \ 2u;
    71         sout | "a \\ -2 : " | a \ -2 | "b \\ -2 : " | b \ -2;
    72         sout | nl;
     52        sout | a | b;
     53        sout | a + b;
     54        sout | a - b;
     55        sout | a * b;
     56        sout | a / b;
     57//      sout | a \ 2 | b \ 2; // FIX ME
     58//      sout | a \ -2 | b \ -2;
    7359
    7460        sout | "conversion";
     
    8268        sout | narrow( 0.14285714285714, 16 );
    8369        sout | narrow( 3.14159265358979, 256 );
    84         sout | nl;
    8570
    86         // sout | "decompose";
    87         // int n, d;
    88         // [n, d] = a;
    89         // sout | a | n | d;
     71        sout | "decompose";
     72        int n, d;
     73//      [n, d] = a;
     74//      sout | a | n | d;
    9075
    9176        sout | "more tests";
Note: See TracChangeset for help on using the changeset viewer.