Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This article essentially describes a modulo (%) operator for floats, which is well-defined and completely analogous to the integer modulo operation. I always wondered why these aren't part of the FPU command set, or at least part of modern programming languages, e.g.:

   1.0 % 5.0 = 1.0
   6.0 % 5.0 = 1.0
   7.0 % 5.0 = 2.0
   7.4 % 5.0 = 2.4
   7.4 % 1.0 = 0.4
With that in place, the torodial distance becomes a one-liner without any case distinctions (i.e. without ifs).


> I always wondered why these aren't part of the FPU command set, or at least part of modern programming languages

They are a part of modern (and not-so-modern) programming languages (% in Python, Rust, etc.). Even C from quarter of a century ago supports them (fmod). There are also at least two CPU instructions on x86, fprem and fprem1.


I couldn't even find an integer modulus operation when I was porting my fractal code to WebGL. https://www.shadertoy.com/view/MdSXWc


Did the % operator not work? I just tested 1 % 10 and it seemed to compile. I used the % operator in other shaders. Remember that WebGL is ultimately a sub and superset of C.


It does work! I wonder why I didn't think of it while transliterating from my older Python codebase.


It's possible it used to not work with an older version of WebGL/GLSL, and now it does.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: