Hey Jeffrey,

it's been a while, thx for the answer. I see that this would be working. 
However, what about min, max, sin, etc.? I do not want to re-implement all 
elementary functions for the Probability type. There must be some way to 
inherit the behaviour of the abstract supertype "Real". I guess I am 
missing something fundamental about the type system here. 

I felt tat something like

type Probability{T<:Real} <:Real
    p::T
end

import Base.convert

convert{T<:Real}(::Type{T}, x::Probability) = convert(T, x.p)
convert{T1<:Real, T2<:Real}(::Type{Probability{T1}}, x::T2) = 
Probability(convert(T1, x))


should do the job as now any Probability can be converted to any concrete 
subtype of Real and "+" shoud be implemented there ;)
Very strange, how does Julia handle inheritance at all???

Best, 

Kevin


On Monday, 29 February 2016 23:45:35 UTC+1, Jeffrey Sarnoff wrote:
>
> Kevin,
>
> If all that you ask of this type is that it does arithmetic, clamps any 
> negative values to zero, and clamps any values greater than one to one, 
> that is easy enough. Just note that arithmetic with probabilities usually 
> is more subtle than that.
>
> import Base: +,-,*,/
>
> immutable Probability <: Real
>      val::Float64
>
>      Probability(x::Float64) = min(1.0, max(0.0, x))
> end
>
> (+){T<:Probability}(a::T, b::T) = Probability( a.val + b.val )
> (-){T<:Probability}(a::T, b::T) = Probability( a.val - b.val )
> (*){T<:Probability}(a::T, b::T) = Probability( a.val * b.val )
> (/){T<:Probability}(a::T, b::T) = Probability( a.val / b.val )
>
> You need conversion and promotion if you want to mix Float64 values and 
> Probability values: 2.0 * Probability(0.25) == Probability(0.5).
>
> On Sunday, February 28, 2016 at 10:33:07 AM UTC-5, Kevin Kunzmann wrote:
>>
>> Hey,
>>
>> I have a (probably) very simple question. I would like to define 
>> 'Probability' as a new subtype of 'Real', only with the additional 
>> restriction that the value must be between 0 and 1. How would I achieve 
>> that 'Julia-style'? This should be possible without having to rewrite all 
>> these promotion rules and stuff, is it not? 
>>
>> Best Kevin
>>
>

Reply via email to