Alter fract() or add new fraction() function - addressing a negative float's decimal
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 24k
- Forks
- 3.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 25
Description
Topic
fract() returns the fractional part of a number. However, what THAT means is a matter of discussion, as a fractional part of a negative number has 3 potential outputs.
- fraction of -1.3 -> -0.3
- fraction of -1.3 -> 0.3
- fraction of -1.3 -> 0.7
In p5.js, fract(-1.3) produces 0.7. This output surprised me!
But this is in keeping with p5strands (which aliases glsl's fract) and WGSL's implementation: x - floor(x).
This all came up became I am trying to have feature parity between L5 creative coding library and p5.js. In Lua, I round toward 0 and remove the integer part, so fract(-1.3) produces 0.3.
Other implementations of fract
- C's modf, works the way i'm describing (rounding toward 0).
- python copies the same
- Rust, i think does the same?
Discussion so far
We are moving discussion from discord. @limzykenneth said that GSLS / shader langs return within the range [0.0, 1.0].
@davepagurek proposed creating a new function name like fractional(n) to disambiguate from fract()'s current output. @limzykenneth said he was leaning toward a signed value output such as e.g. fraction(-1.3) === -0.3.
My own instinct is to separate the decimal part from the sign so that fraction(-1.3) returns -> 0.3.
That's where we are at right now! So we need to decide to do this, what to name it, and which implementation for an added function, if we are commited to creating a new function for different output than fract's current implementation.
References
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the current fract() implementation in src/math/calculation.js and review issue #3487 for prior context. Read the linked discussion and references to compare the possible negative-number behaviors. This is complete only after the API name and semantics are agreed, then the selected behavior is implemented and covered by the project’s relevant checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- computer-graphics, frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100