Skip to content

trunc(ate), div(ide): document behavior and implement "fixed" versions.#7387

Draft
HotwheelsSisyphus wants to merge 1 commit into
supercollider:developfrom
HotwheelsSisyphus:topic/trunc(ate)-div(ide)
Draft

trunc(ate), div(ide): document behavior and implement "fixed" versions.#7387
HotwheelsSisyphus wants to merge 1 commit into
supercollider:developfrom
HotwheelsSisyphus:topic/trunc(ate)-div(ide)

Conversation

@HotwheelsSisyphus

@HotwheelsSisyphus HotwheelsSisyphus commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Thought it would be better to reopen this on another branch. Basically waiting for instructions now.

The story so far

As discussed in that other issue (#2378).

div actually has a similar problem, but we did not discuss it explicitly.
(i.e., a.div(b) == floor(a/b) but is normally equivalent to std::trunc(a/b)).

We had a little "vote" in #2378 (comment), and there are now 2 votes for truncate and 0 for trunc2 as a name for the "standard" method, though I recall that this was tied 2-2 as some point.
(That quorum is, uh, suboptimal, but what can ya do.)

I think I've now figured out how to do this but I do need some direction/decision by more experienced folks as to what exactly to do.

We've agreed on non-breaking changes, i.e., only adding things rather than changing interfaces (i.e., not simply "fixing" the existing trunc). So at any rate, the resulting solution should not break code written in sclang. So that doesn't need to be discussed. The question is really about the Server side only.

There are two main options:

  1. Simply add new, "fixed" UGens (truncate and divide)
  2. Replace the respective operations with corrected ones, and reimplement the backward-compatible trunc on the class library side.

1 of course is simplest. It just seems a bit inelegant and adds more code. Sort of a shantytown approach.
2 replaces some code in the server and adds very little to the class library.

For 2, there would be 2 options:

  • a) Internally rename the operations in question (i.e., opTUGenrunc becomes opTruncate), such that a .truncate method would call a _Truncate primitive).
  • b) don't rename them, only correct the math. I think this could become confusing.

Within 1, two more questions:

  • a) For truncate, the question is:
    • Should it be a unary or a binary op?
      • if it is to be strictly analogous to sc's trunc , then it would a binary op, with two extra multiplications. (Sc's trunc is currently std::floor(a/b) * b; a binary op truncate would be std::trunc(a/b) *b. )
      • if it is to behave like c-style trunc, then it would be unary.
  • b) Should the extra code for that UGen be in a separate file, or at the end of the respective naryOpUGens file?

I think that's roughly it.

also adds methods (not implemented yet, to be added in a later commit):
truncate
divide
roundDown (optional alias for the old trunc)
@HotwheelsSisyphus HotwheelsSisyphus added comp: sclang sclang C++ implementation (primitives, etc.). for changes to class lib use "comp: class library" comp: server plugins comp: help schelp documentation labels Feb 27, 2026
@HotwheelsSisyphus HotwheelsSisyphus marked this pull request as draft February 27, 2026 20:15
@telephon

Copy link
Copy Markdown
Member

Thank you for this overview! For the dev meeting in may, we'll write up a strategy (with @capital-G and you could also join!).

@telephon

telephon commented Feb 28, 2026

Copy link
Copy Markdown
Member

As a direct reply for the part 2:
add to the end of the respective naryOpUGens file.

There is a slightly difficult trade-off to be made with respect to unary vs. binary op.

  • if we want to provide an easy option for people to switch between the two behaviours, then the interface should be the same for both
  • if we want to indicate that this is an implementation of the c-interface, it should be unary. This has the advantage of being slightly more efficient.

I tend towards the first one, and (still, sorry!) towards naming it trunc2 for symmetry reasons The documentation could be:

  • trunc: asymmetric truncation
  • trunc2: symmetric truncation (as is standard in c, but with an extra argument)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: help schelp documentation comp: sclang sclang C++ implementation (primitives, etc.). for changes to class lib use "comp: class library" comp: server plugins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants