liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Add support for iterating ranges with float boundaries.

Open simon-shopify opened this issue 8 years ago • 5 comments

The meaning I gave is to start at the lower bound and step by one. This has the advantage of having (n..m) and ((n.to_f)..(m.to_f)) behave the same. Another thing we could do that have this same property is to cast bounds to integer. This would do the right thing if there is a floating point rounding error, but I feel it is more surprising for cases such as (1.5..8).

Note that it is not possible to create ranges with floating point boundaries directly in Liquid at the moment. However, since there is no distinction between Ruby and Liquid ranges, a drop can introduce such a value by returning it.

simon-shopify avatar Aug 09 '17 16:08 simon-shopify

Why does it make any sense to allow this?

pushrax avatar Aug 09 '17 21:08 pushrax

Why does it make any sense to allow this?

Ah yes I think I was unclear on this point. What I'm doing is fixing an exception that happened in production. I had to choose between

  • giving a proper meaning to for with floats, or
  • gracefully failing.

I went for the first one for these reasons.

  • I think the behaviour is reasonable when you are iterating over natural numbers that happen to be stored in floats. It can happen if the number comes from Javascript? Or as a result of a division?
  • How do you explain to a non-programmer that why there is such distinction between integers and floats. How do you make it clear how to fix it?

simon-shopify avatar Aug 10 '17 14:08 simon-shopify

Hmm, division is perhaps the most compelling reason why this would happen, can we handle that more directly by rounding if the number is an epsilon away from an integer?

pushrax avatar Aug 11 '17 14:08 pushrax

Hmm, division is perhaps the most compelling reason why this would happen, can we handle that more directly by rounding if the number is an epsilon away from an integer?

An raise a proper LiquidError if it's not?

simon-shopify avatar Aug 11 '17 14:08 simon-shopify

Yeah, seems reasonable

pushrax avatar Aug 11 '17 15:08 pushrax