| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
June 13, 2013 So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
I have this code :
import std.stdio;
import std.c.stdlib;
void main()
{
foreach (count; 1 .. 33){
write((2)^^(count), " : ", count, "\n");
}
exit (0);
}
And here is the output :
2 : 1
4 : 2
8 : 3
16 : 4
32 : 5
64 : 6
128 : 7
256 : 8
512 : 9
1024 : 10
2048 : 11
4096 : 12
8192 : 13
16384 : 14
32768 : 15
65536 : 16
131072 : 17
262144 : 18
524288 : 19
1048576 : 20
2097152 : 21
4194304 : 22
8388608 : 23
16777216 : 24
33554432 : 25
67108864 : 26
134217728 : 27
268435456 : 28
536870912 : 29
1073741824 : 30
-2147483648 : 31
0 : 32
-----------------------------------------------------------------------
Everything goes well until the power of 31 and then above that it will be cero.
What do I have to know about how D works with data ?
| ||||
June 13, 2013 Re: So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos | import std.stdio;
import std.math : pow;
void main()
{
cast(ulong)count;
foreach (count; 1 .. 33){
write((2)^^(count), " : ", count, "\n");
}
}
same output.
| |||
June 13, 2013 Re: So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos | On Thursday, June 13, 2013 03:46:59 Carlos wrote: > import std.stdio; > import std.math : pow; > > void main() > { > cast(ulong)count; That line won't compile. > foreach (count; 1 .. 33){ > write((2)^^(count), " : ", count, "\n"); > } > } > > same output. If you want to set the type of count, then give it a type instead of letting foreach infer it. Integeral literals are inferred to be int, so if you don't give count a type, it'll be int. import std.stdio; import std.math : pow; void main() { foreach(ulong count; 1 .. 33) writefln("%s: %s", 2^^count, count); } - Jonathan M Davis | |||
June 13, 2013 Re: So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Thursday, 13 June 2013 at 02:03:35 UTC, Jonathan M Davis wrote:
> On Thursday, June 13, 2013 03:46:59 Carlos wrote:
>> import std.stdio;
>> import std.math : pow;
>>
>> void main()
>> {
>> cast(ulong)count;
>
> That line won't compile.
>
>> foreach (count; 1 .. 33){
>> write((2)^^(count), " : ", count, "\n");
>> }
>> }
>>
>> same output.
>
> If you want to set the type of count, then give it a type instead of letting
> foreach infer it. Integeral literals are inferred to be int, so if you don't
> give count a type, it'll be int.
>
> import std.stdio;
> import std.math : pow;
>
> void main()
> {
> foreach(ulong count; 1 .. 33)
> writefln("%s: %s", 2^^count, count);
> }
>
> - Jonathan M Davis
Great! Thanks!
I would use another space before the ":" .
writefln("%s : %s", 2^^count, count);
| |||
June 13, 2013 Re: So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos | Carlos:
> What do I have to know about how D works with data ?
If you want to avoid the overflow, then use a BigInt from std.bigint:
import std.stdio, std.bigint;
void main() {
foreach (immutable i; 0 .. 100)
writeln(i, " ", 2.BigInt ^^ i);
}
Bye,
bearophile
| |||
June 13, 2013 Re: So I found this using 2 to the power of >= 31 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 13 June 2013 at 02:41:46 UTC, bearophile wrote:
> Carlos:
>
>> What do I have to know about how D works with data ?
>
> If you want to avoid the overflow, then use a BigInt from std.bigint:
>
> import std.stdio, std.bigint;
>
> void main() {
> foreach (immutable i; 0 .. 100)
> writeln(i, " ", 2.BigInt ^^ i);
> }
>
> Bye,
> bearophile
:D Great!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply