Skip to content

Commit

Permalink
Add sqrt (#19)
Browse files Browse the repository at this point in the history
* Mention sqrt in index.md

* Add sqrt

* test sqrt
  • Loading branch information
mschauer authored Feb 4, 2024
1 parent a0c5e3a commit e6fdd23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Provides the [`ULogarithmic`](@ref) and [`Logarithmic`](@ref) subtypes of `Real`

* `ULogarithmic(x)` and `Logarithmic(x)` represent the number `x`.
* `exp(ULogarithmic, x)` and `exp(Logarithmic, x)` represent `exp(x)` (and `x` can be huge).
* Arithmetic: plus, minus, times, divide, power, `inv`, `log`, `prod`, `sum`.
* Arithmetic: plus, minus, times, divide, power, `sqrt`, `inv`, `log`, `prod`, `sum`.
* Comparisons: equality and ordering.
* Random: `rand(ULogarithmic)` and `rand(Logarithmic)` produces a random number in the unit interval.
* Other functions: `float`, `big`, `unsigned` (converts `ULogarithmic` to `Logarithmic`), `signed` (vice versa), `widen`, `typemin`, `typemax`, `zero`, `one`, `iszero`, `isone`, `isinf`, `isfinite`, `isnan`, `sign`, `signbit`, `abs`, `nextfloat`, `prevfloat`, `write`, `read`.
Expand Down
4 changes: 4 additions & 0 deletions src/LogarithmicNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ function Base.exp(x::Logarithmic)
x.signbit ? inv(exp(x.abs)) : exp(x.abs)
end

function Base.sqrt(x::AnyLogarithmic)
uexp(x.log/2)
end

### Hash

const _HASH = hash(ULogarithmic)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _sub(args...) = @inferred -(args...)
_mul(args...) = @inferred *(args...)
_div(args...) = @inferred /(args...)
_pow(args...) = @inferred ^(args...)
_sqrt(args...) = @inferred sqrt(args...)
_float(args...) = @inferred float(args...)
_inv(args...) = @inferred inv(args...)
_prod(args...) = @inferred prod(args...)
Expand Down Expand Up @@ -457,6 +458,13 @@ atypes2 = (ULogarithmic, ULogFloat32, Logarithmic, LogFloat32)
@test _approx(_float(_pow(A(x), n)), float(x)^n)
end
end

@testset "sqrt" begin
for A in atypes, x in vals
x < 0 && continue
@test _approx(_float(_sqrt(A(x))), sqrt(float(x)))
end
end

@testset "inv" begin
for A in atypes, x in vals
Expand Down

0 comments on commit e6fdd23

Please sign in to comment.