From fd7365285acec01174e2134e1f34b81c30913a6e Mon Sep 17 00:00:00 2001 From: Roeland Schoukens Date: Tue, 13 Jan 2026 19:19:02 +1300 Subject: [PATCH] Make linearstep definitely return 1 for x > edge1 The division (xclamped - edge0) / (edge1 - edge0) is not guaranteed to be exactly 1 for xclamped == edge1 if the backend floating point math has higher than expected error bounds. Eg. it is common on GPU backends to use faster but less precise instructions for division. The modified code is mathematically equivalent and it logically guarantees the returned value is exactly 1.0 for x > edge1 > edge0. Signed-off-by: Roeland Schoukens --- src/shaders/stdosl.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/shaders/stdosl.h b/src/shaders/stdosl.h index ee86ae7be9..cc915084da 100644 --- a/src/shaders/stdosl.h +++ b/src/shaders/stdosl.h @@ -337,8 +337,7 @@ vector smoothstep (vector edge0, vector edge1, vector x) float linearstep (float edge0, float edge1, float x) { float result; if (edge0 != edge1) { - float xclamped = clamp (x, edge0, edge1); - result = (xclamped - edge0) / (edge1 - edge0); + result = clamp((x - edge0) / (edge1 - edge0), 0, 1); } else { // special case: edges coincide result = step (edge0, x); }