typing base ^^ exp
Michel Fortin
michel.fortin at michelf.com
Sun Feb 14 16:30:57 PST 2010
On 2010-02-14 18:38:18 -0500, BCS <none at anon.com> said:
> Hello Andrei,
>
>> The type of the power expression is: @uint@ if both @base@ and @exp@
>> have unsigned types less than @ulong@; @int@ if @base@ is a signed
>> integer less than @long@ and @exp@ is an unsigned type less than
>> @ulong@; @ulong@ if both @base@ and @exp@ have unsigned types and at
>> least one is @ulong@; @long@ if @base@ is @long@ and @exp@ is an
>> unsigned type; and @double@ for all other combinations of integral
>> @base@ and @exp at . If at least one of the operands has a floating point
>> type, the result type is the largest participating floating point
>> type.
>> ====
>> Makes sense?
>>
>
> It might if my brain was willing to parse out the @'s :)
It's much more readable this way:
===
The type of the power expression is:
* 'uint' if both 'base' and 'exp' have unsigned types less than 'ulong';
* 'int' if 'base' is a signed integer less than 'long' and 'exp' is an
unsigned type less than 'ulong';
* 'ulong' if both 'base' and 'exp' have unsigned types and at
least one is 'ulong';
* 'long' if 'base' is 'long' and 'exp' is an unsigned type; and
* 'double' for all other combinations of integral 'base' and 'exp'.
If at least one of the operands has a floating point type, the result
type is the largest participating floating point type.
===
So "2 ^^ 2" returns a double while "2 ^^ 2u" returns an int... sound
strange, but it makes sense in a way, I'm not sure it's a good idea to
have signed and unsigned operate in such different ways given how many
people tell you to not use unsigned integers unless it's really
necessary. You're obviously worried about things like 3 ^^ -2 here, I'd
be more tempted to define 3 ^^ -2 as 0, same as (1/3)^^2u. But I don't
have a strong opinion about this.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list