Next: , Previous: Complex Number Functions, Up: Arithmetic

7.4 Conversions

The commands described in this section convert numbers from one form to another; they are two-key sequences beginning with the letter c.

The c f (calc-float) [pfloat] command converts the number on the top of the stack to floating-point form. For example, ‘23’ is converted to ‘23.0’, ‘3:2’ is converted to ‘1.5’, and ‘2.3’ is left the same. If the value is a composite object such as a complex number or vector, each of the components is converted to floating-point. If the value is a formula, all numbers in the formula are converted to floating-point. Note that depending on the current floating-point precision, conversion to floating-point format may lose information.

As a special exception, integers which appear as powers or subscripts are not floated by c f. If you really want to float a power, you can use a j s command to select the power followed by c f. Because c f cannot examine the formula outside of the selection, it does not notice that the thing being floated is a power. See Selecting Subformulas.

The normal c f command is “pervasive” in the sense that it applies to all numbers throughout the formula. The pfloat algebraic function never stays around in a formula; ‘pfloat(a + 1)’ changes to ‘a + 1.0’ as soon as it is evaluated.

With the Hyperbolic flag, H c f [float] operates only on the number or vector of numbers at the top level of its argument. Thus, ‘float(1)’ is 1.0, but ‘float(a + 1)’ is left unevaluated because its argument is not a number.

You should use H c f if you wish to guarantee that the final value, once all the variables have been assigned, is a float; you would use c f if you wish to do the conversion on the numbers that appear right now.

The c F (calc-fraction) [pfrac] command converts a floating-point number into a fractional approximation. By default, it produces a fraction whose decimal representation is the same as the input number, to within the current precision. You can also give a numeric prefix argument to specify a tolerance, either directly, or, if the prefix argument is zero, by using the number on top of the stack as the tolerance. If the tolerance is a positive integer, the fraction is correct to within that many significant figures. If the tolerance is a non-positive integer, it specifies how many digits fewer than the current precision to use. If the tolerance is a floating-point number, the fraction is correct to within that absolute amount.

The pfrac function is pervasive, like pfloat. There is also a non-pervasive version, H c F [frac], which is analogous to H c f discussed above.

The c d (calc-to-degrees) [deg] command converts a number into degrees form. The value on the top of the stack may be an HMS form (interpreted as degrees-minutes-seconds), or a real number which will be interpreted in radians regardless of the current angular mode.

The c r (calc-to-radians) [rad] command converts an HMS form or angle in degrees into an angle in radians.

The c h (calc-to-hms) [hms] command converts a real number, interpreted according to the current angular mode, to an HMS form describing the same angle. In algebraic notation, the hms function also accepts three arguments: ‘hms(h, m, s)’. (The three-argument version is independent of the current angular mode.)

The calc-from-hms command converts the HMS form on the top of the stack into a real number according to the current angular mode.

The c p (calc-polar) command converts the complex number on the top of the stack from polar to rectangular form, or from rectangular to polar form, whichever is appropriate. Real numbers are left the same. This command is equivalent to the rect or polar functions in algebraic formulas, depending on the direction of conversion. (It uses polar, except that if the argument is already a polar complex number, it uses rect instead. The I c p command always uses rect.)

The c c (calc-clean) [pclean] command “cleans” the number on the top of the stack. Floating point numbers are re-rounded according to the current precision. Polar numbers whose angular components have strayed from the -180 to +180 degree range are normalized. (Note that results will be undesirable if the current angular mode is different from the one under which the number was produced!) Integers and fractions are generally unaffected by this operation. Vectors and formulas are cleaned by cleaning each component number (i.e., pervasively).

If the simplification mode is set below basic simplification, it is raised for the purposes of this command. Thus, c c applies the basic simplifications even if their automatic application is disabled. See Simplification Modes.

A numeric prefix argument to c c sets the floating-point precision to that value for the duration of the command. A positive prefix (of at least 3) sets the precision to the specified value; a negative or zero prefix decreases the precision by the specified amount.

The keystroke sequences c 0 through c 9 are equivalent to c c with the corresponding negative prefix argument. If roundoff errors have changed 2.0 into 1.999999, typing c 1 to clip off one decimal place often conveniently does the trick.

The c c command with a numeric prefix argument, and the c 0 through c 9 commands, also “clip” very small floating-point numbers to zero. If the exponent is less than or equal to the negative of the specified precision, the number is changed to 0.0. For example, if the current precision is 12, then c 2 changes the vector ‘[1e-8, 1e-9, 1e-10, 1e-11]’ to ‘[1e-8, 1e-9, 0, 0]’. Numbers this small generally arise from roundoff noise.

If the numbers you are using really are legitimately this small, you should avoid using the c 0 through c 9 commands. (The plain c c command rounds to the current precision but does not clip small numbers.)

One more property of c 0 through c 9, and of c c with a prefix argument, is that integer-valued floats are converted to plain integers, so that c 1 on ‘[1., 1.5, 2., 2.5, 3.]’ produces ‘[1, 1.5, 2, 2.5, 3]’. This is not done for huge numbers (‘1e100’ is technically an integer-valued float, but you wouldn't want it automatically converted to a 100-digit integer).

With the Hyperbolic flag, H c c and H c 0 through H c 9 operate non-pervasively [clean].