Skip to content

support negative base in pow for integer exponent #88

@thedavidmeister

Description

@thedavidmeister

technically negative bases can be supported for integer exponents

currently negative bases simply error due to the log10 step

for example (-2)^2 = 4 and (-2)^3 = -8

for all even integer exponents (-a)^b = a^b

for all odd integer exponents (-a)^b = -(a^b)

the first issue is that non-integer exponents on a negative base can result in complex numbers, and so is categorically NaN in the float spec, even if there exists some answer

for example cube root of -8 is -2 but in javascript Math.pow(-8, 1/3) is NaN

so what we would want is to first check that the exponent is an integer and then check if it is odd/even to preserve or drop the - respectively

we don't currently have an isInteger but a.frac().isZero() should be a safe and relatively efficient check

determining odd/even is potentially more of an issue, the intuitive thing would be to do a.mod(2).isZero() but float modulo is an operation that has precision issues due to float representation limitations

for example 9.6%3.2 is 3.1999999999999993 rather than 0 in float math

the basic logic of % is x - floor(x / y) * y which produces the above precision issues

the question, that i don't currently know the answer to, is whether precision issues in float modulo ever actually manifest in decimal floats (vs. binary floats) where both x and y are integers (x is known to be an integer because that's the only exponents we support, and y is always 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions