issue 7006 - std.math.pow (integral, integral) crashes on negative exponents

M.M. matus at email.cz
Mon Dec 16 12:39:11 UTC 2019


On Monday, 16 December 2019 at 12:25:11 UTC, Johannes Loher wrote:
> On Monday, 16 December 2019 at 06:44:31 UTC, Dominikus Dittes 
> Scherkl wrote:
>> [...] And if its time critical and you need only integers, 
>> there are much faster solutions than using pow (e.g. 
>> odd(x)?-1:1 )
>
> The thing is: In math, it is extremely common to write this as 
> (-1)^^x. I don‘t understand why we should not allow this 
> notation. As Timon explained, the restriction just makes no 
> sense and was not what he had expected (I am also very 
> surprised by this). I admit that Timon’s explanations sounded a 
> bit harsh, but that does not makes them any less true. This is 
> simply an arbitrary limitation. The definition of pow in math 
> can be perfectly adapted to integers by simply using integer 
> division, i.e. 2^^(-1) == 0, so this is how it should work.

As mentioned by both Timon and Johannes, in mathematics, and 
especially in combinatorics, the usage of (-1)^^i for natural 
number i is extremely common. For example, the definition of a 
determinant of a matrix uses this concept. Also, the binomial 
expansion of (a-b)^n can be elegantly expressed using (-1)^i. In 
general, any computation where the sign depends on the parity of 
a number i can be expressed using (-1)^i. (Another example would 
be the inclusion-exclusion principle for expressing the size of 
the union of k sets).

As such, (-1)^^i is _extremely_ useful and common, and changing 
how (common) math works in a programming language is asking for 
troubles.


More information about the Digitalmars-d mailing list