10 @c -----------------------------------------------------------------------------
11 @node Numbers, Strings, Data Types and Structures, Data Types and Structures
13 @c -----------------------------------------------------------------------------
16 * Introduction to Numbers::
17 * Functions and Variables for Numbers::
20 @c -----------------------------------------------------------------------------
21 @node Introduction to Numbers, Functions and Variables for Numbers, Numbers, Numbers
22 @subsection Introduction to Numbers
23 @c -----------------------------------------------------------------------------
25 @c -----------------------------------------------------------------------------
26 @subheading Complex numbers
27 @c -----------------------------------------------------------------------------
29 A complex expression is specified in Maxima by adding the real part of the
30 expression to @code{%i} times the imaginary part. Thus the roots of the
31 equation @code{x^2 - 4*x + 13 = 0} are @code{2 + 3*%i} and @code{2 - 3*%i}.
32 Note that simplification of products of complex expressions can be effected by
33 expanding the product. Simplification of quotients, roots, and other functions
34 of complex expressions can usually be accomplished by using the @code{realpart},
35 @code{imagpart}, @code{rectform}, @code{polarform}, @code{abs}, @code{carg}
39 @category{Complex variables}
43 @c -----------------------------------------------------------------------------
44 @subheading Floating point numbers
45 @c -----------------------------------------------------------------------------
47 Maxima has two types of floating point number. The first is just
48 called a ``float'' (but will be called a ``machine float'' for the
49 rest of this section to avoid ambiguity). This is stored in the
50 underlying lisp's DOUBLE-FLOAT type which will almost certainly be
51 IEEE 754 double precision floating point. To type a literal floating
52 point number, just type its decimal expansion (for example,
53 @code{0.01}) or type it with an explicit exponent (such as @code{1e-2}
56 The second type of floating point number in Maxima is called a
57 ``bigfloat''. Bigfloats are stored as a mantissa and exponent in the
58 same way as machine floats but the exponent is an arbitrary precision
59 integer, so they can represent arbitrarily large or small numbers. The
60 user can also customise the precision of bigfloat arithmetic (which
61 corresponds to choosing the range of the mantissa). See @mref{fpprec}
62 for more information. To type a literal bigfloat, use the exponent
63 notation as above but with the character @code{b} in place of
64 @code{e}. The example of @code{0.01} from above could be entered as a
65 bigfloat with @code{1b-2} or @code{0.001b0}.
67 Calculations using machine floats can be significantly faster than
68 using bigfloats since modern computer processors have dedicated
69 hardware for them. This is particularly noticeable with compiled
70 Maxima code. However, machine floats suffer from the problem of
71 overflow, where a number can become too large for its exponent to be
72 represented in the bits available. In interpreted code, the default
73 behaviour is that a calculation that would cause a floating point
74 overflow instead generates a bigfloat number. To configure this, see
75 the @mref{promote_float_to_bigfloat} variable.
77 @c -----------------------------------------------------------------------------
78 @node Functions and Variables for Numbers, , Introduction to Numbers, Numbers
79 @subsection Functions and Variables for Numbers
80 @c -----------------------------------------------------------------------------
82 @c -----------------------------------------------------------------------------
84 @deffn {Function} bfloat (@var{expr})
86 Converts all numbers and functions of numbers in @var{expr} to bigfloat numbers.
87 The number of significant digits in the resulting bigfloats is specified by the
88 global variable @mrefdot{fpprec}
90 When @mref{float2bf} is @code{false} a warning message is printed when
91 a floating point number is converted into a bigfloat number (since
92 this may lead to loss of precision).
95 @category{Numerical evaluation}
99 @c -----------------------------------------------------------------------------
101 @deffn {Function} bfloatp (@var{expr})
103 Returns @code{true} if @var{expr} is a bigfloat number, otherwise @code{false}.
106 @category{Numerical evaluation} @category{Predicate functions}
110 @c --- 03.11.2011 --------------------------------------------------------------
112 @defvr {Option variable} bftorat
113 Default value: @code{false}
115 @code{bftorat} controls the conversion of bfloats to rational numbers. When
116 @code{bftorat} is @code{false}, @mref{ratepsilon} will be used to control the
117 conversion (this results in relatively small rational numbers). When
118 @code{bftorat} is @code{true}, the rational number generated will accurately
119 represent the bfloat.
121 Note: @code{bftorat} has no effect on the transformation to rational numbers
122 with the function @mrefdot{rationalize}
127 (%i1) ratepsilon:1e-4;
129 (%i2) rat(bfloat(11111/111111)), bftorat:false;
130 `rat' replaced 9.99990999991B-2 by 1/10 = 1.0B-1
134 (%i3) rat(bfloat(11111/111111)), bftorat:true;
135 `rat' replaced 9.99990999991B-2 by 11111/111111 = 9.99990999991B-2
142 @category{Numerical evaluation}
146 @c -----------------------------------------------------------------------------
148 @defvr {Option variable} bftrunc
149 Default value: @code{true}
151 @code{bftrunc} causes trailing zeroes in non-zero bigfloat numbers not to be
152 displayed. Thus, if @code{bftrunc} is @code{false}, @code{bfloat (1)}
153 displays as @code{1.000000000000000B0}. Otherwise, this is displayed as
157 @category{Numerical evaluation}
161 @c -----------------------------------------------------------------------------
163 @deffn {Function} evenp (@var{expr})
165 Returns @code{true} if @var{expr} is an even integer.
166 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
167 @code{false} is returned in all other cases.
170 @category{Predicate functions}
174 @c -----------------------------------------------------------------------------
176 @deffn {Function} float (@var{expr})
178 Converts integers, rational numbers and bigfloats in @var{expr} to floating
179 point numbers. It is also an @mrefcomma{evflag} @code{float} causes
180 non-integral rational numbers and bigfloat numbers to be converted to floating
184 @category{Numerical evaluation} @category{Evaluation flags}
188 @c --- 08.10.2010 DK -----------------------------------------------------------
190 @defvr {Option variable} float2bf
191 Default value: @code{true}
193 When @code{float2bf} is @code{false}, a warning message is printed when
194 a floating point number is converted into a bigfloat number (since
195 this may lead to loss of precision).
198 @category{Numerical evaluation}
202 @c -----------------------------------------------------------------------------
204 @deffn {Function} floatnump (@var{expr})
206 Returns @code{true} if @var{expr} is a floating point number, otherwise
210 @category{Numerical evaluation} @category{Predicate functions}
214 @c -----------------------------------------------------------------------------
216 @defvr {Option variable} fpprec
219 @code{fpprec} is the number of significant digits for arithmetic on bigfloat
220 numbers. @code{fpprec} does not affect computations on ordinary floating point
223 See also @mref{bfloat} and @mrefdot{fpprintprec}
226 @category{Numerical evaluation}
230 @c -----------------------------------------------------------------------------
232 @defvr {Option variable} fpprintprec
235 @code{fpprintprec} is the number of digits to print when printing an ordinary
236 float or bigfloat number.
238 For ordinary floating point numbers,
239 when @code{fpprintprec} has a value between 2 and 16 (inclusive),
240 the number of digits printed is equal to @code{fpprintprec}.
241 Otherwise, @code{fpprintprec} is 0, or greater than 16,
242 and the number of digits printed is 16.
244 For bigfloat numbers,
245 when @code{fpprintprec} has a value between 2 and @code{fpprec} (inclusive),
246 the number of digits printed is equal to @code{fpprintprec}.
247 Otherwise, @code{fpprintprec} is 0, or greater than @code{fpprec},
248 and the number of digits printed is equal to @code{fpprec}.
250 For both ordinary floats and bigfloats,
251 trailing zero digits are suppressed.
252 The actual number of digits printed is less than @code{fpprintprec}
253 if there are trailing zero digits.
255 @code{fpprintprec} cannot be 1.
258 @category{Numerical evaluation} @category{Display flags and variables}
262 @c -----------------------------------------------------------------------------
264 @deffn {Function} integerp (@var{expr})
266 Returns @code{true} if @var{expr} is a literal numeric integer, otherwise
269 @code{integerp} returns @code{false} if its argument is a symbol,
270 even if the argument is declared integer.
279 (%i3) integerp (-17);
281 (%i4) integerp (0.0);
283 (%i5) integerp (1.0);
285 (%i6) integerp (%pi);
289 (%i8) declare (n, integer);
296 @category{Predicate functions}
300 @c -----------------------------------------------------------------------------
302 @defvr {Option variable} m1pbranch
303 Default value: @code{false}
305 @code{m1pbranch} is the principal branch for @code{-1} to a power.
306 Quantities such as @code{(-1)^(1/3)} (that is, an "odd" rational exponent) and
307 @code{(-1)^(1/4)} (that is, an "even" rational exponent) are handled as follows:
309 @c REDRAW THIS AS A TABLE
314 (-1)^(1/4): (-1)^(1/4)
317 m1pbranch:false m1pbranch:true
318 (-1)^(1/3) 1/2+%i*sqrt(3)/2
319 (-1)^(1/4) sqrt(2)/2+%i*sqrt(2)/2
323 @category{Expressions} @category{Global flags}
327 @c -----------------------------------------------------------------------------
328 @deffn {Function} nonnegintegerp (@var{n})
330 Return @code{true} if and only if @code{@var{n} >= 0} and @var{n} is an integer.
333 @category{Package linearalgebra} @category{Predicate functions}
337 @c -----------------------------------------------------------------------------
339 @deffn {Function} numberp (@var{expr})
341 Returns @code{true} if @var{expr} is a literal integer, rational number,
342 floating point number, or bigfloat, otherwise @code{false}.
344 @code{numberp} returns @code{false} if its argument is a symbol, even if the
345 argument is a symbolic number such as @code{%pi} or @code{%i}, or declared to be
346 @code{even}, @code{odd}, @code{integer}, @code{rational}, @code{irrational},
347 @code{real}, @code{imaginary}, or @code{complex}.
354 (%i2) numberp (-13/19);
356 (%i3) numberp (3.14159);
358 (%i4) numberp (-1729b-4);
360 (%i5) map (numberp, [%e, %pi, %i, %phi, inf, minf]);
361 (%o5) [false, false, false, false, false, false]
362 (%i6) declare (a, even, b, odd, c, integer, d, rational,
363 e, irrational, f, real, g, imaginary, h, complex);
365 (%i7) map (numberp, [a, b, c, d, e, f, g, h]);
366 (%o7) [false, false, false, false, false, false, false, false]
370 @category{Predicate functions}
374 @c NEEDS CLARIFICATION, EXAMPLES
375 @c WHAT ARE THE FUNCTIONS WHICH ARE EVALUATED IN FLOATING POINT ??
376 @c WHAT IS A "NUMERVAL" ?? (SOMETHING DIFFERENT FROM A NUMERIC VALUE ??)
377 @c NEED TO MENTION THIS IS AN evflag
379 @c -----------------------------------------------------------------------------
381 @defvr {Option variable} numer
383 @code{numer} causes some mathematical functions (including exponentiation)
384 with numerical arguments to be evaluated in floating point. It causes
385 variables in @code{expr} which have been given numerals to be replaced by
386 their values. It also sets the @mref{float} switch on.
388 See also @mrefdot{%enumer}
393 @c [sqrt(2), sin(1), 1/(1+sqrt(3))];
394 @c [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
397 (%i1) [sqrt(2), sin(1), 1/(1+sqrt(3))];
399 (%o1) [sqrt(2), sin(1), -----------]
401 (%i2) [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
402 (%o2) [1.414213562373095, .8414709848078965, .3660254037844387]
406 @category{Numerical evaluation} @category{Evaluation flags}
410 @c -----------------------------------------------------------------------------
411 @anchor{numer_pbranch}
412 @defvr {Option variable} numer_pbranch
413 Default value: @code{false}
415 The option variable @code{numer_pbranch} controls the numerical evaluation of
416 the power of a negative integer, rational, or floating point number. When
417 @code{numer_pbranch} is @code{true} and the exponent is a floating point number
418 or the option variable @mref{numer} is @code{true} too, Maxima evaluates
419 the numerical result using the principal branch. Otherwise a simplified, but
420 not an evaluated result is returned.
426 @c (-2)^0.75,numer_pbranch:true;
429 @c (-2)^(3/4),numer,numer_pbranch:true;
435 (%i2) (-2)^0.75,numer_pbranch:true;
436 (%o2) 1.189207115002721*%i-1.189207115002721
439 (%o3) (-1)^(3/4)*2^(3/4)
441 (%i4) (-2)^(3/4),numer;
442 (%o4) 1.681792830507429*(-1)^0.75
444 (%i5) (-2)^(3/4),numer,numer_pbranch:true;
445 (%o5) 1.189207115002721*%i-1.189207115002721
449 @category{Numerical evaluation}
453 @c NEEDS CLARIFICATION, EXAMPLES
454 @c HOW TO FIND ALL VARIABLES WHICH HAVE NUMERVALS ??
456 @c -----------------------------------------------------------------------------
458 @deffn {Function} numerval (@var{x_1}, @var{expr_1}, @dots{}, @var{var_n}, @var{expr_n})
460 Declares the variables @code{x_1}, @dots{}, @var{x_n} to have
461 numeric values equal to @code{expr_1}, @dots{}, @code{expr_n}.
462 The numeric value is evaluated and substituted for the variable
463 in any expressions in which the variable occurs if the @code{numer} flag is
464 @code{true}. See also @mrefdot{ev}
466 The expressions @code{expr_1}, @dots{}, @code{expr_n} can be any expressions,
467 not necessarily numeric.
470 @category{Declarations and inferences} @category{Numerical evaluation}
474 @c -----------------------------------------------------------------------------
476 @deffn {Function} oddp (@var{expr})
478 is @code{true} if @var{expr} is an odd integer.
479 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
480 @code{false} is returned in all other cases.
483 @category{Predicate functions}
487 @c -----------------------------------------------------------------------------
488 @anchor{promote_float_to_bigfloat}
489 @defvr {Option variable} promote_float_to_bigfloat
490 Default value: @code{true}
492 When @code{promote_float_to_bigfloat} is true, the result of any
493 floating point calculation that would normally cause a floating point
494 overflow is replaced by a bigfloat number that represents the
495 result. Note that this automatic promotion only happens in interpreted
496 code: compiled code is not affected.
498 This automatic conversion is often convenient, but can be unhelpful in
499 some cases. For example, it can actually cause a loss of precision if
500 @mref{fpprec} is currently smaller than the precision in a floating
501 point number. To disable this behaviour, set
502 @code{promote_float_to_bigfloat} to false.
505 @category{Numerical evaluation}
509 @c --- 03.11.2011 --------------------------------------------------------------
511 @defvr {Option variable} ratepsilon
512 Default value: @code{2.0e-15}
514 @code{ratepsilon} is the tolerance used in the conversion
515 of floating point numbers to rational numbers, when the option variable
516 @mref{bftorat} has the value @code{false}. See @code{bftorat} for an example.
519 @category{Numerical evaluation} @category{Rational expressions}
523 @c -----------------------------------------------------------------------------
525 @deffn {Function} rationalize (@var{expr})
527 Convert all double floats and big floats in the Maxima expression @var{expr} to
528 their exact rational equivalents. If you are not familiar with the binary
529 representation of floating point numbers, you might be surprised that
530 @code{rationalize (0.1)} does not equal 1/10. This behavior isn't special to
531 Maxima -- the number 1/10 has a repeating, not a terminating, binary
535 @c rationalize (0.5);
536 @c rationalize (0.1);
538 @c rationalize (0.1b0);
540 @c rationalize (0.1b0);
541 @c rationalize (sin (0.1*x + 5.6));
544 (%i1) rationalize (0.5);
548 (%i2) rationalize (0.1);
553 (%i4) rationalize (0.1b0);
558 (%i6) rationalize (0.1b0);
559 236118324143482260685
560 (%o6) ----------------------
561 2361183241434822606848
562 (%i7) rationalize (sin (0.1*x + 5.6));
569 @category{Numerical evaluation}
573 @c -----------------------------------------------------------------------------
575 @deffn {Function} ratnump (@var{expr})
577 Returns @code{true} if @var{expr} is a literal integer or ratio of literal
578 integers, otherwise @code{false}.
581 @category{Predicate functions} @category{Rational expressions}
586 @c -----------------------------------------------------------------------------
588 @node Strings, Constants, Numbers, Data Types and Structures
590 @c -----------------------------------------------------------------------------
593 * Introduction to Strings::
594 * Functions and Variables for Strings::
597 @c -----------------------------------------------------------------------------
598 @node Introduction to Strings, Functions and Variables for Strings, Strings, Strings
599 @subsection Introduction to Strings
600 @c -----------------------------------------------------------------------------
609 Strings (quoted character sequences) are enclosed in double quote marks @code{"}
610 for input, and displayed with or without the quote marks, depending on the
611 global variable @mrefdot{stringdisp}
613 Strings may contain any characters, including embedded tab, newline, and
614 carriage return characters. The sequence @code{\"} is recognized as a literal
615 double quote, and @code{\\} as a literal backslash. When backslash appears at
616 the end of a line, the backslash and the line termination (either newline or
617 carriage return and newline) are ignored, so that the string continues with the
618 next line. No other special combinations of backslash with another character
619 are recognized; when backslash appears before any character other than @code{"},
620 @code{\}, or a line termination, the backslash is ignored. There is no way to
621 represent a special character (such as tab, newline, or carriage return)
622 except by embedding the literal character in the string.
624 There is no character type in Maxima; a single character is represented as a
625 one-character string.
627 The @code{stringproc} add-on package contains many functions for working with
633 @c s_1 : "This is a string.";
634 @c s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
635 @c s_3 : "Embedded line termination
637 @c s_4 : "Ignore the \
638 @c line termination \
641 @c stringdisp : false;
643 @c stringdisp : true;
647 (%i1) s_1 : "This is a string.";
648 (%o1) This is a string.
649 (%i2) s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
650 (%o2) Embedded "double quotes" and backslash \ characters.
651 (%i3) s_3 : "Embedded line termination
653 (%o3) Embedded line termination
655 (%i4) s_4 : "Ignore the \
659 (%o4) Ignore the line termination characters in this string.
660 (%i5) stringdisp : false;
663 (%o6) This is a string.
664 (%i7) stringdisp : true;
667 (%o8) "This is a string."
674 @c -----------------------------------------------------------------------------
675 @node Functions and Variables for Strings, , Introduction to Strings, Strings
676 @subsection Functions and Variables for Strings
677 @c -----------------------------------------------------------------------------
679 @c -----------------------------------------------------------------------------
681 @deffn {Function} concat (@var{arg_1}, @var{arg_2}, @dots{})
683 Concatenates its arguments. The arguments must evaluate to atoms. The return
684 value is a symbol if the first argument is a symbol and a string otherwise.
686 @code{concat} evaluates its arguments. The single quote @code{'} prevents
692 (%i3) concat (y, z/2);
694 (%i4) concat ('y, z/2);
698 A symbol constructed by @code{concat} may be assigned a value and appear in
699 expressions. The @mref{::} (double colon) assignment operator evaluates its
703 (%i5) a: concat ('y, z/2);
719 Note that although @code{concat (1, 2)} looks like a number, it is a string.
722 (%i10) concat (1, 2) + 3;
727 @category{Expressions} @category{Strings}
731 @c -----------------------------------------------------------------------------
733 @deffn {Function} sconcat (@var{arg_1}, @var{arg_2}, @dots{})
735 Concatenates its arguments into a string. Unlike @mrefcomma{concat} the
736 arguments do @i{not} need to be atoms.
739 (%i1) sconcat ("xx[", 3, "]:", expand ((x+y)^3));
740 (%o1) xx[3]:y^3+3*x*y^2+3*x^2*y+x^3
744 @category{Expressions} @category{Strings}
748 @c NEEDS CLARIFICATION AND EXAMPLES
750 @c -----------------------------------------------------------------------------
752 @deffn {Function} string (@var{expr})
754 Converts @code{expr} to Maxima's linear notation just as if it had been typed
757 The return value of @code{string} is a string, and thus it cannot be used in a
765 @c SHOULD BE WRITTEN WITH LEADING ? BUT THAT CONFUSES CL-INFO SO WORK AROUND
767 @c -----------------------------------------------------------------------------
769 @defvr {Option variable} stringdisp
770 Default value: @code{false}
772 When @code{stringdisp} is @code{true}, strings are displayed enclosed in double
773 quote marks. Otherwise, quote marks are not displayed.
775 @code{stringdisp} is always @code{true} when displaying a function definition.
780 @c stringdisp: false$
781 @c "This is an example string.";
783 @c print ("This is a string in a function definition.");
785 @c "This is an example string.";
788 (%i1) stringdisp: false$
789 (%i2) "This is an example string.";
790 (%o2) This is an example string.
793 print ("This is a string in a function definition.");
796 print("This is a string in a function definition.")
797 (%i4) stringdisp: true$
798 (%i5) "This is an example string.";
799 (%o5) "This is an example string."
803 @category{Display flags and variables}