changeset 32324:09743854e8b7 stable

doc: Add range example and some copyedits
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 22 Sep 2023 20:28:10 -0400
parents 035f87a5ce45
children 9d758cacf7ca f760ab971d40
files doc/interpreter/numbers.txi
diffstat 1 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/numbers.txi	Fri Sep 22 20:11:48 2023 -0400
+++ b/doc/interpreter/numbers.txi	Fri Sep 22 20:28:10 2023 -0400
@@ -431,10 +431,21 @@
 @DOCSTRING(optimize_range)
 
 Note that the upper (or lower, if the increment is negative) bound on
-the range is not always included in the set of values, and that ranges
-defined by floating point values can produce surprising results because
+the range is not always included in the set of values.  This can be useful
+in some contexts.  For example:
+
+@example
+## x is some predefined range or vector or matrix or array
+x(1:2:end) += 1;   # increment all  odd-numbered elements
+x(2:2:end) -= 1;   # decrement all even-numbered elements
+@end example
+
+The above code works correctly whether @var{x} has an odd number of elements
+or not, so no need to treat the two cases differently.
+
 Octave uses floating point arithmetic to compute the values in the
-range.  Here are some pitfalls that can happen:
+range.  As a result, defining ranges with floating-point values can result
+in pitfalls like these:
 
 @example
 a = -2
@@ -468,8 +479,8 @@
 makes it much safer and much more repeatable across platforms, compilers,
 and compiler settings.  If you know the number of elements, you can also use
 the @code{linspace} function (@pxref{Special Utility Matrices}), which will
-include the endpoints of a range.  If you do not, you can also make judicious
-use of @code{round}, @code{floor}, @code{ceil}, @code{fix}, etc to set the
+include the endpoints of a range.  You can also make judicious use of
+@code{round}, @code{floor}, @code{ceil}, @code{fix}, etc to set the
 limits and the increment without getting interference from floating-point
 rounding.  For example, the earlier example can be made safer and much more
 repeatable with one of the following: