Morpho-lang / morpho

The Morpho language 🦋. Morpho is a small embeddable language for scientific computing applications.
MIT License
33 stars 11 forks source link

[Bug] Exclusive ranges might skip last element #114

Open mattsep opened 2 years ago

mattsep commented 2 years ago

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.

ConduitDan commented 2 years ago

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

ConduitDan commented 2 years ago

Updated the example

softmattertheory commented 2 years ago

Is this now fixed?

mattsep commented 2 years ago

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.