Skip to content

Alter fract() or add new fraction() function - addressing a negative float's decimal #9084

Description

@lee2sman

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.

  1. fraction of -1.3 -> -0.3
  2. fraction of -1.3 -> 0.3
  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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions