Skip to content

[css-values] Function(s) for Standard Means #4700

@Crissov

Description

@Crissov

ECMA-262 (Javascript) does not have dedicated functions (in its global Math object #4688) for the commonly known standard means, except for a rather unexpected one:

  • arithmetic mean or average or middle: calc( (x + y) / 2), calc( (x + y + ) / 3)
  • geometric mean or arithmetic-harmonic mean: sqrt(x * y), pow(x * y * z, 1/3)
  • harmonic mean: calc(2 / (1/x + 1/y) ), calc(3 / (1/x + 1/y + 1/z) )
  • quadratic mean or root mean square (RMS): sqrt( (x*x + y*y) / 2), hypot(x, y, z)
  • cubic mean: cbrt( (x*x*x + y*y*y) / 2) (if it was added), pow( (pow(x, 3) + pow(y, 3) + pow(z, 3)) / 3, 1/3)

Those can all be described by a generalized mean or power mean by a characteristic integer exponent i: pow( (pow(x, i) + pow(y, i)) / 2, 1/i), pow( (pow(x, i) + pow(y, i) + pow(z, i)) / 3, 1/i). This exponent is −1 for the harmonic, 0 for the geometric, 1 for the arithmetic, 2 for the quadratic and 3 for the cubic mean (and so on).

There are two lesser known means, usually specified only for two arguments, i.e. the arithmetic-geometric mean and the geometric-harmonic mean, which result from letting i be ±½.

The code example provided above use two or three arguments, but for an arbitrary number of arguments, one would need functions like sum() or add() for Σ, product() or multiply() for Π and count() or n() to calculate these means. This would be simple to do in JS, but is hard – probably impossible – in plain CSS.

In #581, a map() or interpolate() function has been proposed, which would be related to these functions. (The resolution of that issue is in #2854 with specification pending.) Unfortunately – and almost as usual – I cannot provide any specific use cases for such mean functions, but I think they might be valuable in working with colors, positions and transitions as well as sets of sizes, especially when using cascading variables and units that may compute to different values depending on the user's environment. The steps of font-size keywords, for instance, should form a geometric progression, i.e. a new step between two existing sizes would be calculated as the geometric mean thereof (which is possible already, now that sqrt() or pow() exists).

In case there was agreement to add functions to calculate standard means to CSS, there would still be the issue whether to do so in a single function mean(..., i) (iN, i ≥ −1; or i ∈ {h | g | a | q | c}; or i ∈ {harmonic | geometric | arithmetic | quadratic | cubic}) or in several separate functions like avg(), average(), mid(), middle(), medium() or amean(), meana(), mean-a(), arithmetic-mean(), mean-arithmetic(), mean1() and so on.

PS: For a proposal for mode mod()mode() and median med() see #905.

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