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}
38 @opencatbox{Categories:}
39 @category{Complex variables}
42 @c -----------------------------------------------------------------------------
43 @node Functions and Variables for Numbers, , Introduction to Numbers, Numbers
44 @subsection Functions and Variables for Numbers
45 @c -----------------------------------------------------------------------------
47 @c -----------------------------------------------------------------------------
49 @deffn {Function} bfloat (@var{expr})
51 @code{bfloat} replaces integers, rationals, floating point numbers, and some symbolic constants
52 in @var{expr} with bigfloat (variable-precision floating point) numbers.
54 The constants @code{%e}, @code{%gamma}, @code{%phi}, and @code{%pi}
55 are replaced by a numerical approximation.
56 However, @code{%e} in @code{%e^x} is not replaced by a numeric value
57 unless @code{bfloat(x)} is a number.
59 @code{bfloat} also causes numerical evaluation of some built-in functions,
60 namely trigonometric functions, exponential functions, @code{abs}, and @code{log}.
61 @c ALSO ENTIER BUT LET'S NOT GO INTO IT.
63 The number of significant digits in the resulting bigfloats is specified by the
64 global variable @mrefdot{fpprec}
65 Bigfloats already present in @var{expr} are replaced with values which have
66 precision specified by the current value of @mref{fpprec}.
68 When @mref{float2bf} is @code{false}, a warning message is printed when
69 a floating point number is replaced by a bigfloat number with less precision.
73 @code{bfloat} replaces integers, rationals, floating point numbers, and some symbolic constants
74 in @var{expr} with bigfloat numbers.
77 @c bfloat([123, 17/29, 1.75]);
78 @c bfloat([%e, %gamma, %phi, %pi]);
79 @c bfloat((f(123) + g(h(17/29)))/(x + %gamma));
82 (%i1) bfloat([123, 17/29, 1.75]);
83 (%o1) [1.23b2, 5.862068965517241b-1, 1.75b0]
84 (%i2) bfloat([%e, %gamma, %phi, %pi]);
85 (%o2) [2.718281828459045b0, 5.772156649015329b-1,
86 1.618033988749895b0, 3.141592653589793b0]
87 (%i3) bfloat((f(123) + g(h(17/29)))/(x + %gamma));
88 1.0b0 (g(h(5.862068965517241b-1)) + f(1.23b2))
89 (%o3) ----------------------------------------------
90 x + 5.772156649015329b-1
93 @code{bfloat} also causes numerical evaluation of some built-in functions.
96 @c bfloat(sin(17/29));
98 @c bfloat(abs(-%gamma));
102 (%i1) bfloat(sin(17/29));
103 (%o1) 5.532051841609784b-1
104 (%i2) bfloat(exp(%pi));
105 (%o2) 2.314069263277927b1
106 (%i3) bfloat(abs(-%gamma));
107 (%o3) 5.772156649015329b-1
108 (%i4) bfloat(log(%phi));
109 (%o4) 4.812118250596035b-1
112 @opencatbox{Categories:}
113 @category{Numerical evaluation}
117 @c -----------------------------------------------------------------------------
119 @deffn {Function} bfloatp (@var{expr})
121 Returns @code{true} if @var{expr} is a bigfloat number, otherwise @code{false}.
123 @opencatbox{Categories:}
124 @category{Numerical evaluation}
125 @category{Predicate functions}
129 @c --- 03.11.2011 --------------------------------------------------------------
131 @defvr {Option variable} bftorat
132 Default value: @code{false}
134 @code{bftorat} controls the conversion of bfloats to rational numbers. When
135 @code{bftorat} is @code{false}, @mref{ratepsilon} will be used to control the
136 conversion (this results in relatively small rational numbers). When
137 @code{bftorat} is @code{true}, the rational number generated will accurately
138 represent the bfloat.
140 Note: @code{bftorat} has no effect on the transformation to rational numbers
141 with the function @mrefdot{rationalize}
147 @c rat(bfloat(11111/111111)), bftorat:false;
148 @c rat(bfloat(11111/111111)), bftorat:true;
151 (%i1) ratepsilon:1e-4;
153 (%i2) rat(bfloat(11111/111111)), bftorat:false;
154 `rat' replaced 9.99990999991B-2 by 1/10 = 1.0B-1
158 (%i3) rat(bfloat(11111/111111)), bftorat:true;
159 `rat' replaced 9.99990999991B-2 by 11111/111111 = 9.99990999991B-2
165 @opencatbox{Categories:}
166 @category{Numerical evaluation}
170 @c -----------------------------------------------------------------------------
172 @defvr {Option variable} bftrunc
173 Default value: @code{true}
175 @code{bftrunc} causes trailing zeroes in non-zero bigfloat numbers not to be
176 displayed. Thus, if @code{bftrunc} is @code{false}, @code{bfloat (1)}
177 displays as @code{1.000000000000000B0}. Otherwise, this is displayed as
180 @opencatbox{Categories:}
181 @category{Numerical evaluation}
185 @c -----------------------------------------------------------------------------
186 @anchor{bigfloat_bits}
187 @deffn {Function} bigfloat_bits ()
188 Returns the number of bits of precision in a bigfloat number. This
189 value depends, of course, on the value of @mref{fpprec}.
200 (%i2) bigfloat_bits();
204 (%i4) bigfloat_bits();
208 @opencatbox{Categories:}
209 @category{Numerical evaluation}
213 @c -----------------------------------------------------------------------------
214 @anchor{bigfloat_eps}
215 @deffn {Function} bigfloat_eps ()
216 Returns the smallest bigfloat value, @code{eps}, such that
217 @code{1+eps} is not equal to 1. The value depends on @mref{fpprec},
229 (%i2) bigfloat_eps();
230 (%o2) 1.387778780781446b-17
233 (%i4) bigfloat_eps();
234 (%o4) 1.5407439555097886824447823540679b-33
236 @opencatbox{Categories:}
237 @category{Numerical evaluation}
241 @c -----------------------------------------------------------------------------
242 @anchor{decode_float}
243 @deffn {Function} decode_float (@var{f})
244 @code{decode_float} takes a float @var{f} and returns a list of three
245 values that characterizes @var{f}, which must be either a @code{float}
246 or @code{bfloat}. The first value has the same type as @var{f}, but
247 is a number in the range @code{[1, 2)}. The second value is an
248 exponent. The third value is a float of the same type as @var{f} and
249 has the value of 1 if @var{f} is greater than or equal to 0;
252 If the returned list is @code{[mantissa, expo, sign]}, then
253 @code{scale_float(mantissa, exp)*sign} is identical to @var{f}.
256 (%i1) decode_float(4e0);
258 (%i2) decode_float(4b0);
259 (%o2) [1.0b0, 2, 1.0b0]
260 (%i3) decode_float(%pi);
262 decode_float is only defined for floats and bfloats: %pi
263 -- an error. To debug this try: debugmode(true);
264 (%i4) decode_float(float(%pi));
265 (%o4) [1.570796326794897, 1, 1.0]
266 (%i5) decode_float(1.1e-5);
267 (%o5) [1.441792, - 17, 1.0]
272 This is a relatively simple interface to Common Lisp
273 @url{http://www.lispworks.com/documentation/HyperSpec/Body/f_dec_fl.htm,
274 decode_float}. However we return a signficand in the range
275 @code{[1,2)} instead of @code{[0.5, 1)}. The former matches
276 IEEE-754. Of course, this is extended to support bfloats.
278 @opencatbox{Categories:}
279 @category{Numerical evaluation}
283 @c -----------------------------------------------------------------------------
285 @deffn {Function} evenp (@var{expr})
287 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
288 Returns @code{true} if @var{expr} is a literal even integer, otherwise
291 @code{evenp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
292 is declared @code{even}.
294 @opencatbox{Categories:}
295 @category{Predicate functions}
299 @c -----------------------------------------------------------------------------
301 @deffn {Function} float (@var{expr})
303 Converts integers, rational numbers and bigfloats in @var{expr} to floating
304 point numbers. It is also an @mrefcomma{evflag} @code{float} causes
305 non-integral rational numbers and bigfloat numbers to be converted to floating
308 @opencatbox{Categories:}
309 @category{Numerical evaluation}
310 @category{Evaluation flags}
314 @c --- 08.10.2010 DK -----------------------------------------------------------
316 @defvr {Option variable} float2bf
317 Default value: @code{true}
319 When @mref{float2bf} is @code{false}, a warning message is printed when
320 a floating point number is replaced by a bigfloat number with less precision.
321 @c DOES THAT APPLY ONLY TO BFLOAT, OR DO OTHER FUNCTIONS CALL IT ??
323 @opencatbox{Categories:}
324 @category{Numerical evaluation}
328 @c -----------------------------------------------------------------------------
330 @deffn {Function} float_bits ()
331 Returns the number of bits of precision of a floating-point number.
333 @opencatbox{Categories:}
334 @category{Numerical evaluation}
338 @c -----------------------------------------------------------------------------
340 @deffn {Function} float_eps ()
341 Returns the smallest floating-point value, @code{eps}, such that
342 @code{1+eps} is not equal to 1.
344 @opencatbox{Categories:}
345 @category{Numerical evaluation}
349 @c -----------------------------------------------------------------------------
350 @anchor{float_precision}
351 @deffn {Function} float_precision (@var{f})
352 Returns the number of bits of precision of a floating-point number,
353 which can be either a float or bigfloat. This is basically the number
354 of bits used to represent the mantissa of a floating-point number.
355 For floats, this is 53 (for IEEE double-floats), but can be less when
356 denormal numbers occur. For bigfloats, this is equal to
357 @mref{fpprec}, when converted from digits to bits.
359 @opencatbox{Categories:}
360 @category{Numerical evaluation}
364 @c -----------------------------------------------------------------------------
366 @deffn {Function} float_sign (@var{f})
367 Returns the sign of @var{f}. It is @math{+1} or @math{-1} of the same
368 type as @var{f}. It is an error if @var{f} is not a float or
369 bigfloat. Note that some lisps do not support signed zeros for
370 floating-point numbers. Bigfloats do not support signed zeroes. The
371 examples below assume signed zeroes are supported.
373 (%i1) float_sign(1.0);
375 (%i2) float_sign(-5.0);
377 (%i3) float_sign(-0.0);
379 (%i4) float_sign(1b0);
381 (%i5) float_sign(-5b0);
383 (%o6) float_sign(-0b0);
385 (%i7) float_sign(%pi);
387 float_sign is only defined for floats and bfloats: %pi
388 -- an error. To debug this try: debugmode(true);
392 @c -----------------------------------------------------------------------------
393 @anchor{float_infinity_p}
394 @deffn {Function} float_infinity_p (@var{x})
396 Returns @code{true} if @var{x} is floating point positive infinity or floating point negative infinity,
397 and returns @code{false} for all other arguments;
398 arguments which are not numbers are allowed,
399 and @code{float_infinity_p} returns @code{false} for all such arguments.
401 Positive and negative floating point infinity may be distinguished by @code{sign},
402 which returns @code{pos} for positive infinity and @code{neg} for negative infinity.
404 @code{float_infinity_p} is defined whether or not the Lisp implementation supports float infinity.
405 When float infinity does not exist in the Lisp implementation's number system,
406 @code{float_infinity_p} returns @code{false} for all arguments.
408 A Lisp implementation may support more than one precision of floating point numbers.
409 @code{float_infinity_p} only recognizes double precision floating point infinity,
410 and not any other precision.
412 @opencatbox{Categories:}
413 @category{Numerical evaluation}
414 @category{Predicate functions}
419 @c -----------------------------------------------------------------------------
421 @deffn {Function} float_nan_p (@var{x})
423 Returns @code{true} if @var{x} is a floating point not-a-number (NaN) value,
424 and returns @code{false} for all other arguments;
425 arguments which are not numbers are allowed,
426 and @code{float_nan_p} returns @code{false} for all such arguments.
428 @code{float_nan_p} is defined whether or not the Lisp implementation supports floating point not-a-number values.
429 When floating point not-a-number does not exist in the Lisp implementation's number system,
430 @code{float_nan_p} returns @code{false} for all arguments.
432 A Lisp implementation may support more than one precision of floating point numbers.
433 @code{float_nan_p} only recognizes double precision floating point not-a-number,
434 and not any other precision.
436 @opencatbox{Categories:}
437 @category{Numerical evaluation}
438 @category{Predicate functions}
443 @c -----------------------------------------------------------------------------
445 @deffn {Function} floatnump (@var{expr})
447 Returns @code{true} if @var{expr} is a floating point number, otherwise
450 @opencatbox{Categories:}
451 @category{Numerical evaluation}
452 @category{Predicate functions}
456 @c -----------------------------------------------------------------------------
458 @defvr {Option variable} fpprec
461 @code{fpprec} is the number of significant digits for arithmetic on bigfloat
462 numbers. @code{fpprec} does not affect computations on ordinary floating point
465 See also @mref{bfloat} and @mrefdot{fpprintprec}
467 @opencatbox{Categories:}
468 @category{Numerical evaluation}
472 @c -----------------------------------------------------------------------------
474 @defvr {Option variable} fpprintprec
477 @code{fpprintprec} is the number of digits to print when printing an ordinary
478 float or bigfloat number.
480 For ordinary floating point numbers,
481 when @code{fpprintprec} has a value between 2 and 16 (inclusive),
482 the number of digits printed is equal to @code{fpprintprec}.
483 Otherwise, @code{fpprintprec} is 0, or greater than 16,
484 and the number is printed "readably":
485 that is, it is printed with sufficient digits to exactly reconstruct the number on input.
487 For bigfloat numbers,
488 when @code{fpprintprec} has a value between 2 and @code{fpprec} (inclusive),
489 the number of digits printed is equal to @code{fpprintprec}.
490 Otherwise, @code{fpprintprec} is 0, or greater than @code{fpprec},
491 and the number of digits printed is equal to @code{fpprec}.
493 For both ordinary floats and bigfloats,
494 trailing zero digits are suppressed.
495 The actual number of digits printed is less than @code{fpprintprec}
496 if there are trailing zero digits.
498 @code{fpprintprec} cannot be 1.
500 @opencatbox{Categories:}
501 @category{Numerical evaluation}
502 @category{Display flags and variables}
506 @c -----------------------------------------------------------------------------
508 @deffn {Function} integerp (@var{expr})
510 Returns @code{true} if @var{expr} is a literal numeric integer, otherwise
513 @code{integerp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
514 is declared @code{integer}.
526 @c declare (n, integer);
534 (%i3) integerp (-17);
536 (%i4) integerp (0.0);
538 (%i5) integerp (1.0);
540 (%i6) integerp (%pi);
544 (%i8) declare (n, integer);
550 @opencatbox{Categories:}
551 @category{Predicate functions}
555 @c -----------------------------------------------------------------------------
556 @anchor{integer_decode_float}
557 @deffn {Function} integer_decode_float (@var{f})
558 @code{integer_decode_float} takes a float @var{f} and returns a list of three
559 values that characterizes @var{f}, which must be either a @code{float}
560 or @code{bfloat}. The first value is an integer. The second value is an
561 exponent. The third value is 1 if @var{f} is positive or zero;
564 If the returned list is @code{[mantissa, expo, sign]}, then
565 @code{scale_float(fl(mantissa), expo)*sign} is identical to @var{f}.
566 Here, @code{fl} is either @code{float} or @code{bfloat} depending on
567 whether @var{f} is a @code{float} or a @code{bfloat}.
570 (%i1) integer_decode_float(4.0);
571 (%o1) [4503599627370496, - 50, 1]
572 (%i2) integer_decode_float(4b0);
573 (%o2) [36028797018963968, - 53, 1]
574 (%i3) scale_float(float(%o1[1]), %o1[2]);
576 (%i4) scale_float(bfloat(%o2[1]), %o2[2]);
578 (%i5) integer_decode_float(4);
580 decode_float is only defined for floats and bfloats: 4
581 -- an error. To debug this try: debugmode(true);
582 (%i6) integer_decode_float(1e-7);
583 (%o6) [7555786372591432, - 76, 1]
584 (%i7) integer_decode_float(1b-7);
585 (%o7) [60446290980731459, - 79, 1]
586 (%i8) scale_float(float(%o6[1]), %o6[2]);
590 For lisps that support denormal numbers, we have the following results.
592 (%i1) integer_decode_float(least_positive_float);
594 (%i2) integer_decode_float(100*least_positive_float);
595 (%o2) [100, - 1074, 1]
596 (%i3) integer_decode_float(least_positive_normalized_float);
597 (%o3) [4503599627370496, - 1074, 1]
599 The number of bits in the integer part decreases as the denormal
600 number decreases. Bfloat numbers do not have denormals because the
601 exponent is not bounded.
603 This is a relatively simple interface to Common Lisp
604 @url{http://www.lispworks.com/documentation/HyperSpec/Body/f_dec_fl.htm,
605 integer_decode_float}. However, the integer part can vary depending
606 on the Lisp implementation; we return the same value, independent of
607 the Lisp implementation. Of course, this is extended to support bfloats.
609 @opencatbox{Categories:}
610 @category{Numerical evaluation}
614 @c -----------------------------------------------------------------------------
615 @anchor{is_power_of_two}
616 @deffn {Function} is_power_of_two (@var{n})
617 @code{is_power_to_two} returns @code{true} if @var{n} is a power of
618 two and @code{false} otherwise. @var{n} may be an integer, a
619 rational, a float, or a big float.
623 (%i1) is_power_of_two(0);
625 (%i2) is_power_of_two(4);
627 (%i3) is_power_of_two(355/113);
629 (%i4) is_power_of_two(1/32);
631 (%i5) is_power_of_two(1048576);
633 (%i6) is_power_of_two(1048575);
635 (%i7) is_power_of_two(0.0);
637 (%i8) is_power_of_two(1048576.0);
639 (%i9) is_power_of_two(1048575.0);
641 (%i10) is_power_of_two(1/256.0);
643 (%i11) is_power_of_two(0b0);
645 (%i12) is_power_of_two(1048576b0);
647 (%i13) is_power_of_two(1048575b0);
649 (%i14) is_power_of_two(1/256b0);
653 @opencatbox{Categories:}
654 @category{Predicate functions}
658 @c -----------------------------------------------------------------------------
660 @defvr {Option variable} m1pbranch
661 Default value: @code{false}
663 @code{m1pbranch} is the principal branch for @code{-1} to a power.
664 Quantities such as @code{(-1)^(1/3)} (that is, an "odd" rational exponent) and
665 @code{(-1)^(1/4)} (that is, an "even" rational exponent) are handled as follows:
667 @c REDRAW THIS AS A TABLE
672 (-1)^(1/4): (-1)^(1/4)
675 m1pbranch:false m1pbranch:true
676 (-1)^(1/3) 1/2+%i*sqrt(3)/2
677 (-1)^(1/4) sqrt(2)/2+%i*sqrt(2)/2
680 @opencatbox{Categories:}
681 @category{Expressions}
682 @category{Global flags}
686 @c -----------------------------------------------------------------------------
687 @anchor{nonnegintegerp}
688 @deffn {Function} nonnegintegerp (@var{n})
690 Return @code{true} if and only if @code{@var{n} >= 0} and @var{n} is an integer.
692 @opencatbox{Categories:}
693 @category{Predicate functions}
697 @c -----------------------------------------------------------------------------
699 @deffn {Function} numberp (@var{expr})
701 Returns @code{true} if @var{expr} is a literal integer, rational number,
702 floating point number, or bigfloat, otherwise @code{false}.
704 @code{numberp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
705 is a symbolic number such as @code{%pi} or @code{%i}, or declared to be
706 @code{even}, @code{odd}, @code{integer}, @code{rational}, @code{irrational},
707 @code{real}, @code{imaginary}, or @code{complex}.
714 @c numberp (3.14159);
715 @c numberp (-1729b-4);
716 @c map (numberp, [%e, %pi, %i, %phi, inf, minf]);
717 @c declare (a, even, b, odd, c, integer, d, rational,
718 @c e, irrational, f, real, g, imaginary, h, complex);
719 @c map (numberp, [a, b, c, d, e, f, g, h]);
724 (%i2) numberp (-13/19);
726 (%i3) numberp (3.14159);
728 (%i4) numberp (-1729b-4);
730 (%i5) map (numberp, [%e, %pi, %i, %phi, inf, minf]);
731 (%o5) [false, false, false, false, false, false]
732 (%i6) declare (a, even, b, odd, c, integer, d, rational,
733 e, irrational, f, real, g, imaginary, h, complex);
735 (%i7) map (numberp, [a, b, c, d, e, f, g, h]);
736 (%o7) [false, false, false, false, false, false, false, false]
739 @opencatbox{Categories:}
740 @category{Predicate functions}
744 @c NEEDS CLARIFICATION, EXAMPLES
745 @c WHAT ARE THE FUNCTIONS WHICH ARE EVALUATED IN FLOATING POINT ??
746 @c WHAT IS A "NUMERVAL" ?? (SOMETHING DIFFERENT FROM A NUMERIC VALUE ??)
747 @c NEED TO MENTION THIS IS AN evflag
749 @c -----------------------------------------------------------------------------
751 @defvr {Option variable} numer
753 @code{numer} causes some mathematical functions (including exponentiation)
754 with numerical arguments to be evaluated in floating point. It causes
755 variables in @code{expr} which have been given numerals to be replaced by
756 their values. It also sets the @mref{float} switch on.
758 See also @mrefdot{%enumer}
763 @c [sqrt(2), sin(1), 1/(1+sqrt(3))];
764 @c [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
768 (%i1) [sqrt(2), sin(1), 1/(1+sqrt(3))];
770 (%o1) [sqrt(2), sin(1), -----------]
774 (%i2) [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
775 (%o2) [1.414213562373095, 0.8414709848078965, 0.3660254037844387]
779 @opencatbox{Categories:}
780 @category{Numerical evaluation}
781 @category{Evaluation flags}
785 @c -----------------------------------------------------------------------------
786 @anchor{numer_pbranch}
787 @defvr {Option variable} numer_pbranch
788 Default value: @code{false}
790 The option variable @code{numer_pbranch} controls the numerical evaluation of
791 the power of a negative integer, rational, or floating point number. When
792 @code{numer_pbranch} is @code{true} and the exponent is a floating point number
793 or the option variable @mref{numer} is @code{true} too, Maxima evaluates
794 the numerical result using the principal branch. Otherwise a simplified, but
795 not an evaluated result is returned.
801 @c (-2)^0.75,numer_pbranch:true;
804 @c (-2)^(3/4),numer,numer_pbranch:true;
813 (%i2) (-2)^0.75,numer_pbranch:true;
814 (%o2) 1.189207115002721 %i - 1.189207115002721
822 (%i4) (-2)^(3/4),numer;
824 (%o4) 1.681792830507429 (- 1)
827 (%i5) (-2)^(3/4),numer,numer_pbranch:true;
828 (%o5) 1.189207115002721 %i - 1.189207115002721
832 @opencatbox{Categories:}
833 @category{Numerical evaluation}
837 @c NEEDS CLARIFICATION, EXAMPLES
838 @c HOW TO FIND ALL VARIABLES WHICH HAVE NUMERVALS ??
840 @c -----------------------------------------------------------------------------
842 @deffn {Function} numerval (@var{x_1}, @var{expr_1}, @dots{}, @var{var_n}, @var{expr_n})
844 Declares the variables @code{x_1}, @dots{}, @var{x_n} to have
845 numeric values equal to @code{expr_1}, @dots{}, @code{expr_n}.
846 The numeric value is evaluated and substituted for the variable
847 in any expressions in which the variable occurs if the @code{numer} flag is
848 @code{true}. See also @mrefdot{ev}
850 The expressions @code{expr_1}, @dots{}, @code{expr_n} can be any expressions,
851 not necessarily numeric.
853 @opencatbox{Categories:}
854 @category{Declarations and inferences}
855 @category{Numerical evaluation}
859 @c -----------------------------------------------------------------------------
861 @deffn {Function} oddp (@var{expr})
863 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
864 Returns @code{true} if @var{expr} is a literal odd integer, otherwise
867 @code{oddp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
868 is declared @code{odd}.
870 @opencatbox{Categories:}
871 @category{Predicate functions}
875 @c --- 03.11.2011 --------------------------------------------------------------
877 @defvr {Option variable} ratepsilon
878 Default value: @code{2.0e-15}
880 @code{ratepsilon} is the tolerance used in the conversion
881 of floating point numbers to rational numbers, when the option variable
882 @mref{bftorat} has the value @code{false}. See @code{bftorat} for an example.
884 @opencatbox{Categories:}
885 @category{Numerical evaluation}
886 @category{Rational expressions}
890 @c -----------------------------------------------------------------------------
892 @deffn {Function} rationalize (@var{expr})
894 Convert all double floats and big floats in the Maxima expression @var{expr} to
895 their exact rational equivalents. If you are not familiar with the binary
896 representation of floating point numbers, you might be surprised that
897 @code{rationalize (0.1)} does not equal 1/10. This behavior isn't special to
898 Maxima -- the number 1/10 has a repeating, not a terminating, binary
902 @c rationalize (0.5);
903 @c rationalize (0.1);
905 @c rationalize (0.1b0);
907 @c rationalize (0.1b0);
908 @c rationalize (sin (0.1*x + 5.6));
912 (%i1) rationalize (0.5);
918 (%i2) rationalize (0.1);
920 (%o2) -----------------
925 (%i4) rationalize (0.1b0);
932 (%i6) rationalize (0.1b0);
933 236118324143482260685
934 (%o6) ----------------------
935 2361183241434822606848
938 (%i7) rationalize (sin (0.1*x + 5.6));
939 3602879701896397 x 3152519739159347
940 (%o7) sin(------------------ + ----------------)
941 36028797018963968 562949953421312
945 @opencatbox{Categories:}
946 @category{Numerical evaluation}
950 @c -----------------------------------------------------------------------------
952 @deffn {Function} ratnump (@var{expr})
954 Returns @code{true} if @var{expr} is a literal integer or ratio of literal
955 integers, otherwise @code{false}.
957 @opencatbox{Categories:}
958 @category{Predicate functions}
959 @category{Rational expressions}
963 @c -----------------------------------------------------------------------------
965 @deffn {Function} scale_float (@var{f}, @var{n})
966 @code{scale_float} scales the float @var{f} by the value
967 @code{2^@var{n}}. This is done carefully so that no round-off every
968 occurs. If @var{f} is a float, then it is possible to underflow to 0
969 or overflow, depending on the value of @var{f} and @var{n}. Bigfloats
970 cannot underflow or overflow.
973 (%i1) scale_float(2d0, 2);
975 (%i2) scale_float(2d0, -2);
977 (%i3) scale_float(-2d0, -10);
979 (%i4) scale_float(1d0, -2000);
981 (%i5) scale_float(2b0, 2);
983 (%i6) scale_float(1b0, -2000);
984 (%o6) 8.709809816217217b-603
985 (%i7) scale_float(1, 5);
987 scale_float: first arg must be a float or bfloat: 1
988 -- an error. To debug this try: debugmode(true);
989 (%i8) scale_float(1.0, n);
991 scale_float: second arg must be an integer: n
992 -- an error. To debug this try: debugmode(true);
995 This is a relatively simple interface to Common Lisp
996 @url{http://www.lispworks.com/documentation/HyperSpec/Body/f_dec_fl.htm,
997 scale_float}. Of course, this is extended to support bfloats.
1001 @c -----------------------------------------------------------------------------
1002 @anchor{unit_in_last_place}
1003 @deffn {Function} unit_in_last_place (@var{n})
1005 @code{unit_in_last_place} returns a value that is the gap between
1006 @var{n} and the nearest other number. See, for example,
1007 @url{https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT,
1008 Kahan@comma{} FOOTNOTE 1}. @code{unit_in_last_place} supports rational numbers,
1009 floating-point numbers and bigfloat numbers. For integer, the result
1010 is always 1, and for rational numbers the result is always 0.
1012 The examples below assume
1013 @url{https://en.wikipedia.org/wiki/IEEE_754,IEEE-754} arithmetic that
1015 @url{https://en.wikipedia.org/wiki/IEEE_754-1985#Denormalized_numbers,denormal}
1016 numbers. Some lisps like @url{https://clisp.sourceforge.io/, Clisp}
1017 do not have denormal numbers.
1020 (%i1) unit_in_last_place(0);
1022 (%i2) unit_in_last_place(-123);
1024 (%i3) unit_in_last_place(2/3);
1026 (%i4) unit_in_last_place(355/113);
1028 (%i5) unit_in_last_place(0b0);
1030 (%i6) unit_in_last_place(0.0);
1031 (%o6) 4.940656458412465e-324
1032 (%i7) unit_in_last_place(1.0);
1033 (%o7) 1.110223024625157e-16
1034 (%i8) unit_in_last_place(1b0);
1035 (%o8) 1.387778780781446b-17
1036 (%i9) unit_in_last_place(100.0);
1037 (%o9) 1.4210854715202e-14
1038 (%i10) unit_in_last_place(100b0);
1039 (%o10) 1.77635683940025b-15
1042 (%i12) unit_in_last_place(1b0);
1043 (%o12) 1.5407439555097886824447823540679b-33
1044 (%i13) unit_in_last_place(100b0);
1045 (%o13) 1.972152263052529513529321413207b-31
1048 @opencatbox{Categories:}
1049 @category{Numerical evaluation}
1054 @c -----------------------------------------------------------------------------
1056 @node Strings, Constants, Numbers, Data Types and Structures
1058 @c -----------------------------------------------------------------------------
1061 * Introduction to Strings::
1062 * Functions and Variables for Strings::
1065 @c -----------------------------------------------------------------------------
1066 @node Introduction to Strings, Functions and Variables for Strings, Strings, Strings
1067 @subsection Introduction to Strings
1068 @c -----------------------------------------------------------------------------
1071 @c The following three lines were commented out since they made "make pdf" abort
1077 @c adding the backslash to the index here breaks the LaTeX syntax of the file
1078 @c maxima.fns that is created by the first pdfLaTeX run by "make pdf".
1081 Strings (quoted character sequences) are enclosed in double quote marks @code{"}
1082 for input, and displayed with or without the quote marks, depending on the
1083 global variable @mrefdot{stringdisp}
1085 Strings may contain any characters, including embedded tab, newline, and
1086 carriage return characters. The sequence @code{\"} is recognized as a literal
1087 double quote, and @code{\\} as a literal backslash. When backslash appears at
1088 the end of a line, the backslash and the line termination (either newline or
1089 carriage return and newline) are ignored, so that the string continues with the
1090 next line. No other special combinations of backslash with another character
1091 are recognized; when backslash appears before any character other than @code{"},
1092 @code{\}, or a line termination, the backslash is ignored. There is no way to
1093 represent a special character (such as tab, newline, or carriage return)
1094 except by embedding the literal character in the string.
1096 There is no character type in Maxima; a single character is represented as a
1097 one-character string.
1099 The @code{stringproc} add-on package contains many functions for working with
1105 @c s_1 : "This is a string.";
1106 @c s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
1107 @c s_3 : "Embedded line termination
1108 @c in this string.";
1109 @c s_4 : "Ignore the \
1110 @c line termination \
1113 @c stringdisp : false;
1115 @c stringdisp : true;
1120 (%i1) s_1 : "This is a string.";
1121 (%o1) This is a string.
1124 (%i2) s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
1125 (%o2) Embedded "double quotes" and backslash \ characters.
1128 (%i3) s_3 : "Embedded line termination
1130 (%o3) Embedded line termination
1134 (%i4) s_4 : "Ignore the \
1138 (%o4) Ignore the line termination characters in this string.
1141 (%i5) stringdisp : false;
1146 (%o6) This is a string.
1149 (%i7) stringdisp : true;
1154 (%o8) "This is a string."
1158 @opencatbox{Categories:}
1162 @c -----------------------------------------------------------------------------
1163 @node Functions and Variables for Strings, , Introduction to Strings, Strings
1164 @subsection Functions and Variables for Strings
1165 @c -----------------------------------------------------------------------------
1167 @c -----------------------------------------------------------------------------
1169 @deffn {Function} concat (@var{arg_1}, @var{arg_2}, @dots{})
1171 Concatenates its arguments. The arguments must evaluate to atoms. The return
1172 value is a symbol if the first argument is a symbol and a string otherwise.
1174 @code{concat} evaluates its arguments. The single quote @code{'} prevents
1177 See also @mrefcomma{sconcat} that works on non-atoms, too, @mrefcomma{simplode}
1178 @mref{string} and @mrefdot{eval_string}
1179 For complex string conversions see also @mref{printf}.
1185 @c concat ('y, z/2);
1190 (%i3) concat (y, z/2);
1192 (%i4) concat ('y, z/2);
1196 A symbol constructed by @code{concat} may be assigned a value and appear in
1197 expressions. The @mref{::} (double colon) assignment operator evaluates its
1201 @c a: concat ('y, z/2);
1208 (%i5) a: concat ('y, z/2);
1222 Note that although @code{concat (1, 2)} looks like a number, it is a string.
1225 @c concat (1, 2) + 3;
1228 (%i10) concat (1, 2) + 3;
1232 @opencatbox{Categories:}
1233 @category{Expressions}
1238 @c -----------------------------------------------------------------------------
1240 @deffn {Function} sconcat (@var{arg_1}, @var{arg_2}, @dots{})
1242 Concatenates its arguments into a string. Unlike @mrefcomma{concat} the
1243 arguments do @i{not} need to be atoms.
1245 See also @mrefcomma{concat} @mrefcomma{simplode} @mref{string} and @mrefdot{eval_string}
1246 For complex string conversions see also @mref{printf}.
1249 @c sconcat ("xx[", 3, "]:", expand ((x+y)^3));
1253 (%i1) sconcat ("xx[", 3, "]:", expand ((x+y)^3));
1254 (%o1) xx[3]:y^3+3*x*y^2+3*x^2*y+x^3
1258 Another purpose for @code{sconcat} is to convert arbitrary objects to strings.
1274 @opencatbox{Categories:}
1275 @category{Expressions}
1280 @c NEEDS CLARIFICATION AND EXAMPLES
1282 @c -----------------------------------------------------------------------------
1284 @deffn {Function} string (@var{expr})
1286 Converts @code{expr} to Maxima's linear notation just as if it had been typed
1289 The return value of @code{string} is a string, and thus it cannot be used in a
1292 See also @mrefcomma{concat} @mrefcomma{sconcat} @mref{simplode} and
1293 @mrefdot{eval_string}
1295 @opencatbox{Categories:}
1300 @c SHOULD BE WRITTEN WITH LEADING ? BUT THAT CONFUSES CL-INFO SO WORK AROUND
1302 @c -----------------------------------------------------------------------------
1304 @defvr {Option variable} stringdisp
1305 Default value: @code{false}
1307 When @code{stringdisp} is @code{true}, strings are displayed enclosed in double
1308 quote marks. Otherwise, quote marks are not displayed.
1310 @code{stringdisp} is always @code{true} when displaying a function definition.
1315 @c stringdisp: false$
1316 @c "This is an example string.";
1318 @c print ("This is a string in a function definition.");
1319 @c stringdisp: true$
1320 @c "This is an example string.";
1323 (%i1) stringdisp: false$
1325 (%i2) "This is an example string.";
1326 (%o2) This is an example string.
1330 print ("This is a string in a function definition.");
1332 print("This is a string in a function definition.")
1334 (%i4) stringdisp: true$
1336 (%i5) "This is an example string.";
1337 (%o5) "This is an example string."
1341 @opencatbox{Categories:}
1342 @category{Display flags and variables}