Skip to content

[css-values] Allow sqrt(<length-percentage>) #3764

@Crissov

Description

@Crissov

In b8935e7, @tabatkins has just added trigonometric and exponential functions to [css-values] as discussed in #2331. The commit includes a good explanation why hypot(A, B) is useful (beyond possible number range overflows) by supporting <length> instead of just <number>, like pow(A, B) does.

However, the argument only addresses pow(A, B). I wonder whether similar reasoning could be applied to sqrt(A) which is currently defined as a convenience alias for pow(A, 0.5). Please consider to change this to pow(A/a, 0.5)*a where a is unity with the same dimension and unit as A, e.g. 1px . This means, it would return the square root of the number of the value A using the original dimension (and unit) of A.

(This a could also be exposed as a separate function, e.g. unit(A) with unit(16px), unit(8px + 8px), unit(2 * 8px) all yielding 1px, and unit(1in + 2cm) as well because pixel is the canonical unit, while unit(2cm), unit(1cm + 1cm) and unit(2 * 1cm) should yield 1cm if possible in [css-typed-om]. I would have needed this recently to dynamically remove the length units from the translation components in a matrix().)

If the square root function supported <length>, there might be less reason for having a distinct hypot() function, but more reason to also introduce cbrt(A) for the cubic root and possibly squared(A) and cubed(A) for raising the number of the value A to the second (or third) power without affecting its unit and dimension.

While the square root function can be expressed by the power function with the terminating decimal fraction 0.5 as the exponent parameter, the cubic root requires an approximation to something like 0.333 or a calculation of 1/3. This may lead to unexpected effects due to binary rounding which could be avoided by a generic root(A, B) function which (almost) equals pow(A, 1/B), or perhaps by an inv(A) or rec(A) function that inverts the value to its reciprocal, i.e. calc(1/A), and could be evaluated at a later stage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions