www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Something wrong with std.math.pow?

reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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.
Sep 27 2012
next sibling parent "Tim" <Lt.Infiltrator gmail.com> writes:
 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.
Sep 27 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Sep 27 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Sep 27 2012
prev sibling next sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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.
Sep 28 2012
prev sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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.
Sep 28 2012