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?