Thread overview
Something wrong with std.math.pow?
Sep 28, 2012
Jesse Phillips
Sep 28, 2012
Tim
Sep 28, 2012
Simen Kjaeraas
Sep 28, 2012
Jesse Phillips
Sep 28, 2012
Simen Kjaeraas
Sep 28, 2012
Jesse Phillips
September 28, 2012
Am I getting something wrong with this code?

import std.conv;
import std.math;

void main() {
    pragma(msg, "Number of bits: " ~ to!string(12 * 4));
    pragma(msg, "Addressable Bytes: " ~ to!string(pow(2, 12 * 4)));
}

Number of bits: 48
Addressable Bytes: 0


Linux 64bit dmd 2.060...

Specifically, shouldn't 2^48 be a little bit larger than 0?

And if you have corrections to the math as it applies to the statements I'm open to correction, but I expect it is correct.
September 28, 2012
> import std.conv;
> import std.math;
>
> void main() {
>     pragma(msg, "Number of bits: " ~ to!string(12 * 4));
>     pragma(msg, "Addressable Bytes: " ~ to!string(pow(2, 12 * 4)));
> }
>
> Number of bits: 48
> Addressable Bytes: 0

From what I understand, pragma(msg, ...) prints at compile time.  I don't think that it evaluates functions then.
September 28, 2012
On 2012-47-28 0709, Tim <Lt.Infiltrator@gmail.com> wrote:

>> import std.conv;
>> import std.math;
>>
>> void main() {
>>     pragma(msg, "Number of bits: " ~ to!string(12 * 4));
>>     pragma(msg, "Addressable Bytes: " ~ to!string(pow(2, 12 * 4)));
>> }
>>
>> Number of bits: 48
>> Addressable Bytes: 0
>
>  From what I understand, pragma(msg, ...) prints at compile time.  I don't think that it evaluates functions then.

Yes it does.

-- 
Simen
September 28, 2012
On 2012-35-28 0709, Jesse Phillips <jessekphillips+D@gmail.com> wrote:

> Am I getting something wrong with this code?
>
> import std.conv;
> import std.math;
>
> void main() {
>      pragma(msg, "Number of bits: " ~ to!string(12 * 4));
>      pragma(msg, "Addressable Bytes: " ~ to!string(pow(2, 12 * 4)));
> }
>
> Number of bits: 48
> Addressable Bytes: 0
>
>
> Linux 64bit dmd 2.060...
>
> Specifically, shouldn't 2^48 be a little bit larger than 0?
>
> And if you have corrections to the math as it applies to the statements I'm open to correction, but I expect it is correct.

Quick, how much is 2 << 47 modulo 2^^32?

At least I think that's the problem - all numbers involved are simple ints,
and so you get overflow. Try pow(2L, 12 * 4) instead.

-- 
Simen
September 28, 2012
On Friday, 28 September 2012 at 05:46:49 UTC, Tim wrote:
> From what I understand, pragma(msg, ...) prints at compile time.  I don't think that it evaluates functions then.

In order to print it must have a value to print, so D makes its great attempt to run whatever it can at compile time to get that value.

I forgot to mention, writeln doesn't change the results.
September 28, 2012
On Friday, 28 September 2012 at 05:35:13 UTC, Jesse Phillips wrote:
> Specifically, shouldn't 2^48 be a little bit larger than 0?

After heading to bed I realized that I could no longer rely on compile-time type selection since I was calling a function. So obviously the number I was looking for would not fit into an int.

import std.conv;
import std.math;

void main() {
    pragma(msg, "Number of bits: " ~ to!string(12 * 4));
    pragma(msg, "Addressable Bytes: " ~ to!string(pow(2UL, 12 * 4)));
}

Number of bits: 48
Addressable Bytes: 281474976710656

Much better.