www.delorie.com/gnu/docs/calc/calc_29.html | search |
Buy the book! | |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Calc understands a variety of data types as well as simple numbers. In this section, we'll experiment with each of these types in turn.
The numbers we've been using so far have mainly been either integers or floats. We saw that floats are usually a good approximation to the mathematical concept of real numbers, but they are only approximations and are susceptible to roundoff error. Calc also supports fractions, which can exactly represent any rational number.
1: 3628800 2: 3628800 1: 518400:7 1: 518414:7 1: 7:518414 . 1: 49 . . . . 10 ! 49 RET : 2 + & |
The : command divides two integers to get a fraction; / would normally divide integers to get a floating-point result. Notice we had to type RET between the 49 and the : since the : would otherwise be interpreted as part of a fraction beginning with 49.
You can convert between floating-point and fractional format using c f and c F:
1: 1.35027217629e-5 1: 7:518414 . . c f c F |
The c F command replaces a floating-point number with the "simplest" fraction whose floating-point representation is the same, to within the current precision.
1: 3.14159265359 1: 1146408:364913 1: 3.1416 1: 355:113 . . . . P c F DEL p 5 RET P c F |
(*) Exercise 1. A calculation has produced the result 1.26508260337. You suspect it is the square root of the product of pi and some rational number. Is it? (Be sure to allow for roundoff error!) See section 1. (*)
Complex numbers can be stored in both rectangular and polar form.
1: -9 1: (0, 3) 1: (3; 90.) 1: (6; 90.) 1: (2.4495; 45.) . . . . . 9 n Q c p 2 * Q |
The square root of -9 is by default rendered in rectangular form (0 + 3i), but we can convert it to polar form (3 with a phase angle of 90 degrees). All the usual arithmetic and scientific operations are defined on both types of complex numbers.
Another generalized kind of number is infinity. Infinity
isn't really a number, but it can sometimes be treated like one.
Calc uses the symbol inf
to represent positive infinity,
i.e., a value greater than any real number. Naturally, you can
also write `-inf' for minus infinity, a value less than any
real number. The word inf
can only be input using
algebraic entry.
2: inf 2: -inf 2: -inf 2: -inf 1: nan 1: -17 1: -inf 1: -inf 1: inf . . . . . ' inf RET 17 n * RET 72 + A + |
Since infinity is infinitely large, multiplying it by any finite
number (like -17) has no effect, except that since -17
is negative, it changes a plus infinity to a minus infinity.
("A huge positive number, multiplied by -17, yields a huge
negative number.") Adding any finite number to infinity also
leaves it unchanged. Taking an absolute value gives us plus
infinity again. Finally, we add this plus infinity to the minus
infinity we had earlier. If you work it out, you might expect
the answer to be -72 for this. But the 72 has been completely
lost next to the infinities; by the time we compute `inf - inf'
the finite difference between them, if any, is indetectable.
So we say the result is indeterminate, which Calc writes
with the symbol nan
(for Not A Number).
Dividing by zero is normally treated as an error, but you can get Calc to write an answer in terms of infinity by pressing m i to turn on "infinite mode."
3: nan 2: nan 2: nan 2: nan 1: nan 2: 1 1: 1 / 0 1: uinf 1: uinf . 1: 0 . . . . 1 RET 0 / m i U / 17 n * + |
Dividing by zero normally is left unevaluated, but after m i
it instead gives an infinite result. The answer is actually
uinf
, "undirected infinity." If you look at a graph of
1 / x around x = 0, you'll see that it goes toward
plus infinity as you approach zero from above, but toward minus
infinity as you approach from below. Since we said only 1 / 0,
Calc knows that the answer is infinite but not in which direction.
That's what uinf
means. Notice that multiplying uinf
by a negative number still leaves plain uinf
; there's no
point in saying `-uinf' because the sign of uinf
is
unknown anyway. Finally, we add uinf
to our nan
,
yielding nan
again. It's easy to see that, because
nan
means "totally unknown" while uinf
means
"unknown sign but known to be infinite," the more mysterious
nan
wins out when it is combined with uinf
, or, for
that matter, with anything else.
(*) Exercise 2. Predict what Calc will answer for each of these formulas: `inf / inf', `exp(inf)', `exp(-inf)', `sqrt(-inf)', `sqrt(uinf)', `abs(uinf)', `ln(0)'. See section 2. (*)
(*) Exercise 3. We saw that `inf - inf = nan',
which stands for an unknown value. Can nan
stand for
a complex number? Can it stand for infinity?
See section 3. (*)
HMS forms represent a value in terms of hours, minutes, and seconds.
1: 2@ 30' 0" 1: 3@ 30' 0" 2: 3@ 30' 0" 1: 2. . . 1: 1@ 45' 0." . . 2@ 30' RET 1 + RET 2 / / |
HMS forms can also be used to hold angles in degrees, minutes, and seconds.
1: 0.5 1: 26.56505 1: 26@ 33' 54.18" 1: 0.44721 . . . . 0.5 I T c h S |
First we convert the inverse tangent of 0.5 to degrees-minutes-seconds form, then we take the sine of that angle. Note that the trigonometric functions will accept HMS forms directly as input.
(*) Exercise 4. The Beatles' Abbey Road is 47 minutes and 26 seconds long, and contains 17 songs. What is the average length of a song on Abbey Road? If the Extended Disco Version of Abbey Road added 20 seconds to the length of each song, how long would the album be? See section 4. (*)
A date form represents a date, or a date and time. Dates must be entered using algebraic entry. Date forms are surrounded by `< >' symbols; most standard formats for dates are recognized.
2: <Sun Jan 13, 1991> 1: 2.25 1: <6:00pm Thu Jan 10, 1991> . . ' <13 Jan 1991>, <1/10/91, 6pm> RET - |
In this example, we enter two dates, then subtract to find the number of days between them. It is also possible to add an HMS form or a number (of days) to a date form to get another date form.
1: <4:45:59pm Mon Jan 14, 1991> 1: <2:50:59am Thu Jan 17, 1991> . . t N 2 + 10@ 5' + |
The t N ("now") command pushes the current date and time on the stack; then we add two days, ten hours and five minutes to the date and time. Other date-and-time related commands include t J, which does Julian day conversions, t W, which finds the beginning of the week in which a date form lies, and t I, which increments a date by one or several months. See section 8.5 Date Arithmetic, for more.
(*) Exercise 5. How many days until the next Friday the 13th? See section 5. (*)
(*) Exercise 6. How many leap years will there be between now and the year 10001 A.D.? See section 6. (*)
An error form represents a mean value with an attached standard deviation, or error estimate. Suppose our measurements indicate that a certain telephone pole is about 30 meters away, with an estimated error of 1 meter, and 8 meters tall, with an estimated error of 0.2 meters. What is the slope of a line from here to the top of the pole, and what is the equivalent angle in degrees?
1: 8 +/- 0.2 2: 8 +/- 0.2 1: 0.266 +/- 0.011 1: 14.93 +/- 0.594 . 1: 30 +/- 1 . . . 8 p .2 RET 30 p 1 / I T |
This means that the angle is about 15 degrees, and, assuming our original error estimates were valid standard deviations, there is about a 60% chance that the result is correct within 0.59 degrees.
(*) Exercise 7. The volume of a torus (a donut shape) is 2 pi^2 R r^2 where R is the radius of the circle that defines the center of the tube and r is the radius of the tube itself. Suppose R is 20 cm and r is 4 cm, each known to within 5 percent. What is the volume and the relative uncertainty of the volume? See section 7. (*)
An interval form represents a range of values. While an error form is best for making statistical estimates, intervals give you exact bounds on an answer. Suppose we additionally know that our telephone pole is definitely between 28 and 31 meters away, and that it is between 7.7 and 8.1 meters tall.
1: [7.7 .. 8.1] 2: [7.7 .. 8.1] 1: [0.24 .. 0.28] 1: [13.9 .. 16.1] . 1: [28 .. 31] . . . [ 7.7 .. 8.1 ] [ 28 .. 31 ] / I T |
If our bounds were correct, then the angle to the top of the pole is sure to lie in the range shown.
The square brackets around these intervals indicate that the endpoints themselves are allowable values. In other words, the distance to the telephone pole is between 28 and 31, inclusive. You can also make an interval that is exclusive of its endpoints by writing parentheses instead of square brackets. You can even make an interval which is inclusive ("closed") on one end and exclusive ("open") on the other.
1: [1 .. 10) 1: (0.1 .. 1] 2: (0.1 .. 1] 1: (0.2 .. 3) . . 1: [2 .. 3) . . [ 1 .. 10 ) & [ 2 .. 3 ) * |
The Calculator automatically keeps track of which end values should be open and which should be closed. You can also make infinite or semi-infinite intervals by using `-inf' or `inf' for one or both endpoints.
(*) Exercise 8. What answer would you expect from `1 / (0 .. 10)'? What about `1 / (-10 .. 0)'? What about `1 / [0 .. 10]' (where the interval actually includes zero)? What about `1 / (-10 .. 10)'? See section 8. (*)
(*) Exercise 9. Two easy ways of squaring a number are RET * and 2 ^. Normally these produce the same answer. Would you expect this still to hold true for interval forms? If not, which of these will result in a larger interval? See section 9. (*)
A modulo form is used for performing arithmetic modulo M. For example, arithmetic involving time is generally done modulo 12 or 24 hours.
1: 17 mod 24 1: 3 mod 24 1: 21 mod 24 1: 9 mod 24 . . . . 17 M 24 RET 10 + n 5 / |
In this last step, Calc has found a new number which, when multiplied by 5 modulo 24, produces the original number, 21. If M is prime it is always possible to find such a number. For non-prime M like 24, it is only sometimes possible.
1: 10 mod 24 1: 16 mod 24 1: 1000000... 1: 16 . . . . 10 M 24 RET 100 ^ 10 RET 100 ^ 24 % |
These two calculations get the same answer, but the first one is much more efficient because it avoids the huge intermediate value that arises in the second one.
(*) Exercise 10. A theorem of Pierre de Fermat says that x^(n-1) mod n = 1 if n is a prime number and x is an integer less than n. If n is not a prime number, this will not be true for most values of x. Thus we can test informally if a number is prime by trying this formula for several values of x. Use this test to tell whether the following numbers are prime: 811749613, 15485863. See section 10. (*)
It is possible to use HMS forms as parts of error forms, intervals,
modulo forms, or as the phase part of a polar complex number.
For example, the calc-time
command pushes the current time
of day on the stack as an HMS/modulo form.
1: 17@ 34' 45" mod 24@ 0' 0" 1: 6@ 22' 15" mod 24@ 0' 0" . . x time RET n |
This calculation tells me it is six hours and 22 minutes until midnight.
(*) Exercise 11. A rule of thumb is that one year is about pi * 10^7 seconds. What time will it be that many seconds from right now? See section 11. (*)
(*) Exercise 12. You are preparing to order packaging for the CD release of the Extended Disco Version of Abbey Road. You are told that the songs will actually be anywhere from 20 to 60 seconds longer than the originals. One CD can hold about 75 minutes of music. Should you order single or double packages? See section 12. (*)
Another kind of data the Calculator can manipulate is numbers with units. This isn't strictly a new data type; it's simply an application of algebraic expressions, where we use variables with suggestive names like `cm' and `in' to represent units like centimeters and inches.
1: 2 in 1: 5.08 cm 1: 0.027778 fath 1: 0.0508 m . . . . ' 2in RET u c cm RET u c fath RET u b |
We enter the quantity "2 inches" (actually an algebraic expression which means two times the variable `in'), then we convert it first to centimeters, then to fathoms, then finally to "base" units, which in this case means meters.
1: 9 acre 1: 3 sqrt(acre) 1: 190.84 m 1: 190.84 m + 30 cm . . . . ' 9 acre RET Q u s ' $+30 cm RET |
1: 191.14 m 1: 36536.3046 m^2 1: 365363046 cm^2 . . . u s 2 ^ u c cgs |
Since units expressions are really just formulas, taking the square
root of `acre' is undefined. After all, acre
might be an
algebraic variable that you will someday assign a value. We use the
"units-simplify" command to simplify the expression with variables
being interpreted as unit names.
In the final step, we have converted not to a particular unit, but to a units system. The "cgs" system uses centimeters instead of meters as its standard unit of length.
There is a wide variety of units defined in the Calculator.
1: 55 mph 1: 88.5139 kph 1: 88.5139 km / hr 1: 8.201407e-8 c . . . . ' 55 mph RET u c kph RET u c km/hr RET u c c RET |
We express a speed first in miles per hour, then in kilometers per hour, then again using a slightly more explicit notation, then finally in terms of fractions of the speed of light.
Temperature conversions are a bit more tricky. There are two ways to interpret "20 degrees Fahrenheit"---it could mean an actual temperature, or it could mean a change in temperature. For normal units there is no difference, but temperature units have an offset as well as a scale factor and so there must be two explicit commands for them.
1: 20 degF 1: 11.1111 degC 1: -20:3 degC 1: -6.666 degC . . . . ' 20 degF RET u c degC RET U u t degC RET c f |
First we convert a change of 20 degrees Fahrenheit into an equivalent change in degrees Celsius (or Centigrade). Then, we convert the absolute temperature 20 degrees Fahrenheit into Celsius. Since this comes out as an exact fraction, we then convert to floating-point for easier comparison with the other result.
For simple unit conversions, you can put a plain number on the stack. Then u c and u t will prompt for both old and new units. When you use this method, you're responsible for remembering which numbers are in which units:
1: 55 1: 88.5139 1: 8.201407e-8 . . . 55 u c mph RET kph RET u c km/hr RET c RET |
To see a complete list of built-in units, type u v. Press M-# c again to re-enter the Calculator when you're done looking at the units table.
(*) Exercise 13. How many seconds are there really in a year? See section 13. (*)
(*) Exercise 14. Supercomputer designs are limited by the speed of light (and of electricity, which is nearly as fast). Suppose a computer has a 4.1 ns (nanosecond) clock cycle, and its cabinet is one meter across. Is speed of light going to be a significant factor in its design? See section 14. (*)
(*) Exercise 15. Sam the Slug normally travels about five yards in an hour. He has obtained a supply of Power Pills; each Power Pill he eats doubles his speed. How many Power Pills can he swallow and still travel legally on most US highways? See section 15. (*)
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
webmaster donations bookstore | delorie software privacy |
Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |