4

I want to say minimum value for X is 1000. now X can be anything. so M would be the multiplier of X so that M*X >= 1000. What would be the mathematic way of doing this without using if statement?

M = 1;
X = 1000;
if(X < 1000) M = 1000/X;
X = M*X;

I want something like

$M = 1000/X$

$X = M*X;$

But this means X is always 1000. its not minimum.

I'm just being curios of what could be the expression without if statement.

4 Answers4

5

If you want to ensure that $X$ isn't lower than $1000$, using a multiplier is a contrived way* to achieve it.

Just write

$$X\leftarrow\max(X,1000).$$


The contrived way

You want to ensure

$$MX\ge1000$$ so that

$$M\ge\frac{1000}X.$$

Then you can take the multiplier

$$\max\left(1,\frac{1000}X\right)$$

and assign

$$X\leftarrow X*\max\left(1,\frac{1000}X\right).$$

muru
  • 137
  • Thanks a lot. This is what i was looking for. it looks way better. – M.kazem Akhgary May 15 '17 at 07:00
  • 2
    I would question the use of X ← e notation. As far as I'm aware, that's only standard in algorithms. More standard (and easier to use) in anything other than imperative programming would be saying “let X′ = max(X, 1000)”. – mudri May 15 '17 at 10:46
  • 2
    The OP expressed his question as a code snippet, having the effect to modify X. I stuck to this idea but used a more generic symbol for assignment. –  May 15 '17 at 11:03
3

Ah, Yves Daoust beat me to it. Note that if you want a differentiable function, then you can approximate the maximum function with something like $\log(e^x+e^{1000})$. This quantity is always greater than $1000$; for large $x$, it is slightly greater than $x$. It is never exactly equal to $x$, so it may not fit your needs. (It is also tricky to implement properly on a computer.)

More generally, you could compute $h\log(e^{x/h}+e^{1000/h})$ with a tunable parameter $h$; smaller values give you a sharper transition.

Chris Culter
  • 26,806
  • This is good answer. The only answer with unconditional expression. i liked using max function. Although that is conditional function. But its abstract and makes code readable. Log and power is expensive operation. But good answer after all – M.kazem Akhgary May 15 '17 at 07:11
2

How about something like this? $$ f(x) = \left\{ \begin{array}{ll} x & \quad x \geq 1000 \\ 1000 & \quad x < 1000 \end{array} \right. $$

rb612
  • 3,560
  • Yes. This could be the way to show that too. So this is basically conditional function. I thought there maybe some magic formula that works for all ranges. – M.kazem Akhgary May 15 '17 at 06:51
  • 1
    @M.kazemAkhgary Don't look for magical formulas. Look for something that's readable. The other suggestion of $\max,{X,1000}$ is readable; so is this. There's nothing wrong with expressing multiple cases using a structure that lets you talk about multiple cases. – David Richerby May 15 '17 at 13:32
0

Late for the show, however here is my try.

Let$\quad a,b\in\Bbb R$

$$max(a,b) = \frac{a + b + \vert a-b\vert}{2}$$

and

$$\vert a\vert = \sqrt{a^2}\quad$$


This leads to

$$M = (1000 + X + abs(1000 - X)) / 2;$$

Another (computationally slower) way to write this:

$$M = (1000 + X + sqrt( pow(1000 - X, 2) ) / 2;$$

Stephan
  • 188
  • 1
    Please never write anything like that. The purpose of writing mathematics down is to communicate it to people. Maybe you'll think I'm slow but I'd have to spend a couple of minutes figuring out what that cryptic statement is supposed to mean. In any case, I'm pretty sure it's wrong, as it has $X$ on both sides. – David Richerby May 15 '17 at 13:31
  • @DavidRicherby Please check my update. The writing may be clearer now. – Stephan May 15 '17 at 13:36
  • 1
    My point is that, while the equality you give for $\max$ is presumably true, it's orders of magnitude more work to parse and understand $(a+b+|a-b|)/2$. Perhaps the suggestion isn't so bad if you're writing a computer program and you profile it to check that the branch-free expression is faster, but abs itself might contain a conditional so there's a good chance it's rather slower. And your version exploiting $|a|=\sqrt{a^2}$ is liable to fail due to overflow in a computational setting. – David Richerby May 15 '17 at 13:41
  • 1
    And bear in mind that this is Mathematics, not Stack Overflow so the general presumption is that we're doing mathematics, not writing code. (But, then again, there's code in the question; I interpreted that as just being a language in which the asker knows how to define what they're looking for; the tag is "calculus".) – David Richerby May 15 '17 at 13:45