|
1 | | -# Define rounded versions of elementary functions |
2 | | -# E.g. +(a, b, RoundDown) |
3 | | -# Some, like sin(a, RoundDown) are already defined in CRlibm |
| 1 | +#= Design summary: |
4 | 2 |
|
| 3 | +This is a so-called "traits-based" design, as follows. |
5 | 4 |
|
6 | | -import Base: +, -, *, /, sin, sqrt, inv, ^, zero, convert, parse |
| 5 | +The main body of the file defines versions of elementary functions with all allowed |
| 6 | +interval rounding types, e.g. |
| 7 | ++(IntervalRounding{:correct}, a, b, RoundDown) |
| 8 | ++(IntervalRounding{:fast}, a, b, RoundDown) |
| 9 | ++(IntervalRounding{:none}, a, b, RoundDown) |
7 | 10 |
|
8 | | -# unary minus: |
9 | | --{T<:AbstractFloat}(a::T, ::RoundingMode) = -a # ignore rounding |
| 11 | +The current allowed rounding types are |
| 12 | +- :correct # correct rounding (rounding to the nearest floating-point number) |
| 13 | +- :fast # fast rounding by prevfloat and nextfloat (slightly wider than needed) |
| 14 | +- :none # no rounding at all (for speed, but forgoes any pretense at being rigorous) |
10 | 15 |
|
11 | | -# zero: |
12 | | -zero{T<:AbstractFloat}(a::Interval{T}, ::RoundingMode) = zero(T) |
13 | | -zero{T<:AbstractFloat}(::Type{T}, ::RoundingMode) = zero(T) |
| 16 | +The function `setrounding(Interval, rounding_type)` then defines rounded |
| 17 | + functions *without* an explicit rounding type, e.g. |
| 18 | +
|
| 19 | +sin(x, r::RoundingMode) = sin(IntervalRounding{:correct}, x, r) |
| 20 | +
|
| 21 | +These are overwritten when `setrounding(Interval, rounding_type)` is called again. |
| 22 | +
|
| 23 | +In Julia v0.6, but *not* in Julia v0.5, this will automatically redefine all relevant functions, in particular those used in +(a::Interval, b::Interval) etc., so that all interval functions will automatically work with the correct interval rounding type. |
| 24 | +=# |
14 | 25 |
|
15 | | -convert(::Type{BigFloat}, x, rounding_mode::RoundingMode) = setrounding(BigFloat, rounding_mode) do |
16 | | - convert(BigFloat, x) |
17 | | -end |
18 | 26 |
|
19 | | -parse{T}(::Type{T}, x, rounding_mode::RoundingMode) = setrounding(T, rounding_mode) do |
20 | | - parse(T, x) |
| 27 | +doc"""Interval rounding trait type""" |
| 28 | +immutable IntervalRounding{T} end |
| 29 | + |
| 30 | +# Functions that are the same for all rounding types: |
| 31 | +@eval begin |
| 32 | + # unary minus: |
| 33 | + -{T<:AbstractFloat}(a::T, ::RoundingMode) = -a # ignore rounding |
| 34 | + |
| 35 | + # zero: |
| 36 | + zero{T<:AbstractFloat}(a::Interval{T}, ::RoundingMode) = zero(T) |
| 37 | + zero{T<:AbstractFloat}(::Type{T}, ::RoundingMode) = zero(T) |
| 38 | + |
| 39 | + convert(::Type{BigFloat}, x, rounding_mode::RoundingMode) = |
| 40 | + setrounding(BigFloat, rounding_mode) do |
| 41 | + convert(BigFloat, x) |
| 42 | + end |
| 43 | + |
| 44 | + parse{T}(::Type{T}, x, rounding_mode::RoundingMode) = setrounding(T, rounding_mode) do |
| 45 | + parse(T, x) |
| 46 | + end |
| 47 | + |
| 48 | + |
| 49 | + sqrt{T<:Rational}(a::T, rounding_mode::RoundingMode) = setrounding(float(T), rounding_mode) do |
| 50 | + sqrt(float(a)) |
| 51 | + end |
| 52 | + |
21 | 53 | end |
22 | 54 |
|
23 | 55 |
|
| 56 | + |
24 | 57 | # no-ops for rational rounding: |
25 | 58 | for f in (:+, :-, :*, :/) |
26 | 59 | @eval $f{T<:Rational}(a::T, b::T, ::RoundingMode) = $f(a, b) |
27 | 60 | end |
28 | 61 |
|
29 | | -sqrt{T<:Rational}(a::T, rounding_mode::RoundingMode) = setrounding(float(T), rounding_mode) do |
30 | | - sqrt(float(a)) |
31 | | -end |
32 | | - |
33 | | - |
34 | 62 |
|
| 63 | +# Define functions with different rounding types: |
35 | 64 | for mode in (:Down, :Up) |
36 | 65 |
|
37 | 66 | mode1 = Expr(:quote, mode) |
38 | 67 | mode1 = :(::RoundingMode{$mode1}) |
39 | 68 |
|
40 | 69 | mode2 = Symbol("Round", mode) |
41 | 70 |
|
| 71 | + if mode == :Down |
| 72 | + directed = :prevfloat |
| 73 | + else |
| 74 | + directed = :nextfloat |
| 75 | + end |
| 76 | + |
42 | 77 |
|
43 | | - for f in (:+, :-, :*, :/, |
44 | | - :atan2) |
| 78 | + # binary functions: |
| 79 | + for f in (:+, :-, :*, :/, :atan2) |
45 | 80 |
|
46 | | - @eval begin |
47 | | - function $f{T<:AbstractFloat}(a::T, b::T, $mode1) |
48 | | - setrounding(T, $mode2) do |
49 | | - $f(a, b) |
| 81 | + @eval function $f{T<:AbstractFloat}(::Type{IntervalRounding{:correct}}, |
| 82 | + a::T, b::T, $mode1) |
| 83 | + setrounding(T, $mode2) do |
| 84 | + $f(a, b) |
| 85 | + end |
50 | 86 | end |
51 | | - end |
52 | | - end |
| 87 | + |
| 88 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:fast}}, |
| 89 | + a::T, b::T, $mode1) = $directed($f(a, b)) |
| 90 | + |
| 91 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:none}}, |
| 92 | + a::T, b::T, $mode1) = $f(a, b) |
| 93 | + |
53 | 94 | end |
54 | 95 |
|
55 | | - @eval begin |
56 | | - function ^{T<:AbstractFloat,S}(a::T, b::S, $mode1) |
57 | | - setrounding(T, $mode2) do |
58 | | - ^(a, b) |
59 | | - end |
| 96 | + |
| 97 | + # power: |
| 98 | + |
| 99 | + @eval function ^{S<:Real}(::Type{IntervalRounding{:correct}}, |
| 100 | + a::BigFloat, b::S, $mode1) |
| 101 | + setrounding(BigFloat, $mode2) do |
| 102 | + ^(a, b) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + # for correct rounding for Float64, must pass through BigFloat: |
| 107 | + @eval function ^{S<:Real}(::Type{IntervalRounding{:correct}}, a::Float64, b::S, $mode1) |
| 108 | + setprecision(BigFloat, 53) do |
| 109 | + Float64(^(IntervalRounding{:correct}, BigFloat(a), b, $mode2)) |
60 | 110 | end |
61 | 111 | end |
62 | 112 |
|
| 113 | + @eval ^{T<:AbstractFloat,S<:Real}(::Type{IntervalRounding{:fast}}, |
| 114 | + a::T, b::S, $mode1) = $directed(a^b) |
| 115 | + |
| 116 | + @eval ^{T<:AbstractFloat,S<:Real}(::Type{IntervalRounding{:none}}, |
| 117 | + a::T, b::S, $mode1) = a^b |
| 118 | + |
| 119 | + |
| 120 | + # functions not in CRlibm: |
| 121 | + for f in (:sqrt, :inv, :tanh, :asinh, :acosh, :atanh) |
| 122 | + |
| 123 | + @eval function $f{T<:AbstractFloat}(::Type{IntervalRounding{:correct}}, |
| 124 | + a::T, $mode1) |
| 125 | + setrounding(T, $mode2) do |
| 126 | + $f(a) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + |
| 131 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:fast}}, |
| 132 | + a::T, $mode1) = $directed($f(a)) |
| 133 | + |
| 134 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:none}}, |
| 135 | + a::T, $mode1) = $f(a) |
| 136 | + |
63 | 137 |
|
64 | | - for f in (:sqrt, :inv, |
65 | | - :tanh, :asinh, :acosh, :atanh) # these functions not in CRlibm |
66 | | - @eval begin |
67 | | - function $f{T<:AbstractFloat}(a::T, $mode1) |
68 | | - setrounding(T, $mode2) do |
69 | | - $f(a) |
70 | | - end |
71 | | - end |
72 | | - end |
73 | 138 | end |
74 | 139 |
|
75 | 140 |
|
| 141 | + # Functions defined in CRlibm |
| 142 | + |
76 | 143 | for f in CRlibm.functions |
77 | | - @eval $f{T<:AbstractFloat}(a::T, $mode1) = CRlibm.$f(a, $mode2) |
| 144 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:correct}}, |
| 145 | + a::T, $mode1) = CRlibm.$f(a, $mode2) |
| 146 | + |
| 147 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:fast}}, |
| 148 | + a::T, $mode1) = $directed($f(a)) |
| 149 | + |
| 150 | + @eval $f{T<:AbstractFloat}(::Type{IntervalRounding{:none}}, |
| 151 | + a::T, $mode1) = $f(a) |
| 152 | + |
| 153 | + end |
| 154 | +end |
| 155 | + |
| 156 | +doc""" |
| 157 | + setrounding(Interval, rounding_type::Symbol) |
| 158 | +
|
| 159 | +Set the rounding type used for all interval calculations on Julia v0.6 and above. |
| 160 | +Valid `rounding_type`s are `:correct`, `:fast` and `:none`. |
| 161 | +""" |
| 162 | +function setrounding(::Type{Interval}, rounding_type::Symbol) |
| 163 | + |
| 164 | + if rounding_type == current_rounding_type[] |
| 165 | + return # no need to redefine anything |
| 166 | + end |
| 167 | + |
| 168 | + if rounding_type ∉ (:correct, :fast, :none) |
| 169 | + throw(ArgumentError("Rounding type must be one of `:correct`, `:fast`, `:none`")) |
| 170 | + end |
| 171 | + |
| 172 | + roundtype = IntervalRounding{rounding_type} |
| 173 | + |
| 174 | + |
| 175 | + # binary functions: |
| 176 | + for f in (:+, :-, :*, :/, :^, :atan2) |
| 177 | + |
| 178 | + @eval $f{T<:AbstractFloat}(a::T, b::T, r::RoundingMode) = $f($roundtype, a, b, r) |
78 | 179 | end |
79 | 180 |
|
| 181 | + @eval ^{T<:AbstractFloat, S<:Real}(a::T, b::S, r::RoundingMode) = ^($roundtype, promote(a, b)..., r) |
| 182 | + |
| 183 | + # unary functions: |
| 184 | + for f in vcat(CRlibm.functions, |
| 185 | + [:sqrt, :inv, :tanh, :asinh, :acosh, :atanh]) |
| 186 | + |
| 187 | + @eval $f{T<:AbstractFloat}(a::T, r::RoundingMode) = $f($roundtype, a, r) |
| 188 | + |
| 189 | + @eval $f(x::Real, r::RoundingMode) = $f(float(x), r) |
| 190 | + |
| 191 | + end |
| 192 | + |
| 193 | + current_rounding_type[] = rounding_type |
80 | 194 | end |
| 195 | + |
| 196 | +# default: correct rounding |
| 197 | +const current_rounding_type = Symbol[:undefined] |
| 198 | +setrounding(Interval, :correct) |
0 commit comments