digitalmars.D.learn - Something wrong with std.math.pow?
- Jesse Phillips (14/14) Sep 27 2012 Am I getting something wrong with this code?
- Tim (2/11) Sep 27 2012 From what I understand, pragma(msg, ...) prints at compile time.
- Simen Kjaeraas (4/16) Sep 27 2012 Yes it does.
- Jesse Phillips (5/7) Sep 28 2012 In order to print it must have a value to print, so D makes its
- Simen Kjaeraas (6/19) Sep 27 2012 Quick, how much is 2 << 47 modulo 2^^32?
- Jesse Phillips (15/16) Sep 28 2012 After heading to bed I realized that I could no longer rely on
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
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: 0From what I understand, pragma(msg, ...) prints at compile time. I don't think that it evaluates functions then.
Sep 27 2012
On 2012-47-28 0709, Tim <Lt.Infiltrator gmail.com> wrote:Yes it does. -- Simenimport 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: 0From what I understand, pragma(msg, ...) prints at compile time. I don't think that it evaluates functions then.
Sep 27 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.
Sep 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
Sep 27 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.
Sep 28 2012