Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(*){T<:AbstractFloat}(x::T, y::Vector{T}) should work #17544

Closed
JeffreySarnoff opened this issue Jul 21, 2016 · 6 comments
Closed

(*){T<:AbstractFloat}(x::T, y::Vector{T}) should work #17544

JeffreySarnoff opened this issue Jul 21, 2016 · 6 comments

Comments

@JeffreySarnoff
Copy link
Contributor

JeffreySarnoff commented Jul 21, 2016

# this works
1.5 * [1,0, 2.0]
# this does not
import Base:(*)
immutable FP <: AbstractFloat
   val::Float64
end
(*)(a::FP, b::FP) = FP(a.val * b.val)
afp=FP(1.5); fps=[FP(1.0),FP(2.0)];
afp * fps

There should be fallback definitions that support arithmetic functions where the type of the args is AbstractFloat (or maybe Real) and one or both args is a Vector (or an Array) of the same type, where the arithmetic operator is supported for two elements of that type.

@KristofferC
Copy link
Sponsor Member

Make FP <: AbstractFloat or Number and it works.

@JeffreySarnoff
Copy link
Contributor Author

JeffreySarnoff commented Jul 21, 2016

I left out the supertype -- edited above.

this error occurs

julia> immutable FP <: AbstractFloat
          val::Float64
       end

julia> import Base:*
julia> (*)(a::FP, b::FP) = FP(a.val * b.val)
* (generic function with 151 methods)

julia> afp=FP(1.5); fps=[FP(1.0),FP(2.0)];

julia> afp * fps
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type FP
This may have arisen from a call to the constructor FP(...),
since type constructors fall back to convert methods.
 in promote_op(::Base.#.*, ::Type{FP}, ::Type{FP}) at ./number.jl:73
 in .*(::FP, ::Array{FP,1}) at ./arraymath.jl:71
 in *(::FP, ::Array{FP,1}) at ./abstractarraymath.jl:54
 in eval(::Module, ::Any) at ./boot.jl:234
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46

version 0.5-

@yuyichao
Copy link
Contributor

Basically dup of #17314

@JeffreySarnoff
Copy link
Contributor Author

I may be missing the core of #17314. Importing Base:one and defining one(::Type{FP}) = FP(1.0) and similarly defining one(x::FP) does not do anything to resolve the error.

@yuyichao
Copy link
Contributor

yuyichao commented Jul 21, 2016

julia> immutable FP <: AbstractFloat
           val::Float64
       end

julia> import Base: *

julia> *(a::FP, b::FP) = FP(a.val * b.val)
* (generic function with 151 methods)

julia> Base.one(::Type{FP}) = FP(1.0)

julia> afp = FP(1.5); fps = [FP(1.0), FP(2.0)]
2-element Array{FP,1}:
 FP(1.0)
 FP(2.0)

julia> afp * fps
2-element Array{FP,1}:
 FP(1.5)
 FP(3.0)

#265

@JeffreySarnoff
Copy link
Contributor Author

fancy that -- thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants