Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 29, 2018 Why pow() won't go beyond 2^31? | ||||
---|---|---|---|---|
| ||||
I am using the function pow() from std.math but if I try pow(2, 32) it returns 0, it doesn't compute beyond the maximum value of an int(2^31) and I am working with long. What should I do? |
November 29, 2018 Re: Why pow() won't go beyond 2^31? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | On Thursday, 29 November 2018 at 07:07:06 UTC, Murilo wrote:
> I am using the function pow() from std.math but if I try pow(2, 32) it returns 0, it doesn't compute beyond the maximum value of an int(2^31) and I am working with long. What should I do?
what exactly is your input?
´´´
import std.stdio;
import std.experimental.all;
void main()
{
long u = 2;
assert(pow(u,32) == 4294967296);
assert(pow(2UL,32) == 4294967296);
}
´´´
|
November 29, 2018 Re: Why pow() won't go beyond 2^31? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo Attachments:
| On Thu, Nov 29, 2018 at 8:10 AM Murilo via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > I am using the function pow() from std.math but if I try pow(2, > 32) it returns 0, it doesn't compute beyond the maximum value of > an int(2^31) and I am working with long. What should I do? > if you look at doc: https://dlang.org/phobos/std_math.html#.pow.2 you will see that return type is infered from pow arguments, so if both arguments are int for example the return value would be int too https://run.dlang.io/is/FMVJhY so if you want to have long as output one of your args should be (u)long or you can enforce that by pow!long(2,32); https://run.dlang.io/is/WlDfsE |
December 10, 2018 Re: Why pow() won't go beyond 2^31? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | Hi guys, thank you for helping me out here, there is this facebook group for the D language, here we can help and teach each other. It is called Programming in D. Please join. https://www.facebook.com/groups/662119670846705/?ref=bookmarks |
Copyright © 1999-2021 by the D Language Foundation