| Thread overview |
|---|
August 24, 2016 pow exponent type issue | ||||
|---|---|---|---|---|
| ||||
I'm a little confused on why pow behaves so differently when switching from an int to a uint for the exponent.
import std.math : pow;
import std.stdio : writeln;
void main()
{
float x = 2;
int y1 = 1;
uint y2 = 1;
writeln(pow(x, -y1)); //prints 0.5
writeln(pow(x, -y2)); //prints inf
}
| ||||
August 24, 2016 Re: pow exponent type issue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Wednesday, 24 August 2016 at 19:16:56 UTC, jmh530 wrote:
> I'm a little confused on why pow behaves so differently when switching from an int to a uint for the exponent.
>
> import std.math : pow;
> import std.stdio : writeln;
>
> void main()
> {
>
> float x = 2;
> int y1 = 1;
> uint y2 = 1;
>
> writeln(pow(x, -y1)); //prints 0.5
> writeln(pow(x, -y2)); //prints inf
>
> }
-y1 is -1. But -y2 is uint.max, i.e. a pretty large positive number.
The 'u' in "uint" stands for "unsigned". That is, it doesn't know negative numbers. Dont' use uint when you need negative numbers.
| |||
August 24, 2016 Re: pow exponent type issue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Wednesday, 24 August 2016 at 19:41:35 UTC, ag0aep6g wrote:
>
> -y1 is -1. But -y2 is uint.max, i.e. a pretty large positive number.
>
> The 'u' in "uint" stands for "unsigned". That is, it doesn't know negative numbers. Dont' use uint when you need negative numbers.
Ahh, doh.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply