Inform 7 Home Page / Documentation
§15.20. Multiplication of units
To recap, then, it is forbidden to multiply 122kg and 10kg, not because it could never make sense (a scientist might occasionally multiply two weights) but because the result is - what? Not a number, and not a weight any more. But we are allowed to tell Inform what the result ought to be, and once we have done so, the multiplication will be allowed:
A length is a kind of value. 10m specifies a length. An area is a kind of value. 10 sq m specifies an area.
The balance platform is in the Weighbridge. "The balance platform is 10m by 8m, giving it an area of [10m multiplied by 8m]."
which will turn up as:
The balance platform is 10m by 8m, giving it an area of 80 sq m.
And having told Inform that lengths multiply to area, we could also divide an area by a length to get a length: no further instructions would be needed.
The built-in "Metric Units" extension includes all of the standard ways that physical quantities are multiplied, and a good way to see these is to try out one of the Metric Units examples and look at the Kinds index, which includes a table showing how all of this works.
Start of Chapter 15: Numbers and Equations | |
Back to §15.19. Arithmetic with units | |
Onward to Chapter 16: Tables: §16.1. Laying out tables |
ExampleDepth |
In the following, we pretend that every item has a cuboidal shape. Every thing has a length, width and depth, while a "measured container" also has interior dimensions. (Thus a 10x10x10 container with 1cm-thick sides might have interior dimensions 9x9x9.)
A length is a kind of value. 10 cm specifies a length. An area is a kind of value. 10 sq cm specifies an area. A length times a length specifies an area. A volume is a kind of value. 10 cu cm specifies a volume. A length times an area specifies a volume.
A thing has a length called height. A thing has a length called width. A thing has a length called depth. The height of a thing is usually 10 cm. The width of a thing is usually 10 cm. The depth of a thing is usually 10 cm.
To decide what volume is the exterior volume of (item - a thing):
let base area be the height of the item multiplied by the width of the item;
let base volume be the base area multiplied by the depth of the item;
decide on the base volume.
In order to see how these shapes might fit together spatially, we need to work out the three dimensions in order of size. (If we were only dealing with portable objects, we could simply insist that the length always be greater than the width which in turn must be greater than the depth, because we could always turn them over in our hands until this was so: but some of the things we deal with may be fixed in place.) A clever way to do this might be to put them in a table of three rows and sort it, but we will write the calculation out longhand:
To decide what length is the largest dimension of (item - a thing):
let long side be the height of item;
if the width of the item is greater than the long side, now the long side is the width of the item;
if the depth of the item is greater than the long side, now the long side is the depth of the item;
decide on the long side.
To decide what length is the middling dimension of (item - a thing):
let longer side be the height of item;
let shorter side be the width of item;
if the width of the item is greater than the height of the item:
let shorter side be the height of item;
let longer side be the width of item;
if the depth of the item is greater than the longer side, decide on the longer side;
if the depth of the item is less than the shorter side, decide on the shorter side;
decide on the depth of the item.
To decide what length is the shortest dimension of (item - a thing):
let short side be the height of item;
if the width of the item is less than the short side, now the short side is the width of the item;
if the depth of the item is less than the short side, now the short side is the depth of the item;
decide on the short side.
When testing this example, the author made use of the following: it's no longer needed, but may be useful to anyone else planning elaborations.
To test the dimensions of (item - a thing):
say "[the item] - height [height of the item], width [width of the item], depth [depth of the item].";
say "largest side [largest dimension of the item], middling [middling dimension of the item], smallest [shortest dimension of the item]."
We now introduce a new kind: a measured container, which not only has exterior dimensions - the height, width and depth which every thing now has - but also interior measurements. A convenient way to do calculations with the hollow interior is to regard it as if it were a solid shape in its own right, and we do this with the aid of something out of world, which the player never sees: the "imaginary cuboid", which is made into the shape of whatever measured container's interior is being thought about.
A measured container is a kind of container. A measured container has a length called interior height. A measured container has a length called interior width. A measured container has a length called interior depth.
To imagine the interior of (receptacle - a measured container) as a cuboid:
now the height of the imaginary cuboid is the interior height of the receptacle;
now the width of the imaginary cuboid is the interior width of the receptacle;
now the depth of the imaginary cuboid is the interior depth of the receptacle.
To decide what volume is the interior volume of (receptacle - a measured container):
imagine the interior of the receptacle as a cuboid;
decide on the exterior volume of the imaginary cuboid.
If we assume that we could always pack items into a measured container with perfect ease, never wasting any space, then the only volume constraint will be that the total volume of the contents must not exceed the volume of the inside of the container. So we need to calculate the available volume.
To decide what volume is the available volume of (receptacle - a measured container):
let the remaining space be the interior volume of the receptacle;
repeat with item running through things in the receptacle:
decrease the remaining space by the exterior volume of the item;
if the remaining space is less than 0 cu cm, decide on 0 cu cm;
decide on the remaining space.
If we only constrained volume, a 140 cm-long fishing rod could fit into a 12 cm by 12 cm compact disc box. So we also insist the basic shape must fit, in some orientation perpendicular to one of the sides (i.e.: we can turn the item over in any of its three sides, but not turn it diagonally or wedge it in at a tilt). This requires the longest side of the item to be less than the longest side of the receptacle, and the middle-length side, and also the shortest side. The number of these conditions to fail gives us a clue as to how we can best describe the reason why the shape won't squeeze in.
Check inserting something (called the item) into a measured container (called the receptacle):
if the exterior volume of the item is greater than the interior volume of the receptacle, say "[The item] will never fit inside [the receptacle]." instead;
if the exterior volume of the item is greater than the available volume of the receptacle, say "[The item] will not fit into [the receptacle] with [the list of things in the receptacle]." instead;
imagine the interior of the receptacle as a cuboid;
if the largest dimension of the item is greater than the largest dimension of the imaginary cuboid, say "[The item] is too long to fit into [the receptacle]." instead;
if the middling dimension of the item is greater than the middling dimension of the imaginary cuboid, say "[The item] is too wide to fit into [the receptacle]." instead;
if the shortest dimension of the item is greater than the shortest dimension of the imaginary cuboid, say "[The item] is too bulky to fit into [the receptacle]." instead.
And finally a situation to try out these rules.
The Cubist Lab is a room. "A laboratory which, as the art critic Louis Vauxcelles said about Braque's paintings in 1908, is full of little cubes: everyday objects rendered as if cuboidal."
The box is a measured container. The interior height is 10 cm. The interior depth is 5 cm. The interior width is 6 cm. The player carries the box.
A pebble is a kind of thing. The height is usually 2 cm. The depth is usually 2 cm. The width is usually 2 cm. The player carries 25 pebbles.
A red rubber ball is carried by the player. The depth is 5 cm. The width is 5 cm. The height is 5 cm.
An arrow is carried by the player. The height is 40 cm. The width is 1 cm. The depth is 1 cm.
A crusty baguette is carried by the player. The height is 80 cm. The width is 4 cm. The depth is 5 cm.
A child's book is carried by the player. The height is 1 cm. The width is 9 cm. The depth is 9 cm.
A featureless white cube is carried by the player. The height is 6 cm. The width is 6 cm. The depth is 6 cm.
Test me with "put arrow in box / put book in box / put cube in box / put ball in box / put baguette in box / put pebbles in box".
Several warnings about this. First, the numbers can't go very high (if the Settings for the project set the story file format to the Z-machine): while the volume can in theory go to 32,767, in practice this equates to an object 32 cm on a side, which is not very large. One way to avoid this is to use the Glulx format, allowing for sizes in excess of 10 m on a side: or we could simply scale the dimensions to suit our purposes, using a decimeter (10 cm) as the basic unit of measurement, for instance.
Second, the system will require a height, width, and depth for every portable object in the game, which is a large commitment to data entry; it may become tiresome. So it is probably not worth bothering with this kind of simulation unless it is going to be genuinely significant.
ExampleFabrication |
When we make a new kind of value, the new named values can themselves have properties. That is convenient because, for instance, we might want to associate a material (itself the property of an object) with certain features, such as price.
A material is a kind of value. The materials are silk, velvet, cotton, and wool.
Cost is a kind of value.. $1.99 per sq yard specifies a cost. A cost times an area specifies a price.
The cost of silk is usually $5.75 per sq yard. The cost of velvet is usually $9.50 per sq yard. The cost of cotton is usually $2.29 per sq yard. The cost of wool is usually $4.75 per sq yard.
A pattern is a kind of thing. A pattern has a material. A pattern has an area. A pattern has a price. The price of a pattern is usually $9.99. Understand "pattern" as a pattern. Understand "patterns" as the plural of a pattern.
After printing the name of a pattern:
if planning:
do nothing;
otherwise:
say " pattern".
To decide what price is the material price of (chosen item - pattern):
let C be the cost of the material of the chosen item;
let A be the area of the chosen item;
decide on C * A.
To decide what price is the overall price of (chosen item - pattern):
let P be the price of the chosen item;
let M be the material price of the chosen item;
decide on P + M.
Planning it for is an action applying to one material and one thing.
Carry out planning it for:
now the material of the second noun is the material understood.
Report planning it for:
say "You lay plans for a [material understood] [second noun], running [material price of the second noun] for materials and [price of the second noun] for the pattern itself, for a total of [overall price of the second noun]."
Joanne's Fabrics is a room. Joanne's Fabrics contains a pattern bin.
The cape is a pattern. The material of the cape is velvet. The area of the cape is 9 sq yards.
The bodice is a pattern. The material of the bodice is silk. The area of the bodice is 2 sq yards. The price of the bodice is $11.99.
Test me with "plan silk bodice / plan velvet bodice / plan velvet cape / plan wool cape".
ExampleThe Speed of Thought |
Suppose that we have a number of objects in the game that are sized in some conventional unit (such as meters), but which we would like to describe in slightly less formal terms. To do this, we will start with measurements as defined in the built-in extension Metric Units, so we don't have to recreate all these.
We'll add our own set of "conceptual units" -- things we're familiar with in real life. As we'll see below, Inform will automatically choose a unit of the right order to express a given distance if we tell it to print a length "in conceptual units".
Note: the following will compile only if you have settings set for Glulx. (To change this, go to the Settings panel and click on the Glulx option.) The Glulx virtual machine is capable of handling larger numbers than the Z-machine.
1 quarter (in conceptual units, in quarters, singular) or 2 quarters (in conceptual units, in quarters, plural) specifies a length equivalent to 24mm.
1 pencil (in conceptual units, in pencils, singular) or 2 pencils (in conceptual units, in pencils, plural) specifies a length equivalent to 18cm.
1 bathtub (in conceptual units, in bathtubs, singular) or 2 bathtubs (in conceptual units, in bathtubs, plural) specifies a length equivalent to 152cm.
1 Olympic swimming pool (in conceptual units, in Olympic swimming pools, singular) or 2 Olympic swimming pools (in conceptual units, in Olympic swimming pools, plural) specifies a length equivalent to 50 meters.
1 Empire state building (in conceptual units, in Empire State buildings, singular) or 2 Empire State buildings (in conceptual units, in Empire State buildings, plural) specifies a length equivalent to 443m.
1 credit card (in conceptual units, in credit cards, singular) or 2 credit cards (in conceptual units, in credit cards, plural) specifies an area equivalent to 46 sq cm.
1 letter sheet (in conceptual units, in letter sheets, singular) or 2 letter sheets (in conceptual units, in letter sheets, plural) specifies an area equivalent to 603 sq cm.
1 queen-sized mattress (in conceptual units, in queen-sized mattresses, singular) or 2 queen-sized mattresses (in conceptual units, in queen-sized mattresses, plural) specifies an area equivalent to 3 square meters.
1 football field (in conceptual units, in football fields, singular) or 2 football fields (in conceptual units, in football fields, plural) specifies an area equivalent to 5351 square meters.
Understand "report [something]" as reporting. Reporting is an action applying to one thing.
Check reporting:
if the noun is not a fact:
say "The public doesn't want to hear about [the noun]." instead.
Report reporting:
if the extent of the noun is greater than 0mm and the surface of the noun is greater than 0 sq cm:
contextualize "'[The noun] has a length of [about] [extent of the noun in conceptual units] and an area of [about] [surface of the noun in conceptual units].'";
otherwise if the extent of the noun is greater than 0mm:
contextualize "'[The noun] has a length of [about] [extent of the noun in conceptual units].'";
otherwise if the surface of the noun is greater than 0 sq cm:
contextualize "'[The noun] has an area of [about] [surface of the noun in conceptual units].'";
otherwise:
say "'[The noun] is... pretty hard to imagine,' you say weakly. That's not going to go over well."
To say about:
say "[one of]roughly[or]about[or]around[or]approximately[at random]";
To contextualize (chosen information - text):
say "[one of]You turn to the camera and speak:[or][or]Turning to another camera angle, you add:[or][stopping] ";
say "[chosen information] ";
say "[one of][line break][or]Right now the station will be cutting over to a visual of that.[or][line break][or]Pity the kids in audiovisual who have to scare that image together in a hurry.[or]You smile brightly.[stopping]";
The Science Journalism Desk is a room. "From here you, the Science Anchor, have the privilege of reporting the latest and most fascinating stories to an eager public."
Instead of thinking:
say "Currently you have to report on the International Space Station. Your story could include [the list of facts carried by the player]."
Instead of taking inventory:
say "It looks foolish to be fiddling with your possessions on camera."
Instead of dropping a fact:
say "You decide to omit [the noun] from your lineup.";
now the noun is nowhere.
A fact is a kind of thing. Every fact is carried by the player. A fact has a length called the extent. A fact has an area called the surface.
The experiment module is a fact. The extent is 1116cm.
The logistics module is a fact. The extent is 421cm.
The solar array is a fact. The surface is 375 sq m. The extent is 58m.
An individual solar cell is a fact. The surface is 8 sq cm.
The orbit height is a fact.
Report reporting the orbit height:
contextualize "'The station orbits at heights between [about] [278km in conceptual units] and [460km in conceptual units] above the earth.'" instead.
Every turn:
if the player carries no facts:
say "And that's all! The channel cuts to weather.";
end the story saying "Time for lunch".
Test me with "report experiment module / report logistics / report height / report array / report solar cell".