Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » real sum approximation problem
real sum approximation problem [message #754485] Wed, 02 November 2011 17:32 Go to next message
giovi disanto is currently offline giovi disantoFriend
Messages: 58
Registered: 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 21:28 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: 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
exact answers. You must introduce a tolerance on your requirement.

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

Ed Willink

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 23:22 Go to previous messageGo to next message
giovi disanto is currently offline giovi disantoFriend
Messages: 58
Registered: 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 05:26 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: 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
a) your input is double
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

Ed Willink

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 14:09 Go to previous message
giovi disanto is currently offline giovi disantoFriend
Messages: 58
Registered: 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: Wed Apr 24 14:04:16 GMT 2024

Powered by FUDForum. Page generated in 0.25163 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top