1

I have a calculation that goes like this:

$$\text{<any positive number>} - 100$$

As long as the result is a positive number it is acceptable.

However, if the result is a negative number, I need the outcome to calculate to zero. A negative number is not permissible.

Is such a formula possible?


Update 1

This is for use in a CSS calc function .

 .class { margin-top: calc(100vh - 100px); }

I don't want the margin to ever be negative.

The function accepts only basic math operators (+, -, /, *).


Update 2

Some have mentioned in the comments that this question is more about CSS than math, and belongs in Stack Overflow.

However, this question is seeking a mathematical formula which has nothing to do with CSS (or coding, for that matter). It just happens to go into a CSS function.

  • 1
    This site is about mathematics. If you have any questions about CSS or related things I suggest asking them on StackOverflow instead. – Kamil Jarosz Feb 24 '16 at 20:08
  • @kamil09875, I apologize for the late update, but this is not a question about CSS. It's a question about the mathematical expression that goes into the function, which has nothing to do with CSS. – Michael Benjamin Feb 24 '16 at 20:09
  • Also, I upvoted your answer and others which answer the original question. Thanks. – Michael Benjamin Feb 24 '16 at 20:10
  • 1
    This really is a css question - you need to know whether css supports max or absolute value or an if statement to use one of the answers. If not, you need a css hack. Posting on a better SE site is more likely to find one. – Ethan Bolker Feb 24 '16 at 20:12
  • @EthanBolker, I didn't know how to build such a formula, so I didn't know where to target or how to narrow down the question. The calc function only accepts addition (+), subtraction (-), multiplication (*) and division (/). https://www.w3.org/TR/css3-values/#calc-notation – Michael Benjamin Feb 24 '16 at 20:15
  • @Michael_B It is about CSS, because we are restricted to use the language of CSS. – Thomas Andrews Feb 24 '16 at 20:19
  • If such a calculation is possible using basic mathematical operators (as listed in previous comment), I would appreciate an answer. If not, I'll come back in 24 hours and accept the community choice. – Michael Benjamin Feb 24 '16 at 20:20
  • Does the range checking example (scroll down in the link you provided) solve your problem? – Ethan Bolker Feb 24 '16 at 20:26
  • @EthanBolker, I appreciate the research. Some CSS properties, such as width and height do not allow negative values. So regardless of the calc result, it can never be less than zero, and this question wouldn't be necessary. Some properties, however, such as margin, do take negative values. – Michael Benjamin Feb 24 '16 at 20:31

3 Answers3

7

You can write the $\max$ as

$\max(x,y) = (|x-y|+x+y)/2$

thus you would like $\max( x-100,0)$ that can be written as $$ (|x-100|+x-100)/2 $$

pancho
  • 752
  • 3
    I would like to add that $|x-100|$ may be written as $\sqrt{(x-100)^2}$ if one would like to use only "traditional" operations. – Apoorv Feb 24 '16 at 20:15
  • but how do you get square root with only + - * /? – BigName May 23 '17 at 09:40
  • 1
    You cannot, to rigorously define the square root for any value $y$ in an interval, it is needed the axiom of the supremum. – pancho May 23 '17 at 13:04
5

It is

$$z=\max(y,0)$$

where $y$ is your number.

Kamil Jarosz
  • 4,984
  • Correct, of course. I wonder whether the OP will accept "max" as a formula. – Ethan Bolker Feb 24 '16 at 19:59
  • @EthanBolker there are several representations of this function, except the $\max$ you can use absolute value, definition by cases etc. I think this one is the most elegant. – Kamil Jarosz Feb 24 '16 at 20:02
  • I agree on the elegance. I wonder if any of them will suit the OP. He may have a narrow notion of what "function" means. – Ethan Bolker Feb 24 '16 at 20:05
1

$ y= \begin{cases} x-100 , x>100,\\0 ,0<x\le100 \end{cases} $

Yeah..
  • 347