Show
Ignore:
Timestamp:
08/28/10 23:24:19 (1 year ago)
Author:
dsimcha
Message:

Add casting to integer.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rational/rational.d

    r794 r795  
    123123 * If you use an arbitrary precision type, it must have the relevant operators 
    124124 * overloaded and have value semantics. 
    125  * 
    126  * BUGS:  Casting a Rational instantiated with a user-defined fixed-width 
    127  *        integer type to a floating point type currently doesn't work and may 
    128  *        hang your program in an infinite loop. 
    129125 * 
    130126 * Examples: 
     
    540536    } 
    541537 
     538    /**Casts this to an integer by truncating the fractional part.  Equivalent 
     539     * to $(D integerPart), and then casting it to type $(D I). 
     540     */ 
     541    I opCast(I)() if(isIntegerLike!I && is(typeof(cast(I) Int.init))) { 
     542        return cast(I) integerPart; 
     543    } 
     544 
    542545    /**Returns the numerator.*/ 
    543546    @property Int num() { 
     
    701704    assert(intFract.integerPart == 1); 
    702705    assert(intFract.fractionPart == rational(1, 4)); 
     706    assert(cast(long) intFract == 1); 
    703707 
    704708    // Test whether CTFE works for primitive types.  Doesn't work yet.