Skip to content

Commit 28ea00c

Browse files
committed
Run formatter
1 parent 8d12aeb commit 28ea00c

2 files changed

Lines changed: 135 additions & 42 deletions

File tree

src/implementations/BigFloat.jl

Lines changed: 104 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ const _MPFRMachineInteger = Union{_MPFRMachineSigned,_MPFRMachineUnsigned}
3232
const _MPFRMachineFloat = Base.GMP.CdoubleMax
3333

3434
make_mpfr_error() = error("Invalid use of @make_mpfr")
35-
function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr...)
35+
function make_mpfr_impl(
36+
fn::Expr,
37+
mod::Module,
38+
rounding_mode::Bool,
39+
rest::Expr...,
40+
)
3641
pre = :()
3742
post = :()
3843
for restᵢ in rest
39-
restᵢ isa Expr && restᵢ.head === :(=) && length(restᵢ.args) == 2 || make_mpfr_error()
44+
restᵢ isa Expr && restᵢ.head === :(=) && length(restᵢ.args) == 2 ||
45+
make_mpfr_error()
4046
if restᵢ.args[1] === :pre
4147
pre = restᵢ.args[2]
4248
elseif restᵢ.args[1] === :post
@@ -48,7 +54,8 @@ function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr..
4854
fn.head === :(->) || make_mpfr_error()
4955
if fn.args[2] isa Expr
5056
# Julia likes to insert a line number node
51-
(fn.args[2].head === :block && fn.args[2].args[1] isa LineNumberNode) || make_mpfr_error()
57+
(fn.args[2].head === :block && fn.args[2].args[1] isa LineNumberNode) ||
58+
make_mpfr_error()
5259
fn_name = fn.args[2].args[end]
5360
else
5461
fn_name = fn.args[2]
@@ -76,13 +83,20 @@ function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr..
7683
catch
7784
return :() # some functions may not be known in all Julia versions - this is not an error
7885
end
79-
args = sizehint!(Any[], length(fn.args) -1)
80-
argnames = sizehint!(Symbol[], length(fn.args) -1)
81-
types = sizehint!(Any[], length(fn.args) + (return_type === BigFloat ? 0 : fieldcount(return_type) -1))
86+
args = sizehint!(Any[], length(fn.args) - 1)
87+
argnames = sizehint!(Symbol[], length(fn.args) - 1)
88+
types = sizehint!(
89+
Any[],
90+
length(fn.args) +
91+
(return_type === BigFloat ? 0 : fieldcount(return_type) - 1),
92+
)
8293
if return_type === BigFloat
8394
push!(types, Ref{BigFloat})
8495
else
85-
append!(types, Iterators.repeated(Ref{BigFloat}, fieldcount(return_type)))
96+
append!(
97+
types,
98+
Iterators.repeated(Ref{BigFloat}, fieldcount(return_type)),
99+
)
86100
end
87101
for (i, argᵢ) in enumerate(Iterators.drop(fn.args, 1))
88102
argᵢ isa Expr && argᵢ.head === :(::) || make_mpfr_error()
@@ -92,7 +106,8 @@ function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr..
92106
push!(args, Expr(:(::), argtype))
93107
continue
94108
end
95-
argname = length(argᵢ.args) == 2 ? argᵢ.args[1]::Symbol : Symbol(:arg, i)
109+
argname =
110+
length(argᵢ.args) == 2 ? argᵢ.args[1]::Symbol : Symbol(:arg, i)
96111
push!(args, Expr(:(::), argname, argtype))
97112
push!(argnames, argname)
98113
if isbitstype(argtype)
@@ -104,17 +119,38 @@ function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr..
104119
end
105120
end
106121
return quote
107-
promote_operation(::typeof($ju_name), $((:(::Type{<:$(arg.args[end])}) for arg in args)...)) = $return_type
122+
function promote_operation(
123+
::typeof($ju_name),
124+
$((:(::Type{<:$(arg.args[end])}) for arg in args)...),
125+
)
126+
return $return_type
127+
end
108128

109129
function operate_to!(out::$return_type, ::typeof($ju_name), $(args...))
110130
$pre
111131
ccall(
112132
($(QuoteNode(fn_name)), :libmpfr),
113133
Int32,
114-
($(types...), $((typeof(s) for s in surplus_args)...), $((rounding_mode ? (:($_MPFRRoundingMode),) : ())...)),
115-
$((return_type <: Tuple ? (:(out[$i]) for i in 1:fieldcount(return_type)) : (:out,))...),
116-
$(argnames...), $(surplus_args...),
117-
$((rounding_mode ? (:($(Base.Rounding.rounding_raw)($BigFloat)),) : ())...),
134+
(
135+
$(types...),
136+
$((typeof(s) for s in surplus_args)...),
137+
$((rounding_mode ? (:($_MPFRRoundingMode),) : ())...),
138+
),
139+
$(
140+
(
141+
return_type <: Tuple ?
142+
(:(out[$i]) for i in 1:fieldcount(return_type)) :
143+
(:out,)
144+
)...
145+
),
146+
$(argnames...),
147+
$(surplus_args...),
148+
$(
149+
(
150+
rounding_mode ?
151+
(:($(Base.Rounding.rounding_raw)($BigFloat)),) : ()
152+
)...
153+
),
118154
)
119155
$post
120156
return out
@@ -123,11 +159,11 @@ function make_mpfr_impl(fn::Expr, mod::Module, rounding_mode::Bool, rest::Expr..
123159
end
124160

125161
macro make_mpfr(fn::Expr, rest::Expr...)
126-
esc(make_mpfr_impl(fn, __module__, true, rest...))
162+
return esc(make_mpfr_impl(fn, __module__, true, rest...))
127163
end
128164

129165
macro make_mpfr_noround(fn::Expr, rest::Expr...)
130-
esc(make_mpfr_impl(fn, __module__, false, rest...))
166+
return esc(make_mpfr_impl(fn, __module__, false, rest...))
131167
end
132168

133169
# copy
@@ -157,7 +193,8 @@ operate!(::typeof(one), x::BigFloat) = operate_to!(x, copy, 1)
157193
# ldexp
158194

159195
@make_mpfr ldexp(::_MPFRMachineSigned, ::_MPFRMachineSigned) -> mpfr_set_si_2exp
160-
@make_mpfr ldexp(::_MPFRMachineUnsigned, ::_MPFRMachineSigned) -> mpfr_set_ui_2exp
196+
@make_mpfr ldexp(::_MPFRMachineUnsigned, ::_MPFRMachineSigned) ->
197+
mpfr_set_ui_2exp
161198
@make_mpfr ldexp(::BigFloat, ::_MPFRMachineSigned) -> mpfr_mul_2si
162199
@make_mpfr ldexp(::BigFloat, ::_MPFRMachineUnsigned) -> mpfr_mul_2ui
163200

@@ -259,17 +296,34 @@ end
259296
for f in (:log, :log2, :log10)
260297
@eval @make_mpfr $f(x::BigFloat) -> $(Symbol(:mpfr_, f)) pre=begin
261298
if x < 0
262-
throw(DomainError(x, string($f, " was called with a negative real argument but ",
263-
"will only return a complex result if called ",
264-
"with a complex argument. Try ", $f, "(complex(x)).")))
299+
throw(
300+
DomainError(
301+
x,
302+
string(
303+
$f,
304+
" was called with a negative real argument but ",
305+
"will only return a complex result if called ",
306+
"with a complex argument. Try ",
307+
$f,
308+
"(complex(x)).",
309+
),
310+
),
311+
)
265312
end
266313
end
267314
end
268315
@make_mpfr log1p(x::BigFloat) -> mpfr_log1p pre=begin
269316
if x < -1
270-
throw(DomainError(x, string("log1p was called with a real argument < -1 but ",
271-
"will only return a complex result if called ",
272-
"with a complex argument. Try log1p(complex(x)).")))
317+
throw(
318+
DomainError(
319+
x,
320+
string(
321+
"log1p was called with a real argument < -1 but ",
322+
"will only return a complex result if called ",
323+
"with a complex argument. Try log1p(complex(x)).",
324+
),
325+
),
326+
)
273327
end
274328
end
275329

@@ -287,8 +341,21 @@ end
287341

288342
# trigonometric
289343
# Functions for which NaN results are converted to DomainError, following Base
290-
for f in (:sin, :cos, :tan, :cot, :sec, :csc, :acos, :asin, :atan, :acosh, :asinh, :atanh,
291-
(VERSION v"1.10" ? (:sinpi, :cospi, :tanpi) : ())...)
344+
for f in (
345+
:sin,
346+
:cos,
347+
:tan,
348+
:cot,
349+
:sec,
350+
:csc,
351+
:acos,
352+
:asin,
353+
:atan,
354+
:acosh,
355+
:asinh,
356+
:atanh,
357+
(VERSION v"1.10" ? (:sinpi, :cospi, :tanpi) : ())...,
358+
)
292359
@eval @make_mpfr $f(x::BigFloat) -> $(Symbol(:mpfr_, f)) pre=begin
293360
isnan(x) && return operate_to!(out, copy, x)
294361
end post=begin
@@ -298,21 +365,24 @@ end
298365
@make_mpfr sincos(::BigFloat)::Tuple{BigFloat,BigFloat} -> mpfr_sin_cos
299366
VERSION v"1.10" && for f in (:sin, :cos, :tan)
300367
@eval begin
301-
@make_mpfr $(Symbol(f, :d))(x::BigFloat) -> ($(Symbol(:mpfr_, f, :u)), 0x00000168) pre=begin
368+
@make_mpfr $(Symbol(f, :d))(x::BigFloat) ->
369+
($(Symbol(:mpfr_, f, :u)), 0x00000168) pre=begin
302370
isnan(x) && return operate_to!(out, copy, x)
303371
end post=begin
304372
isnan(out) && throw(DomainError(x, "NaN result for non-NaN input."))
305373
end
306374

307-
@make_mpfr $(Symbol(:a, f, :d))(x::BigFloat) -> ($(Symbol(:mpfr_a, f, :u)), 0x00000168) pre=begin
375+
@make_mpfr $(Symbol(:a, f, :d))(x::BigFloat) ->
376+
($(Symbol(:mpfr_a, f, :u)), 0x00000168) pre=begin
308377
isnan(x) && return operate_to!(out, copy, x)
309378
end post=begin
310379
isnan(out) && throw(DomainError(x, "NaN result for non-NaN input."))
311380
end
312381
end
313382
end
314383
@make_mpfr atan(::BigFloat, ::BigFloat) -> mpfr_atan2
315-
VERSION v"1.10" && @make_mpfr atand(::BigFloat, ::BigFloat) -> (mpfr_atan2u, 0x00000168)
384+
VERSION v"1.10" &&
385+
@make_mpfr atand(::BigFloat, ::BigFloat) -> (mpfr_atan2u, 0x00000168)
316386

317387
# hyperbolic
318388
@make_mpfr cosh(::BigFloat) -> mpfr_cosh
@@ -327,11 +397,15 @@ VERSION ≥ v"1.10" && @make_mpfr atand(::BigFloat, ::BigFloat) -> (mpfr_atan2u,
327397
@make_mpfr_noround round(::BigFloat, ::RoundingMode{:Up}) -> mpfr_ceil
328398
@make_mpfr_noround round(::BigFloat, ::RoundingMode{:Down}) -> mpfr_floor
329399
@make_mpfr_noround round(::BigFloat, ::RoundingMode{:ToZero}) -> mpfr_trunc
330-
@make_mpfr_noround round(::BigFloat, ::RoundingMode{:NearestTiesAway}) -> mpfr_round
400+
@make_mpfr_noround round(::BigFloat, ::RoundingMode{:NearestTiesAway}) ->
401+
mpfr_round
331402

332-
@make_mpfr modf(::BigFloat)::Tuple{BigFloat,BigFloat} -> mpfr_modf pre=(out = reverse(out)) post=(out = reverse(out))
403+
@make_mpfr modf(::BigFloat)::Tuple{BigFloat,BigFloat} -> mpfr_modf pre=(
404+
out = reverse(out)
405+
) post=(out = reverse(out))
333406
@make_mpfr rem(::BigFloat, ::BigFloat) -> mpfr_fmod
334-
@make_mpfr rem(::BigFloat, ::BigFloat, ::RoundingMode{:Nearest}) -> mpfr_remainder
407+
@make_mpfr rem(::BigFloat, ::BigFloat, ::RoundingMode{:Nearest}) ->
408+
mpfr_remainder
335409

336410
# miscellaneous
337411
@make_mpfr min(::BigFloat, ::BigFloat) -> mpfr_min

test/bigfloat_wrappers.jl

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
@testset "MPFR wrappers" begin
22
# call MA.operate_to!(out, op, args...) and compare with expected. If Base throws for
33
# the inputs, assert MA throws the same kind of error.
4-
function check_op_matches_expected(op, args...; out=BigFloat(), expected=missing)
4+
function check_op_matches_expected(
5+
op,
6+
args...;
7+
out = BigFloat(),
8+
expected = missing,
9+
)
510
# preserve originals for mutation checks
611
old_args = MA.copy_if_mutable.(args)
712

@@ -45,11 +50,11 @@
4550
check_op_matches_expected(copy, big"12.")
4651
check_op_matches_expected(copy, -12)
4752
check_op_matches_expected(copy, UInt(12))
48-
check_op_matches_expected(copy, 12f0)
49-
check_op_matches_expected(copy, 12.)
53+
check_op_matches_expected(copy, 12.0f0)
54+
check_op_matches_expected(copy, 12.0)
5055

51-
check_op_matches_expected(ldexp, -5, 3; expected=ldexp(-5., 3))
52-
check_op_matches_expected(ldexp, UInt(5), 3; expected=ldexp(5., 3))
56+
check_op_matches_expected(ldexp, -5, 3; expected = ldexp(-5.0, 3))
57+
check_op_matches_expected(ldexp, UInt(5), 3; expected = ldexp(5.0, 3))
5358
check_op_matches_expected(ldexp, big"5.", -3)
5459
check_op_matches_expected(ldexp, big"5.", UInt(3))
5560

@@ -62,15 +67,15 @@
6267
check_op_matches_expected(-, big"1.5", big"2.5")
6368
check_op_matches_expected(-, big"1.5", Int32(-42))
6469
check_op_matches_expected(-, big"1.5", 0x6525)
65-
check_op_matches_expected(-, big"1.5", 17.)
70+
check_op_matches_expected(-, big"1.5", 17.0)
6671
check_op_matches_expected(-, Int16(-62), big"1.5")
6772
check_op_matches_expected(-, 0x73764fa, big"1.5")
68-
check_op_matches_expected(-, 24., big"1.5")
73+
check_op_matches_expected(-, 24.0, big"1.5")
6974

7075
check_op_matches_expected(*, big"63.6", big"91.")
7176
check_op_matches_expected(*, big"63.6", Int32(-52))
7277
check_op_matches_expected(*, big"63.6", 0x53d2a)
73-
check_op_matches_expected(*, big"63.6", 8f3)
78+
check_op_matches_expected(*, big"63.6", 8.0f3)
7479

7580
for dividend in (58, 0)
7681
check_op_matches_expected(/, big"5352.5", BigFloat(dividend))
@@ -126,7 +131,11 @@
126131
check_op_matches_expected(tand, big"30.0")
127132
check_op_matches_expected(tand, big"90.")
128133
end
129-
check_op_matches_expected(sincos, big"0.5"; out=(BigFloat(), BigFloat()))
134+
check_op_matches_expected(
135+
sincos,
136+
big"0.5";
137+
out = (BigFloat(), BigFloat()),
138+
)
130139
check_op_matches_expected(sec, big"0.7")
131140
check_op_matches_expected(csc, big"0.7")
132141
check_op_matches_expected(cot, big"0.7")
@@ -151,15 +160,25 @@
151160
check_op_matches_expected(asinh, big"0.5")
152161
check_op_matches_expected(atanh, big"0.3")
153162

154-
for rm in (RoundNearest, RoundUp, RoundDown, RoundToZero, RoundNearestTiesAway)
163+
for rm in (
164+
RoundNearest,
165+
RoundUp,
166+
RoundDown,
167+
RoundToZero,
168+
RoundNearestTiesAway,
169+
)
155170
check_op_matches_expected(round, big"24.", rm)
156171
end
157172

158-
check_op_matches_expected(modf, big"3.1415"; out=(BigFloat(), BigFloat()))
173+
check_op_matches_expected(
174+
modf,
175+
big"3.1415";
176+
out = (BigFloat(), BigFloat()),
177+
)
159178
check_op_matches_expected(rem, big"7.5", big"2.3")
160179
check_op_matches_expected(rem, big"7.5", big"2.3", RoundNearest)
161180
check_op_matches_expected(min, big"1.2", big"2.3")
162181
check_op_matches_expected(max, big"1.2", big"2.3")
163-
check_op_matches_expected(copysign, big"-1.2", big"2.0")
182+
return check_op_matches_expected(copysign, big"-1.2", big"2.0")
164183
end
165184
end

0 commit comments

Comments
 (0)