411 REFERENCE · Wikibooks
OpenSCAD User Manual/Ranges
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
part of the section on Values, Data Types, and Constants
Ranges
Ranges are a syntactic short form for specifying a sequence of numbers,
written in two forms:
[<start>:<end>] // increment defaults to 1
[<start>:<increment>:<end>]
the use of ':' as separators distinguishes these from vectors, which use commas
The increment in a range may be negative, but then start should be smaller than end else a warning will be generated that stops execution.
They may only be used by for() loops and by the children() module but may be created using expressions and returned from functions.
r1 = [0:10]; // literal range assigned to variable
echo(r1); // ECHO: [0: 1: 10]
Development snapshot A range with start greater than end and no increment generates a warning that the increment is positive and stops.
Versions after 2014: When start is less than end value, with a negative increment, no warning is generated but sets a null range, equivalent to [].
In version 2021.01 and earlier: A range with start greater than end generates a warning and is re-written as [<end>: 1: <start>].
In general the start, increment, and end values should be numerically "aligned" in that the end value should be achieved by
start + increment x (end-start)/increment == end
but this is not required.
Using an increment with a factional part ( as 1.2 ) can lead to unexpected results due to inexact arithmetic using 64-bit binary floating point.
This issue is completely avoided using integers for the increment, but fractions that are a multiples of 2, such as 0.25 (1/4) and 0.375 (3/8) are also safe.
start=0.5;incr=2.5;end = 10;
r2 = [start:incr:end];
for(a=r2)echo(a);
ECHO: 0.5
ECHO: 3
ECHO: 5.5
ECHO: 8 // never reaches end