@@ -32,11 +32,17 @@ const _MPFRMachineInteger = Union{_MPFRMachineSigned,_MPFRMachineUnsigned}
3232const _MPFRMachineFloat = Base. GMP. CdoubleMax
3333
3434make_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..
123159end
124160
125161macro 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... ))
127163end
128164
129165macro 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... ))
131167end
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
259296for 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
267314end
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
274328end
275329
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
299366VERSION ≥ 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
313382end
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
0 commit comments