Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operations: exponentiation #2

Open
tinybike opened this issue Jul 6, 2014 · 3 comments
Open

Operations: exponentiation #2

tinybike opened this issue Jul 6, 2014 · 3 comments
Assignees

Comments

@tinybike
Copy link
Member

tinybike commented Jul 6, 2014

No description provided.

@tinybike tinybike self-assigned this Jul 6, 2014
@longemen3000
Copy link

longemen3000 commented May 6, 2019

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

@StevenSiew
Copy link

Here is source code to show how to perform exponentiation

using Decimals
import Base.sqrt

function SciNota(x::Decimal)
    prec = precision(Decimal)
    str = string(x.c)
    strlen = length(str)
    expo_adj = strlen - 1
    if strlen > prec
        str = str[1:prec]
    else
        while length(str) < prec
            str = str * "0"
        end
    end
    return (Int64(x.s),str,x.q + expo_adj)
end

function SciString(x::Decimal)
    (sign,str,expo) = SciNota(x)
    result = sign == 1 ? "-" : ""
    result = result * str[1] * "." * str[2:end] * "e" * string(expo)
    return result
end

function sqrt(x::Decimal;guard_digits=6)
    x_str = SciString(x)
    local z_str
    setprecision(BigFloat, precision(Decimal) + guard_digits; base=10) do
        z_str      = string(  Base.sqrt(parse(BigFloat,x_str))  )
    end
    z = parse(Decimal,z_str)
    return z
end

function power(x::Decimal,y::Decimal;guard_digits=6)
    x_str = SciString(x)
    y_str = SciString(y)
    local z_str
    setprecision(BigFloat, precision(Decimal) + guard_digits; base=10) do
        z_str      = string(  parse(BigFloat,x_str) ^ parse(BigFloat,y_str)  )
    end
    z = parse(Decimal,z_str)
    return z
end

x = Decimal(0,5,-1)
y = sqrt(dec"3")

println("start of program")
println( SciNota(x) )
println( SciString(x) )
println( string(power(x,y)) )

@barucden
Copy link
Collaborator

Ideally, we'd like to avoid conversions from/to String. @longemen3000 has already proposed a baseline implementation (#33), but it needs more work. You can offer help, if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants