mitsuba-renderer / drjit

Dr.Jit — A Just-In-Time-Compiler for Differentiable Rendering
BSD 3-Clause "New" or "Revised" License
593 stars 43 forks source link

Endpoint in dr.linspace #163

Closed WeiPhil closed 1 year ago

WeiPhil commented 1 year ago

Hi, I encountered a small issue with dr.linspace. When including the endpoint, the last element might not exactly be equal to stop due to floating point errors. In the numpy variant this is avoided by manually setting the last element to stop (https://github.com/numpy/numpy/blob/v1.25.0/numpy/core/function_base.py#L170-L171). Should there be a similar mechanic for the drjit variant?

wjakob commented 1 year ago

A manual assignment to an element (as is done in NumPy) would break the symbolic nature of the operation (and hence, megakernel compilation). A dr.select() statement could be used to conditionally change the last element but this would cause extra code to run on every SIMD lane, which seems kind of overkill for 99.9% of use cases.

Generally I would say that this is an acceptable accuracy issue for Dr.Jit, just like other arithmetic operations that are slightly less accurate than the C library counterparts. If it is really crucial that the endpoint has no rounding error, then this is also something you could retrofit in your code using a dr.select(dr.arange(UInt32, len(array)) == len(array)-1, .., ..) style expression.