Skip to content

Commit cd364b1

Browse files
author
Michael Abbott
committed
003
1 parent c439572 commit cd364b1

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
Provides Bhaskara I's sine approximation formula
44

55
```julia
6-
sinπ(θ) = 16-θ)θ / (5π^2 - 4-θ)θ) # < 0.1% error, θ ∈ [0,π]
6+
bsin(θ) = 16-θ)θ / (5π^2 - 4-θ)θ) # error < 0.002, θ ∈ [0,π]
77
```
88

9-
There is also `sin2π(θ)` for `θ ∈ [-π,π]`, and `Bhaskara.sin(θ)` for `θ ∈ 𝐑`,
9+
There is also `b2sin(θ)` for `θ ∈ [-π,π]`, and `Bhaskara.sin(θ)` for `θ ∈ 𝐑`,
1010
by inserting appropriate `mod(,2π)` and `sign()*abs()` bits.
11-
Similarly `cosπ(θ)` is for `θ ∈ [-π/2,π/2]`, and `Bhaskara.cos(θ)` for all `θ ∈ 𝐑`.
11+
Similarly `bcos(θ)` is for `θ ∈ [-π/2,π/2]`, and `Bhaskara.cos(θ)` for all `θ ∈ 𝐑`.
1212

1313
<img src="sin.png?raw=true" width="600" height="400" alt="versions of sin" align="center" padding="5">
1414

1515
<!--
1616
using Bhaskara, Plots
17-
plot(-2π:0.01:2π, sinπ.(-2π:0.01:2π), lab="sin\\pi")
18-
plot!(-2π:0.01:2π, sin2π.(-2π:0.01:2π), lab="sin2\\pi")
17+
plot(-2π:0.01:2π, bsin.(-2π:0.01:2π), lab="bsin")
18+
plot!(-2π:0.01:2π, b2sin.(-2π:0.01:2π), lab="b2sin")
1919
plot!(-2π:0.01:2π, Bhaskara.sin.(-2π:0.01:2π), lab="sin")
2020
plot!(xticks=([-2π,-π,0,π,2π],["-2\\pi","-\\pi","0","\\pi","2\\pi"]), ylim=[-2,2])
2121
savefig("sin.png")

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
julia 1.0

sin.png

-4 Bytes
Loading

src/Bhaskara.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
module Bhaskara
22

3-
export sinπ, sin2π, cosπ, sind180
3+
export bsin, b2sin, bcos
44

55

6-
sinπ(θ) = 16-θ)θ / (5π^2 - 4-θ)θ)
6+
bsin(θ) = 16-θ)θ / (5π^2 - 4-θ)θ)
77

8-
sin2π(θ) = sign(θ) * sinπ(abs(θ))
8+
b2sin(θ) = sign(θ) * bsin(abs(θ))
99

10-
sin::T) where T<:Number = sin2π(mod+π, T(2)*π) - π)
10+
sin::T) where T<:Number = b2sin(mod+π, T(2)*π) - π)
1111

1212

1313
sind180(x) = 4(180-x)x / (40500 - (180-x)x)
1414

15-
sind360(x) = sign(x) * sin180(abs(x))
15+
sind360(x) = sign(x) * sind180(abs(x))
1616

1717
sind(x::Number) = sind360(mod(x+180,360)-180)
1818

1919

20-
cosπ(θ) =^2 - 4θ^2) /^2+θ^2)
20+
bcos(θ) =^2 - 4θ^2) /^2+θ^2)
2121

22-
cos::T) where T<:Number = sin2π(mod+π/T(2), T(2)*π) - π)
22+
cos::T) where T<:Number = b2sin(mod-π/T(2), T(2)*π) - π)
2323

2424

2525
tan::Number) = sin(θ) / cos(θ)

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Test
2+
using Bhaskara
3+
4+
test(f, g, r=0:0.001:π, e=0.002) = maximum(@. abs(f(r)-g(r)) ) < e
5+
6+
@test test(bsin, Base.sin)
7+
@test test(b2sin, Base.sin, -π:0.001:π)
8+
@test test(Bhaskara.sin, Base.sin, -3π:0.001:3π)
9+
10+
@test test(Bhaskara.sind180, Base.sind, 0:0.1:180)
11+
@test test(Bhaskara.sind360, Base.sind, -180:0.1:180)
12+
@test test(Bhaskara.sind, Base.sind, -1000:0.1:1000)
13+
14+
@test test(bcos, Base.cos, -0.5π:0.001:0.5π)
15+
@test test(Bhaskara.cos, Base.cos, -3π:0.001:3π)
16+
17+
@test bsin(big(0.1)) isa BigFloat

0 commit comments

Comments
 (0)