Hello,
I am trying to use pow with an integer argument, but I cannot have a bigint result, for example, pow(10,72)
.
Do I have to write my pow function or is there a native solution?
thanks,
Fausto
Thread overview |
---|
October 01, 2022 bigint and pow | ||||
---|---|---|---|---|
| ||||
Hello, I am trying to use pow with an integer argument, but I cannot have a bigint result, for example, thanks, |
October 02, 2022 Re: bigint and pow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fausto | On 10/2/22 00:04, Fausto via Digitalmars-d-learn wrote: > Hello, > > I am trying to use pow with an integer argument, but I cannot have a bigint result, for example, ```pow(10,72)```. > Do I have to write my pow function or is there a native solution? > > thanks, > Fausto > In contrast to certain scripting languages, there's no implicit promotion, you have to opt in for BigInt [1] usage in D: ```d import std; void main() { // all print the same writeln(BigInt(10) ^^ 72); writeln(10.BigInt ^^ 72); writeln("10".BigInt ^^ 72); } ``` [1] https://dlang.org/phobos/std_bigint.html#.BigInt |
October 02, 2022 Re: bigint and pow | ||||
---|---|---|---|---|
| ||||
Posted in reply to rassoc | On Sunday, 2 October 2022 at 02:02:37 UTC, rassoc wrote:
> On 10/2/22 00:04, Fausto via Digitalmars-d-learn wrote:
>> Hello,
>>
>> I am trying to use pow with an integer argument, but I cannot have a bigint result, for example, ```pow(10,72)```.
>> Do I have to write my pow function or is there a native solution?
>>
>> thanks,
>> Fausto
>>
>
> In contrast to certain scripting languages, there's no implicit promotion, you have to opt in for BigInt [1] usage in D:
>
> ```d
> import std;
> void main()
> {
> // all print the same
> writeln(BigInt(10) ^^ 72);
> writeln(10.BigInt ^^ 72);
> writeln("10".BigInt ^^ 72);
> }
> ```
>
> [1] https://dlang.org/phobos/std_bigint.html#.BigInt
Thanks a lot. I am to used to C and, more important, I didn't think to look for also another operator for the power function :)
|
October 02, 2022 Re: bigint and pow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fausto | On 10/2/22 09:24, Fausto via Digitalmars-d-learn wrote: > Thanks a lot. I am to used to C and, more important, I didn't think to look for also another operator for the power function :) > D does have pow and many other useful math functions [1], it's just not defined for BitInts. Oh, and speaking of C, you also have access to all the usual C math [1] functions with just an import: ```d import std.stdio : writeln; void main() { import std.math : pow; writeln(pow(10, 3)); // pow from D import core.stdc.math : pow; writeln(pow(10, 3)); // pow from C // can also make it more explicit to show where it is coming from: import cmath = core.stdc.math; writeln(cmath.pow(10, 3)); } ``` Have fun with D! [1] https://dlang.org/library/std/math.html [2] https://dlang.org/library/core/stdc/math.html |
October 02, 2022 Re: bigint and pow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fausto | On 10/2/22 09:24, Fausto via Digitalmars-d-learn wrote:
> Thanks a lot. I am to used to C and, more important, I didn't think to look for also another operator for the power function :)
>
Oh, and I forgot to mention that this is doing what you probably asked for originally:
```d
import std;
import cmath = core.stdc.math;
void main()
{
// both print 1e+72
writeln(pow(10.0, 72));
writeln(cmath.pow(10, 72));
}
```
But it's just floating-point scientific notation, not true BigInts.
Another math difference from C is that D has well-defined wrapping math for signed ints.
|