14.14. Arithmetic with units

| Chapter 14: Numbers and Equations |

| 14.14. Arithmetic with units

The example equations in the previous section carried out quite a lot of arithmetic, but they may have given the impression that Inform always allows arithmetic - which is not true.

This is actually a good thing, because it keeps us from error. For instance, Inform will not allow:

> Equation - Newton's Totally Bogus Law
> F = m^2
> where F is a force, m is a mass.

because whatever you get when you square a mass, you don't get a force - in the same way that a length times another length makes an area, not another length. Physicists call this "dimensional analysis", and it often provides clues about which equations are right. Just after the Second World War, someone correctly worked out the explosive power of an atomic bomb without any classified information simply by guessing what values would appear in the formula, and then finding the simplest equation they could appear in.

In general, Inform will not allow numerical kinds of value to be multiplied or divided by each other (or square or cube rooted) unless we give it instructions that this would make sense.

Of course, there's plenty we can still do without any need for such instructions. For instance, going back to weight,

> The Weighbridge is a room. "A sign declares that the maximum load is [100kg multiplied by 3]."

...will produce the text "A sign declares that the maximum load is 300kg." Here Inform knows that it makes sense to multiply a weight by 3, and that the result will be a weight. Similarly, Inform allows us to add and subtract weights, and several different forms of division are allowed:

> The blackboard is in the Weighbridge. "A blackboard propped against one wall reads: '122 / 10 is [122 divided by 10] remainder [remainder after dividing 122 by 10]; but 122kg / 10kg is [122kg divided by 10kg] remainder [remainder after dividing 122kg by 10kg]; and 122kg / 10 is [122kg divided by 10] remainder [remainder after dividing 122kg by 10].'"

When we visit the Weighbridge, we find:

> A blackboard propped against one wall reads: "122 / 10 is 12 remainder 2; but 122kg / 10kg is 12 remainder 2kg; and 122kg / 10 is 12kg remainder 2kg."

Whereas we are not allowed to divide 122 by 10kg: that would make no sense, since 122 is a number and not made up of kilograms. Inform will produce a problem message if we try. Similarly, Inform won't normally allow us to multiply two weights together - but see the next section.

253
| Example Frozen Assets A treatment of money which keeps track of how much the player has on him, and a BUY command which lets him go shopping. |
RB

In our brave new world, everything will have a price, so we had better spell this out. "Frozen Assets" Price is a kind of value. $10.99 specifies a price. A thing has a price. The price of a thing is usually $0.00. After examining something for sale, say "It can be yours for [the price of the noun]." Now we assume a simple shopping model in which the player can't take anything without paying for it. Definition: a thing is free if the price of it is $0.00. Definition: a thing is for sale if it is not free. Instead of taking something for sale: say "You'll have to pay for that." Before buying something for sale when the money is not in the wallet: say "You're broke." instead. Before buying something for sale when the money is free: say "You're broke." instead. Before buying something for sale when the price of the money is less than the price of the noun: say "Your funds do not run to the price of [the noun]." instead. Instead of buying something: decrease the price of the money by the price of the noun; say "You fork over [the price of the noun] for [the noun], leaving yourself with [the price of the money]."; if the money is free, remove the money from play; now the price of the noun is $0.00; now the player is carrying the noun. The player's money object is going to be a bit unusual, because it has value but cannot itself be bought. The player carries a wallet. The wallet contains money. The price of the money is $4.50. The printed name of the money is "[price of the money] in cash". Understand "cash" as the money. Instead of taking the money: say "Best to leave it alone until you need to buy something." Instead of buying something free: say "[The noun] is yours already." Instead of buying the money: say "The money belongs to you; you buy things with it." Now we just need something to buy. The Dessert Parlor is a room. "An underlit, over-crowded room campily furnished with a lot of gilt-frame mirrors and twinkle lights: it is essentially a brothel of food. The service is slow at best, and on Saturday nights glacial. However. The wares on display more than make up for these trivial inconveniences." The vanilla ice cream is an edible thing in the Parlor. The price of the ice cream is $2.45. The description is "In the scale of ice creams, you recognize this as a very inferior vanilla because it has no adjectives in the title." The raspberry tart is an edible thing in the Parlor. The price of the tart is $4.50. The description is "An almond-laced shell packed with raspberries-under-glaze." The syllabub is an edible thing in the Parlor. The price of the syllabub is $4.25. The description is "Whipped cream, alcohol, and lime juice, a substance without any redeeming food value whatever." The espresso cake is an edible thing in the Parlor. The price of the espresso cake is $5.50. The description is "A lethal wedge of purest blackness." Test me with "inventory / examine syllabub / get syllabub / buy syllabub / drop it / get it / buy raspberry tart". Implementing caloric units for this scenario is left as an exercise for the reader.

254
| Example Money for Nothing An OFFER price FOR command, allowing the player to bargain with a flexible seller. |
RB

255
| Example Lemonade Containers for liquid which keep track of how much liquid they are holding and of what kind, and allow quantities to be moved from one container to another. |
RB

256
| Example Savannah Using the liquid implementation demonstrated in Lemonade for putting out fires. |
RB

Previous | Contents | Next