Now I cannot add to my post...
Reading it again, maybe the easy principle I used lies a bit burried in the code I pasted. All you need is a fixed point (I used USD) and the relative rates of other items to it.

Scenario:
//var %apples = 1, %melons = 0.5, %peaches = 3
This means: for one apple, I can trade half a melon, or 3 peaches. Thats their rate. The other way round: Melons have the double value of apples, and a peach is worth 1/3 of an apple.

Now, If e.g. you want to get the exchange result trading apples and melons, you'd simply multply its number by its value:
Their value is $calc(1 / rate) ... $cac(1/0.5) == 2.
3 melons are worh $calc(2[value] * 3[number]) = 6 apples. Easy.

But how to convert peaches to melons, or vice versa? For instance, how many melons do I get for 12 peaches?

Use the "rule of proportion" and "convert" them to the "fixed point" first, to apples. In fact, we did this already above:
If 3 peaches are worth 1 apple, one peach is worh 1/3 of an apple: value = 1 / rate.

This value is now 1) multiplied by the number of items (peaches):
$calc(%num * (1 / %peaches) * %melons) ... == $calc(12 * (1/3) * 0.5)

and 2) multiplied by the rate for melons:
$calc(12 * (1/3) * 0.5) .... == $calc(12 / 3 * 0.5) ... == $calc(4 * 0.5) ... == 2

12 peaches (with a exchange rate of 3) equal 4 apples, and 1 apple is worth half a melon: 12 peaches are worth 2 melons.

Thus, the calculation goes: [number of items-to-get] == $calc([number of items-to-give] / [rate of item-to-give] * [rate of item-to-get])