3 ms·
I do a lot of FFTs for a living - I really want my zeroth frequency in my zeroth bin. Negative indices (as done by Python and other places) are nice though. T
by civility 8y ago
I do a lot of FFTs for a living - I really want my zeroth frequency in my zeroth bin. Negative indices (as done by Python and other places) are nice though.
This points to another example where 0-based should be preferred. When doing modulo arithmetic, 0..N-1 mod N gives 0..N-1, but 1..N mod N puts the zero at the end. I also cringe at languages where -1 mod N does not equal N-1.
- jacobolus 8y agoYes, people should never use “mod” or % as a synonym for “remainder”. It is horrible. For any language with a mod operator, a mod b should always be equal to (a + k×b) mod b for any integer k. Breaking this invariant makes the mod operator useless in pretty much every application I ever have for it. In e.g. JavaScript I need to define a silly helper function like mod = (x,y) => x%y + y*(x%y && x>0^y>0) And then remember to never use the native % operator.
- simonbyrne 8y agoWhat language defines % as mod and not remainder?
- jacobolus 8y agoAccording to Wikipedia: Perl, Python, Lua, Ruby, Tcl, R (as %%), Smalltalk (as \\), various other languages under some alternate name, sometimes with the two variants given different names. The other (“remainder”) version which takes its sign from the first argument is pretty much worthless in practice. IMO it doesn’t need any name at all. But what it definitely doesn’t need is a shorter and more convenient name than the useful modulo operator. Its ubiquity is a serious screwup in programming language design, albeit largely accidental.
- simonbyrne 8y agoI guess because remainder is simpler to implement? (anecdotally, the assembly generated by @code_native in Julia is much shorter for remainder) In my opinion neither are used sufficiently often to justify taking up a valuable ASCII symbol (same with xor).
- jacobolus 8y agoThis depends on what the behavior is of the division operator. People should use floor division, then implementing modulo is easy. I don’t know what is available as chip instructions, I could certainly believe hardware designers made the wrong choice.
- simonbyrne 8y agoI take the opinion that people should have the option to round division (and compute remainders) however they want: down, up, to zero, to nearest (with different options for breaking ties). https://github.com/JuliaLang/julia/issues/9283 https://github.com/JuliaLang/julia/issues/9283
- ScottBurson 8y agoCommon Lisp has all four division operators ('floor', 'ceiling', 'truncate' (equivalent to what most languages consider division), and 'round') [0], and both 'mod' and 'rem' [1]. [0] http://www.lispworks.com/documentation/lw50/CLHS/Body/f_floorc.htm http://www.lispworks.com/documentation/lw50/CLHS/Body/f_floo... [1] http://www.lispworks.com/documentation/lw50/CLHS/Body/f_mod_r.htm http://www.lispworks.com/documentation/lw50/CLHS/Body/f_mod_...
- simonbyrne 8y agoThe other point is that remainder makes much more sense for floating point numbers, as it's exact. mod on the other hand is not, and is fiendishly difficult to get "right" (if that is even possible).
- jacobolus 8y agoIn what context do you use it? I use a “mod” operator all the time for floating point calculations, and have never come across a need for the other one. As one simple example, it is frequently useful to map floats into the range [0, 2π), but I have never once wanted to map positive floats into the range [0, 2π) while negative floats get mapped into (–2π, 0] by the same code.
- simonbyrne 8y ago> I really want my zeroth frequency in my zeroth bin That's precisely what it does! > I also cringe at languages where -1 mod N does not equal N-1. julia> mod(-1,10) 9
- jacobolus 8y agoA 1-character infix operator is much cleaner to read than a 3-character name of a 2-parameter function. But admittedly when you use 1-indexed arrays, any kind of modulo operator becomes pretty inconvenient a lot of the time (lots of futzing to avoid off-by-1 or boundary errors). So maybe it doesn’t matter in the Julia context.