Eclipse Community Forums Forum Search:

Home » Modeling » OCL » real sum approximation problem
real sum approximation problem Wed, 02 November 2011 13:32
 giovi Messages: 39Registered: September 2011 Member
hi,
i've problem with a constraint in my model. I want to test in my state machine if the sum of probability (Real eattribuute) of all outgoingTransitions in any states is equal to 1. For do this i've write this ocl code:

outgoingTransition.transProb->iterate(p; res : Real = 0 | res+p) = 1.0

constraint work but the problem is in the approximation... if for example i test this rule in a state with 2 transition with 0.2 and 0.8 probability, i receive as result 1.0000000149011612, not 1.0. Where is problem?
Re: real sum approximation problem [message #754518 is a reply to message #754485] Wed, 02 November 2011 17:28
 Ed WillinkMessages: 3175Registered: July 2009 Senior Member
Hi

This is a standard problem in almost every programming language.
Floating point calculations have finite precision and so you never get

Though you have only eight accurate digits, so I suspect you have an
algorithm problem too. Traditional Ecore OCL is using doubles, the new
code uses BigDecimal.

Regards

On 02/11/2011 18:32, giovi wrote:
> hi,
> i've problem with a constraint in my model. I want to test in my state
> machine if the sum of probability (Real eattribuute) of all
> outgoingTransitions in any states is equal to 1. For do this i've
> write this ocl code:
>
> outgoingTransition.transProb->iterate(p; res : Real = 0 | res+p) = 1.0
>
> constraint work but the problem is in the approximation... if for
> example i test this rule in a state with 2 transition with 0.2 and 0.8
> probability, i receive as result 1.0000000149011612, not 1.0. Where is
> problem?
Re: real sum approximation problem [message #754526 is a reply to message #754518] Wed, 02 November 2011 19:22
 giovi Messages: 39Registered: September 2011 Member
Hi Edward and thanks for quickly response. Do you suggest me to use Double or BigDecimal type? as i know OCL support only Real type.... what you mean when you said there is an algorithm problem? it's a simple constraints in an ecore element...
Re: real sum approximation problem [message #754539 is a reply to message #754526] Thu, 03 November 2011 01:26
 Ed WillinkMessages: 3175Registered: July 2009 Senior Member
Hi

I made two points. Firstly you will not achieve precise accuracy so you
must have a tolerance.

If you want more than 8 digit accuracy you must ensure that you are
using an algorithm that is not precision sensitive, and at least double
(i.e no floats). If you are using the default OCL going to BigDecimal is
not wise.

Just make sure
b) your algorithm is sensible (i.e not 10.0e10 * (x - y)) where x and y
are almost equal
c) you do not expect exact answers

Regards

On 03/11/2011 00:22, giovi wrote:
> Hi Edward and thanks for quickly response. Do you suggest me to use
> Double or BigDecimal type? as i know OCL support only Real type....
> what you mean when you said there is an algorithm problem? it's a
> simple constraints in an ecore element...
Re: real sum approximation problem [message #754614 is a reply to message #754539] Thu, 03 November 2011 10:09
 giovi Messages: 39Registered: September 2011 Member
Thanks Edward! i changed type of probability in my ecore metamodel to double (previously float) and now it works!
 Previous Topic: [OCLinEcore] Resolving attributes in OCL Constraints Next Topic: How to get the elements of OCL document (.ocl)
Goto Forum:

Current Time: Thu May 23 15:27:55 EDT 2013