I have a user interface with two numeric input boxes, send amount and receive amount. The values are in different currencies and are related by an exchange rate. That is, receive amount should always be send amount * exchange rate. But the user is allowed to click into either input box and modify the value, which causes the other value to be updated.I've got a simple system to observe changes to either value and update the other one. I've had problems with infinite loops of the values updating each other because they're floats and multiplying then dividing by the exchange rate doesn't always create precisely the same value. So I added a simple flag to ignore updates while the updater itself is running.But now I'm adding more functionality when the user updates an amount field, requiring other objects to listen to the fields. As a result some of those objects end up changing one of the amount fields, which sets off an infinite loop.I've always felt like this is a fragile modelling of the problem, but I haven't thought of anything better. Is there any established pattern or approach to modelling two values that update each other?
Two user inputs that alter each other
observer pattern;data modeling
The right way to implement state and state change notifications is epitomized by the following setter method pseudocode:method setValue( T newValue ){    if( newValue equals existingValue )        return;    existingValue = newValue;    issue state change notification;}This is known to work, and to work perfectly.  No infinite loops, no oscillations, no unnecessary notifications, no problems whatsoever.Until one day someone decides to use floats.Your problem is not with the very well established concept of state change notifications and the observer pattern, your problem is with the fact that you have been using a crappy data type for the job.You tried to overcome the malfunctions caused by the imprecision inherent in floats with an approach which, although not outright wrong, was not really the correct approach for the problem at hand: you added a flag to ignore updates while the updater itself is running.So, here are some approaches for solving your problem directly, instead of letting it be and trying to work around it:Use proper equality comparisons for your floats: if( abs( float1 - float2 ) < epsilon ) where epsilon is a very small number like 0.00000001, depending on the desired precision.  For more information, see this: What's wrong with using == to compare floats in Java?Use a decimal or BigDecimal data type instead of float. If you are dealing with currency values, you definitely should be doing this. For more information, see this: Why not use Double or Float to represent currency?