Skip to content

Commit

Permalink
Add a fourthroot function with unicode symbol ∜ (JuliaLang#48899)
Browse files Browse the repository at this point in the history
* adds fourthroot function
  • Loading branch information
tomchor authored and Xnartharax committed Apr 13, 2023
1 parent 1530f02 commit e8fade7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Build system changes
New library functions
---------------------
* `tanpi` is now defined. It computes tan(πx) more accurately than `tan(pi*x)` ([#48575]).
* `fourthroot(x)` is now defined in `Base.Math` and can be used to compute the fourth root of `x`.
It can also be accessed using the unicode character ``, which can be typed by `\fourthroot<tab>` ([#48899]).

New library features
--------------------
Expand Down
1 change: 1 addition & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ include("math.jl")
using .Math
const ()=sqrt
const ()=cbrt
const ()=fourthroot

# now switch to a simple, race-y TLS, relative include for the rest of Base
delete_method(which(include, (Module, String)))
Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export
bitrotate,
bswap,
cbrt,
fourthroot,
ceil,
cis,
cispi,
Expand Down Expand Up @@ -364,6 +365,7 @@ export
zero,
,
,
,
,
,

Expand Down
12 changes: 10 additions & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export sin, cos, sincos, tan, sinh, cosh, tanh, asin, acos, atan,
acosd, acotd, acscd, asecd, asind, atand,
rad2deg, deg2rad,
log, log2, log10, log1p, exponent, exp, exp2, exp10, expm1,
cbrt, sqrt, significand,
cbrt, sqrt, fourthroot, significand,
hypot, max, min, minmax, ldexp, frexp,
clamp, clamp!, modf, ^, mod2pi, rem2pi,
@evalpoly, evalpoly
Expand Down Expand Up @@ -715,6 +715,13 @@ julia> .√(1:4)
"""
sqrt(x)

"""
fourthroot(x)
Return the fourth root of `x` by applying `sqrt` twice successively.
"""
fourthroot(x::Number) = sqrt(sqrt(x))

"""
hypot(x, y)
Expand Down Expand Up @@ -1537,7 +1544,7 @@ include("special/log.jl")
# Float16 definitions

for func in (:sin,:cos,:tan,:asin,:acos,:atan,:cosh,:tanh,:asinh,:acosh,
:atanh,:log,:log2,:log10,:sqrt,:log1p)
:atanh,:log,:log2,:log10,:sqrt,:fourthroot,:log1p)
@eval begin
$func(a::Float16) = Float16($func(Float32(a)))
$func(a::ComplexF16) = ComplexF16($func(ComplexF32(a)))
Expand Down Expand Up @@ -1573,5 +1580,6 @@ end
exp2(x::AbstractFloat) = 2^x
exp10(x::AbstractFloat) = 10^x
clamp(::Missing, lo, hi) = missing
fourthroot(::Missing) = missing

end # module
24 changes: 23 additions & 1 deletion test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ end
@test atan(x,y) atan(big(x),big(y))
@test atanh(x) atanh(big(x))
@test cbrt(x) cbrt(big(x))
@test fourthroot(x) fourthroot(big(x))
@test cos(x) cos(big(x))
@test cosh(x) cosh(big(x))
@test cospi(x) cospi(big(x))
Expand Down Expand Up @@ -219,6 +220,9 @@ end
@test isequal(cbrt(T(0)), T(0))
@test isequal(cbrt(T(1)), T(1))
@test isequal(cbrt(T(1000000000))^3, T(1000)^3)
@test isequal(fourthroot(T(0)), T(0))
@test isequal(fourthroot(T(1)), T(1))
@test isequal(fourthroot(T(100000000))^4, T(100)^4)
@test isequal(cos(T(0)), T(1))
@test cos(T(pi)/2) T(0) atol=eps(T)
@test isequal(cos(T(pi)), T(-1))
Expand Down Expand Up @@ -271,6 +275,8 @@ end
@test asin(sin(x)) x
@test cbrt(x)^3 x
@test cbrt(x^3) x
@test fourthroot(x)^4 x
@test fourthroot(x^4) x
@test asinh(sinh(x)) x
@test atan(tan(x)) x
@test atan(x,y) atan(x/y)
Expand Down Expand Up @@ -1255,6 +1261,22 @@ end
end
end

@testset "fourthroot" begin
for T in (Float32, Float64)
@test fourthroot(zero(T)) === zero(T)
@test fourthroot(one(T)) === one(T)
@test fourthroot(T(Inf)) === T(Inf)
@test isnan_type(T, fourthroot(T(NaN)))
for x in (pcnfloat(nextfloat(nextfloat(zero(T))))...,
0.45, 0.6, 0.98,
map(x->x^3, 1.0:1.0:1024.0)...,
prevfloat(T(Inf)))
by = fourthroot(big(T(x)))
@test fourthroot(T(x)) by rtol=eps(T)
end
end
end

@testset "hypot" begin
@test hypot(0, 0) == 0.0
@test hypot(3, 4) == 5.0
Expand Down Expand Up @@ -1519,7 +1541,7 @@ end
end

# test constant-foldability
for fn in (:sin, :cos, :tan, :log, :log2, :log10, :log1p, :exponent, :sqrt, :cbrt,
for fn in (:sin, :cos, :tan, :log, :log2, :log10, :log1p, :exponent, :sqrt, :cbrt, :fourthroot,
:asin, :atan, :acos, :sinh, :cosh, :tanh, :asinh, :acosh, :atanh,
:exp, :exp2, :exp10, :expm1
)
Expand Down

0 comments on commit e8fade7

Please sign in to comment.