Integer Square Root

Mike James foo at bar.com
Thu Aug 12 05:35:40 PDT 2010


Would it be more useful to have a true integer square root rather than 
overloading the real square root? Something like:

T intsqrt(T)(T i) {
    T bitMask = 1;
    T result = 0;

    bitMask <<= T.sizeof * 4 - 1;

    while (bitMask) {
        result |= bitMask;
        if ((result * result) > i) {
            result &= ~bitMask;
        }
        bitMask >>= 1;
    }

    return result;
}


-=mike=- 



More information about the Digitalmars-d mailing list