Decimals.jl icon indicating copy to clipboard operation
Decimals.jl copied to clipboard

Operations: exponentiation

Open tinybike opened this issue 11 years ago • 3 comments

tinybike avatar Jul 06 '14 10:07 tinybike

this works in a way, it takes care of all irrational cases by going into bigfloat, if not, then the Decimal fields are calculated manually

function ^(x::Decimal, y::Decimal)
    #(-1.23)^(something) code to catch errors prior to the routine
    x.s == 1 && begin
        (y.q < 0  && throw(DomainError(y),"Exponentiation yielding a complex result requires a complex argument."))
    end
    y.s == 1 && (return ^(inv(x),-y))
    y.q < 0 && (return Decimal(BigFloat(x)^BigFloat(y))) #add correction to precision, its too much
    
    if iseven(y.c) #squared number
        zs = 0
    else
        if x.s == 0 
            zs =0
        else
            zs = 1
        end
    end
    
        zc = x.c^((y.c)*(10^y.q))
        zq = (x.q)*((y.c)*(10^y.q))
        return  Decimal(zs,zc,zq)
 
end

longemen3000 avatar May 06 '19 09:05 longemen3000