Skip to content

Commit 43245c3

Browse files
jw3126dextorious
authored andcommitted
replace 0.5 by 1//2 (#10)
1 parent 5ee27b6 commit 43245c3

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/NumericalIntegration.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Compat
77
export integrate
88
export Trapezoidal, TrapezoidalEven, TrapezoidalFast, TrapezoidalEvenFast
99
export SimpsonEven, SimpsonEvenFast
10+
export IntegrationMethod
1011

1112
@compat abstract type IntegrationMethod end
1213

@@ -17,26 +18,28 @@ immutable TrapezoidalEvenFast <: IntegrationMethod end
1718
immutable SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
1819
immutable SimpsonEvenFast <: IntegrationMethod end
1920

21+
const HALF = 1//2
22+
2023
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::Trapezoidal)
2124
@assert length(x) == length(y) "x and y vectors must be of the same length!"
2225
retval = zero(promote(x[1], y[1])[1])
2326
for i in 1 : length(y)-1
2427
retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
2528
end
26-
return 0.5 * retval
29+
return HALF * retval
2730
end
2831

2932
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEven)
3033
@assert length(x) == length(y) "x and y vectors must be of the same length!"
31-
return (x[2] - x[1]) * (0.5 * (y[1] + y[end]) + sum(y[2:end-1]))
34+
return (x[2] - x[1]) * (HALF * (y[1] + y[end]) + sum(y[2:end-1]))
3235
end
3336

3437
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalFast)
3538
retval = zero(promote(x[1], y[1])[1])
3639
@fastmath @simd for i in 1 : length(y)-1
3740
@inbounds retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
3841
end
39-
return 0.5 * retval
42+
return HALF * retval
4043
end
4144

4245
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEvenFast)
@@ -45,7 +48,7 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
4548
@fastmath @simd for i in 2 : N
4649
@inbounds retval += y[i]
4750
end
48-
@inbounds return (x[2] - x[1]) * (retval + 0.5*y[1] + 0.5*y[end])
51+
@inbounds return (x[2] - x[1]) * (retval + HALF*y[1] + HALF*y[end])
4952
end
5053

5154
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEven)

test/runtests.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
using NumericalIntegration
22
using Base.Test
33

4-
methods = [Trapezoidal(), TrapezoidalEven(), TrapezoidalFast(), TrapezoidalEvenFast(), SimpsonEven(), SimpsonEvenFast()]
54
x = collect(-π : π/1000 : π)
65
y = sin.(x)
76
p = collect(0 : π/1000 : π)
87
q = sin.(p)
9-
for method in methods
10-
println(string("Testing method: ", typeof(method)))
11-
@test abs(integrate(x, y, method)) < 1e-4
12-
@test abs(integrate(p, q, method)-2.0) < 1e-4
8+
for M in subtypes(IntegrationMethod)
9+
println("Testing method: ", M)
10+
for T in [Float32, Float64, BigFloat]
11+
for (xs,ys,val,atol) in [
12+
(x,y,0,1e-4),
13+
(p,q,2,1e-4),
14+
]
15+
result = @inferred integrate(T.(xs), T.(ys),M())
16+
@test isapprox(result, val, atol=atol)
17+
@test typeof(result) == T
18+
end
19+
end
1320
end

0 commit comments

Comments
 (0)