morpho icon indicating copy to clipboard operation
morpho copied to clipboard

[Bug] Exclusive ranges might skip last element

Open mattsep opened this issue 2 years ago • 4 comments

There is a bug in the parsing of exclusive ranges that is showcased by this example:

print List(1.0 ... 1.2 : 0.15) // prints [ 1 ], but should print [ 1, 1.15 ]

This happens because the exclusive range 1.0 ... 1.2 : 0.15 is converted to 1.0 .. 1.05 : 0.15 by subtracting the step from the end, which is not the right thing to do in this situation. Since this happens at parse time, it's not possible to revert this calculation in the range_count function.

If we were to instead convert all inclusive ranges to exclusive ranges by adding the step to the end, we would find that 1.0 .. 1.2 : 0.15 would be converted to 1.0 ... 1.35 : 0.15 which would print [ 1, 1.15, 1.3 ] and this is also not correct.

What we likely need is to explicitly flag whether a given range is inclusive or exclusive range at construction so that we can compute the correct number of elements in the range_count function, at which point we can convert both ranges to one or the other.

mattsep avatar Mar 07 '22 23:03 mattsep

Did this get fixed? I cannot reproduce this on the main branch

ConduitDan avatar Mar 29 '22 14:03 ConduitDan

Updated the example

ConduitDan avatar Apr 04 '22 14:04 ConduitDan

Is this now fixed?

softmattertheory avatar Apr 20 '22 17:04 softmattertheory

Not yet - this issue arises due to how exclusive ranges are converted to inclusive ranges at parse time. I think inclusive and exclusive ranges need to be parsed separately, and only converted to one or the other at a later time, once we have full knowledge of the bounds and step size.

mattsep avatar May 08 '22 20:05 mattsep