Skip to content

Commit b54498c

Browse files
authored
Specialize sqrt and cbrt (#6)
* Specialize sqrt and cbrt * Add tests * Fix test
1 parent b0e1bbe commit b54498c

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name = "MultiplesOfPi"
22
uuid = "b749d01f-fee9-4313-9f11-89ddf7ea9d58"
33
authors = ["Jishnu Bhattacharya", "Center for Space Science", "New York University Abu Dhabi"]
4-
version = "0.5.1"
4+
version = "0.5.2"
5+
6+
[deps]
7+
FastRationals = "275e499e-7a09-5551-a1d1-9fe535a2b717"
58

69
[compat]
710
julia = "1"

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ false
2626

2727
julia> (1//3)Pi + (4//3)Pi == (5//3)Pi
2828
true
29+
30+
julia> float(sqrt(pi)^2) == float(pi)
31+
false
32+
33+
julia> float(sqrt(Pi)^2) == float(Pi)
34+
true
2935
```
3036

3137
We may also simplify algebraic expressions involving powers of `Pi` as
@@ -138,6 +144,12 @@ julia> Pi + π
138144
2Pi
139145
```
140146

147+
Note that addition and subtraction are not type-stable at present by design.
148+
149+
# Performance
150+
151+
In general using `Pi` instead of `pi` will be less performant, as `pi` is aggressively promoted to a floating-point value in most calculations. The use of `Pi` is mainly intended for tests where exact fractions of `Pi` are desirable.
152+
141153
# Installation
142154

143155
Install the package using

src/MultiplesOfPi.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module MultiplesOfPi
22

3+
using FastRationals: FastRational
4+
35
export PiExpTimes
46
export Pi
57

@@ -246,6 +248,16 @@ Base.:(//)(p::PiExpTimes, ::Irrational{:π}) = PiExpTimes(p.x//1, p.n-1)
246248
Base.:(//)(y::Real, p::PiExpTimes) = PiExpTimes(y//p.x, -p.n)
247249
Base.:(//)(::Irrational{:π}, p::PiExpTimes) = PiExpTimes(1//p.x, -p.n+1)
248250

251+
_half(n::Integer) = FastRational(n, 2)
252+
_half(n::Rational) = n//2
253+
_half(n) = n/2
254+
_third(n::Integer) = FastRational(n, 3)
255+
_third(n::Rational) = n//3
256+
_third(n) = n/3
257+
258+
Base.sqrt(p::PiExpTimes) = PiExpTimes(sqrt(p.x), _half(p.n))
259+
Base.cbrt(p::PiExpTimes) = PiExpTimes(cbrt(p.x), _third(p.n))
260+
249261
# Pretty-printing
250262

251263
function Base.show(io::IO, p::PiExpTimes)

test/runtests.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Test
1111
@test π == Pi
1212
@test 2π 2Pi
1313

14-
@test float(Pi^0.5) == sqrt(pi)
14+
@test float(Pi^0.5) == pi^0.5
1515

1616
@testset "nested" begin
1717
@test PiExpTimes{PiExpTimes{Int}}(3, 0) == 3
@@ -395,6 +395,20 @@ end
395395
@test float(@inferred Pi^2.0) === pi^2.0
396396
@test float(@inferred Pi^-2.0) === pi^-2.0
397397
@test (@inferred Pi^0.0) == 1.0
398+
@test float(@inferred Pi^0.5) == float(@inferred sqrt(Pi))
399+
@test BigFloat(@inferred Pi^0.5) == BigFloat(@inferred sqrt(Pi))
400+
@test float(@inferred Pi^(1//3)) == float(@inferred cbrt(Pi))
401+
@test BigFloat(@inferred Pi^(1//3)) == BigFloat(@inferred cbrt(Pi))
402+
for T in [Float64, BigFloat]
403+
@test T(sqrt(Pi)^2) == T(Pi)
404+
@test T(sqrt(Pi^2)) == T(Pi)
405+
@test T(sqrt(Pi^2.0)) == T(Pi)
406+
@test T(sqrt(Pi^(2//1))) == T(Pi)
407+
@test T(cbrt(Pi)^3) == T(Pi)
408+
@test T(cbrt(Pi^3)) == T(Pi)
409+
@test T(cbrt(Pi^3.0)) == T(Pi)
410+
@test T(cbrt(Pi^(3//1))) == T(Pi)
411+
end
398412
end
399413

400414
@testset "Irrational exponent" begin

0 commit comments

Comments
 (0)