Changeset 630a82a for src/libcfa


Ignore:
Timestamp:
Apr 8, 2016, 5:22:29 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
3da470c
Parents:
3d9b5da
Message:

C99 compound literals now work, comment rational code, clean up hoisting AddVisit?

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational

    r3d9b5da r630a82a  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // rational --
     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.
    810//
    911// Author           : Peter A. Buhr
    1012// Created On       : Wed Apr  6 17:56:25 2016
    1113// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr  7 17:23:36 2016
    13 // Update Count     : 9
     14// Last Modified On : Fri Apr  8 11:38:27 2016
     15// Update Count     : 15
    1416//
    1517
    1618#include "iostream"
    1719
     20// implementation
    1821struct Rational {
    1922        long int numerator, denominator;                                        // invariant: denominator > 0
    2023}; // Rational
    2124
     25// constants
    2226extern struct Rational 0;
    2327extern struct Rational 1;
    2428
    25 long int gcd( long int a, long int b );
    26 long int simplify( long int *n, long int *d );
    27 Rational rational();                                                                    // constructor
    28 Rational rational( long int n );                                                // constructor
    29 Rational rational( long int n, long int d );                    // constructor
     29// constructors
     30Rational rational();
     31Rational rational( long int n );
     32Rational rational( long int n, long int d );
     33
     34// getter/setter for numerator/denominator
    3035long int numerator( Rational r );
    3136long int numerator( Rational r, long int n );
     37long int denominator( Rational r );
    3238long int denominator( Rational r, long int d );
     39
     40// comparison
    3341int ?==?( Rational l, Rational r );
    3442int ?!=?( Rational l, Rational r );
     
    3745int ?>?( Rational l, Rational r );
    3846int ?>=?( Rational l, Rational r );
     47
     48// arithmetic
    3949Rational -?( Rational r );
    4050Rational ?+?( Rational l, Rational r );
     
    4252Rational ?*?( Rational l, Rational r );
    4353Rational ?/?( Rational l, Rational r );
     54
     55// conversion
    4456double widen( Rational r );
    4557Rational narrow( double f, long int md );
     58
     59// I/O
    4660forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
    4761forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
  • src/libcfa/rational.c

    r3d9b5da r630a82a  
    1111// Created On       : Wed Apr  6 17:54:28 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Thu Apr  7 17:28:03 2016
    14 // Update Count     : 12
     13// Last Modified On : Fri Apr  8 15:39:17 2016
     14// Update Count     : 17
    1515//
    1616
     
    2323} // extern
    2424
     25
     26// constants
     27
    2528struct Rational 0 = {0, 1};
    2629struct Rational 1 = {1, 1};
    2730
    28 // Calculate the greatest common denominator of two numbers, the first of which may be negative.  It is used to reduce
    29 // rationals.
    30 
    31 long int gcd( long int a, long int b ) {
     31
     32// helper
     33
     34// Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
     35static long int gcd( long int a, long int b ) {
    3236    for ( ;; ) {                                                                                // Euclid's algorithm
    3337                long int r = a % b;
     
    3943} // gcd
    4044
    41 long int simplify( long int *n, long int *d ) {
     45static long int simplify( long int *n, long int *d ) {
    4246    if ( *d == 0 ) {
    4347                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     
    4852} // Rationalnumber::simplify
    4953
    50 Rational rational() {                                                                   // constructor
    51 //    r = (Rational){ 0, 1 };
    52         Rational t = { 0, 1 };
    53         return t;
     54
     55// constructors
     56
     57Rational rational() {
     58    return (Rational){ 0, 1 };
     59//      Rational t = { 0, 1 };
     60//      return t;
    5461} // rational
    5562
    56 Rational rational( long int n ) {                                               // constructor
    57 //    r = (Rational){ n, 1 };
    58         Rational t = { n, 1 };
    59         return t;
     63Rational rational( long int n ) {
     64    return (Rational){ n, 1 };
     65//      Rational t = { n, 1 };
     66//      return t;
    6067} // rational
    6168
    62 Rational rational( long int n, long int d ) {                   // constructor
     69Rational rational( long int n, long int d ) {
    6370    long int t = simplify( &n, &d );                                    // simplify
    6471//    r = (Rational){ n / t, d / t };
     
    6673        return t;
    6774} // rational
     75
     76
     77// getter/setter for numerator/denominator
    6878
    6979long int numerator( Rational r ) {
     
    7989} // numerator
    8090
     91long int denominator( Rational r ) {
     92    return r.denominator;
     93} // denominator
     94
    8195long int denominator( Rational r, long int d ) {
    8296    long int prev = r.denominator;
     
    87101} // denominator
    88102
     103
     104// comparison
     105
    89106int ?==?( Rational l, Rational r ) {
    90107    return l.numerator * r.denominator == l.denominator * r.numerator;
     
    110127    return ! ( l < r );
    111128} // ?>=?
     129
     130
     131// arithmetic
    112132
    113133Rational -?( Rational r ) {
     
    149169    return t;
    150170} // ?/?
     171
     172
     173// conversion
    151174
    152175double widen( Rational r ) {
     
    188211} // narrow
    189212
     213
     214// I/O
     215
    190216forall( dtype istype | istream( istype ) )
    191217istype * ?|?( istype *is, Rational *r ) {
Note: See TracChangeset for help on using the changeset viewer.