0

The absolute value of $x$ is defined as $x$ if $x>= 0$ and $-x$ otherwise.

But is it possible to define it without using logic? People say that $|x|=\sqrt{x^2},$ but that can give either $x$ or $-x.$ So maybe I can put the solutions to $\sqrt{x^2}$ in a tuple and refer to 2nd element, as $(-x, x)?$ So would this be correct?: $|x|=(\sqrt{x^2})_2$

ryang
  • 38,879
  • 14
  • 81
  • 179
  • 6
    "but that can give either x or -x" This is untrue. Look into this post and this post. – Maximal Ideal Apr 28 '23 at 13:34
  • 2
    $\sqrt{}$ denotes the principal root – Golden_Ratio Apr 28 '23 at 13:37
  • $\max(x, -x)$ (if we are talking about real numbers, of course). Of course, computing $\max$ might require some logic itself. – lmsteffan Apr 28 '23 at 13:38
  • 1
    "I wanted to see if it is possible to define it without using logic..." and a name like "professional coder". For this it may be worth asking what sort of numbers specifically you are trying to work with. If you are working with numbers on a computer, such as with signed integers... you can just directly modify the bit corresponding to the sign to be zero (corresponding to a positive number). – JMoravitz Apr 28 '23 at 13:40
  • 2
    In a programming context, taking the effort to square a number followed by square rooting a number, you may encounter errors when the square of the number winds up requiring more memory to store than the data type it was assigned as, leading to inaccuracies. – JMoravitz Apr 28 '23 at 13:42

0 Answers0