Re: [julia-users] Re: Trait for exactness of numbers
I see thank you very much for your answer! :D El martes, 25 de octubre de 2016, 13:20:50 (UTC-5), Tim Holy escribió: > > > Why not use dispatch instead? > > Because subtyping isn't powerful enough for all needs. For example: > > > julia> using Unitful > > julia> const mm = u"mm" > mm > > julia> isa(3.2mm, AbstractFloat) > false > > > You'd probably like to use the fancy logic of `FloatRange` if you're > constructing a range `3.2mm:0.1mm:4.8mm`, so the solution is to dispatch on > a trait that indicates that arithmetic isn't exact (which is what really is > going on inside that code anyway---who cares what kind of number type it > is). > > Best, > --Tim > > On Tue, Oct 25, 2016 at 12:55 PM, Ismael Venegas Castelló < > ismael...@gmail.com > wrote: > >> Why not use dispatch instead? >> >> isexact(::Integer) = true >> isexact(::Rational) = true >> isexact(x::Complex) = isexact(x.re) >> isexact(::Any) = false > > >
Re: [julia-users] Re: Trait for exactness of numbers
> Why not use dispatch instead? Because subtyping isn't powerful enough for all needs. For example: julia> using Unitful julia> const mm = u"mm" mm julia> isa(3.2mm, AbstractFloat) false You'd probably like to use the fancy logic of `FloatRange` if you're constructing a range `3.2mm:0.1mm:4.8mm`, so the solution is to dispatch on a trait that indicates that arithmetic isn't exact (which is what really is going on inside that code anyway---who cares what kind of number type it is). Best, --Tim On Tue, Oct 25, 2016 at 12:55 PM, Ismael Venegas Castelló < ismael.vc1...@gmail.com> wrote: > Why not use dispatch instead? > > isexact(::Integer) = true > isexact(::Rational) = true > isexact(x::Complex) = isexact(x.re) > isexact(::Any) = false
[julia-users] Re: Trait for exactness of numbers
Why not use dispatch instead? isexact(::Integer) = true isexact(::Rational) = true isexact(x::Complex) = isexact(x.re) isexact(::Any) = false
Re: [julia-users] Re: Trait for exactness of numbers
+1. We need number traits for a variety of circumstances; I was also contemplating them as a step in generalizing the FloatRange/StepRange distinction, for example to Unitful numbers (numbers with physical units). You need type-stability for the created range object, so I think a trait is the only way to go here. --Tim On Mon, Oct 24, 2016 at 3:30 PM, Jeffrey Sarnoff wrote: > for values, something like this may do: > > function isexact(x) > if isa(x, Integer) || isa(x, Rational) > true > elseif isa(x, Complex) > isExact(x.re) > else > false > end > end > > > > > On Monday, October 24, 2016 at 2:09:09 PM UTC-4, jw3126 wrote: >> >> A couple of times I was in a situation, where I had two algorithms >> solving the same problem, one which was faster and one which was more >> numerically stable. Now for some types of numbers (FloatingPoint, >> Complex{Float64}...) numerical stability is important while for others it >> does not matter (e.g. Integer, Complex{Int64}...) etc. >> In such situations it would be handy to have a mechanism (a trait) which >> decides whether a type is exact or prone to rounding. >> Maybe there is already such a thing somewhere? >> >
[julia-users] Re: Trait for exactness of numbers
for values, something like this may do: function isexact(x) if isa(x, Integer) || isa(x, Rational) true elseif isa(x, Complex) isExact(x.re) else false end end On Monday, October 24, 2016 at 2:09:09 PM UTC-4, jw3126 wrote: > > A couple of times I was in a situation, where I had two algorithms solving > the same problem, one which was faster and one which was more numerically > stable. Now for some types of numbers (FloatingPoint, Complex{Float64}...) > numerical stability is important while for others it does not matter (e.g. > Integer, Complex{Int64}...) etc. > In such situations it would be handy to have a mechanism (a trait) which > decides whether a type is exact or prone to rounding. > Maybe there is already such a thing somewhere? >