log2 buggy or is a real thing?

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 4 01:23:59 PDT 2012


This progam:

import std.math;
import std.stdio;
import std.typetuple;

ulong log2(ulong n)
{
    return n == 1 ? 0
                  : 1 + log2(n / 2);
}

void print(ulong value)
{
    writefln("%s: %s %s", value, log2(value), std.math.log2(value));
}

void main()
{
    foreach(T; TypeTuple!(byte, ubyte, short, ushort, int, uint, long, ulong))
        print(T.max);
}


prints out

127: 6 6.98868
255: 7 7.99435
32767: 14 15
65535: 15 16
2147483647: 30 31
4294967295: 31 32
9223372036854775807: 62 63
18446744073709551615: 63 64


So, the question is: Is std.math.log2 buggy, or is it just an issue with the 
fact that std.math.log2 is using reals, or am I completely misunderstanding 
something here?

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list