-
Notifications
You must be signed in to change notification settings - Fork 707
[css-values] Emulate conditionals with discontinuous function(s) in calc() #6638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Oh, I guess |
No, So "use x^2 if x is positive but use 2*x if x is negative (and use +0 if x is ±0)" is calc(max(0, sign(var(--x))) * pow(var(--x), 2) - min(0, sign(var(--x))) * 2 * var(--x)) In #4731 (comment) I wrote a summary for writing |
Yeah, |
I guess if you could use math in
|
What's the problem for 0? If you want something special for 0, you can use Having proper conditionals would be better of course, but for now |
We added |
calc()
doesn't support conditionals (e.g.if
statements) today, and that is a good thing. We shouldn't add conditionals.However, it is pretty natural to want to write piecewise functions with
calc()
. Conditionals are certainly sufficient to express piecewise functions, but they aren't actually necessary - all you need is a discontinuous basis function.min()
andmax()
arealmostexpressive enough, but not really. Edit: This could be polyfilled bymax()
, but we might want to do better here.(This kind of thing is actually pretty common in GPU programming, where programmers often try to write branchless code because branches are particularly slow on many GPUs. I'm cribbing from common patterns in GPU software.)
One way to do this would be if we added a
step()
function, which looks like this:For example, if the author wanted to express "use x^2 if x is positive but use 2*x if x is negative" they could do it like this:
A simpler way for authors, but just as expressive, would be to add something like
select(a, b, condition)
. This would result ina
ifcondition
is greater than 0, andb
otherwise. The same example written this way would be:This seems like not much implementation burden, but a significant increase in power / flexibility for authors.
The text was updated successfully, but these errors were encountered: