- #1
SonyAD
- 68
- 0
How would you solve this without using decisional blocks?
What precisely do you mean by "solve", and by "decisional block"?SonyAD said:How would you solve this without using decisional blocks?
Pere Callahan said:How would I solve what? What about
[tex]
\operatorname{sgn} x = x/|x| ?
[/tex]
Hurkyl said:What precisely do you mean by "solve", and by "decisional block"?
Jarle said:Algorithms like;
|x| = x if x positive; else -x
are not essentially different from ordinary functions like f(x) = 2x, or qualitatively different you might say.
Jarle said:Both, given an argument, have an explicit way of producing a specific output. That is what functions are all about.
Jarle said:The function sgn(x) is not so to say a 'cop-out' for not being able to define an explicit function for the purpose you have, but a completely natural way of producing an element given an argument. I can though remember I felt the same at some point.. As a side note; one would not accept 'piecewise' functions at some point a few hundred years ago.
Jarle said:Nevertheless, within the boundaries of the question i propose [tex]f(x) = lim_{n \to \infty} \frac{2}{\pi} \arctan(xn)[/tex] .
Office_Shredder said:If you're afraid of dividing by zero, add machine epsilon to x first. This cannot change the sign, and if you are legitimately trying to find what sign [tex] - \epsilon[/tex] is, you're probably doing it wrong
Jarle said:Nevertheless, within the boundaries of the question i propose [tex]f(x) = lim_{n \to \infty} \frac{2}{\pi} \arctan(xn)[/tex] .
SonyAD said:I don't see how the Heaviside function is any simpler. Or elegant.
Trunc() doesn't involve branching/decision blocks because all you ever do is clear some bits, set them to 0. Or discard what's after the decimal point. Just like abs() only changes the sign or sign bit one way.
Hrm. If you were interested in computer programs that manipulate binary representations of numeric data, then why the heck didn't you say so, when I asked you what you meant by "solve"? :grumpy:SonyAD said:abs() is not a piece wise function. It only requires you to turn one particular bit to one particular value each and every single time.
SonyAD said:Ok. Here's how I did it.
[tex]trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} - 1 \right) + trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} + 1\right)-trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} \right)[/tex]
SonyAD said:Ok. Here's how I did it.
[tex]trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} - 1 \right) + trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} + 1\right)-trunc\left( \frac{ | x + 1 | - | x - 1 | }{2} \right)[/tex]
Jarle said:How much more decision is turning a positive sign to 1 and a negative sign to 0, compared to removing the fractional part?
Hurkyl said:Hrm. If you were interested in computer programs that manipulate binary representations of numeric data, then why the heck didn't you say so, when I asked you what you meant by "solve"? :grumpy:
Hurkyl said:Incidentally, conditional assignment doesn't involve branching.
Hurkyl said:I'm having trouble thinking of any situation where hacking together a decisional block via floating-point operations is both preferable to branching that I wouldn't expect to work even better with conditional assigns.
P.S. I suspect trying to bit twiddle floating-point data in registers to be inefficient.
Mark44 said:I'm with Hurkyl on this - if your question was in the context of a computer implementation, why didn't you say so up front?
Mark44 said:In terms of computer arithmetic operations, the expression above seems very inefficient.
Mark44 said:If you have a system that adheres to IEEE Standard for Floating Point Operations (IEEE 754), it's a simple matter of looking at the highest-order bit to determine the sign of the number.
Mark44 said:In the first term above you have two floating point additions, two calls to fabs, a floating point subtraction, a floating point division, and then a truncation.
Mark44 said:Then you have to do the same exact thing two more times. All of these operations takes a number of clock cycles to complete. It might be the case that calculating the sign of a number via the expression above is slower than if-else logic, even considering cases where the else branch is taken, and the FP unit pipeline has to be restarted.
D H said:You've just hidden the decision blocks inside this black box function trunc (which is loaded with decision blocks). In a very inefficient way, I might add.
What exactly is the point of this exercise?
"Setting a bit to zero" is very much a computer-specific operation, rather than having anything to do with mathematics of real numbers.SonyAD said:That's not the point at all. I was just trying to show how the implementation of such a function doesn't involve decision blocks.
No it's not. On most (all?) microprocessors these days, conditional assign is a single machine instruction. There is no branching involved; flow always moves onto the next instruction no matter what the result of the test is.The branches are very short but it's still branching.
Well, gee, you never mentioned fixed point arithmetic until this post. How were we to know that? We can't read your mind. You need to spell things out. What exactly are you trying to do here, and what is the point of this silliness?SonyAD said:The same with trunc. In a fixed point implementation of real numbers you set all the bits in the fractional part to 0. On paper you only need to discard what's after the decimal point.
Hurkyl said:"Setting a bit to zero" is very much a computer-specific operation, rather than having anything to do with mathematics of real numbers.
Hurkyl said:The very specific reason I asked you to precisely clarify what you meant by "solve" and by "decisional block" was to prevent exactly what this happened in this thread. You are the only one who has any idea what you actually mean, everyone else posts an answer according to other reasonable interpretations of those words, and then you get persnickety because nobody was psychic and played the game in exactly the way you had in mind.
Hurkyl said:Anyways, I can prove that you cannot construct the sign function as a composition of elementary arithmetic operations (+,-,*,/) and the absolute value function: each of those functions is continuous, and the composite of continuous functions is continuous, so I am curious what else you had in mind as permissible.
Hurkyl said:No it's not. On most (all?) microprocessors these days, conditional assign is a single machine instruction. There is no branching involved; flow always moves onto the next instruction no matter what the result of the test is.
D H said:Well, gee, you never mentioned fixed point arithmetic until this post. How were we to know that? We can't read your mind. You need to spell things out. What exactly are you trying to do here, and what is the point of this silliness?
SonyAD said:abs() is not a piece wise function. It only requires you to turn one particular bit to one particular value each and every single time.
[...]
That's why abs() can be used to implement decision blocks in mathematics. And so can trunc(), which only requires you to dispose of the fractional part of a number. This is easy enough in fixed point arithmetic. Just set the least significant half/part to 0 every time. Truncating a float is not straight forward, though. But that's because of the ugly implementation.
I don't know; you haven't precisely specified what you mean by those. The closest thing you gave is that it's to be implemented via a straight-line computer program.SonyAD said:Furthermore, how is conditional assignment not a piecewise function? Or a decision block?
Hurkyl said:P.S. "a method of computing a function" has nothing to do with the function itself, other than producing its values.
The naive sign function for real numbers is a mathematical function that returns the sign of a given real number, either positive (+1), negative (-1), or zero (0). It is denoted as sgn(x) and is defined as follows: sgn(x) = 1 if x > 0, sgn(x) = -1 if x < 0, and sgn(x) = 0 if x = 0.
The purpose of the naive sign function is to determine the sign of a real number. It is often used in mathematical calculations and can also be used in programming to determine the direction of a value or to classify data.
The traditional sign function, also known as the signum function, returns the complex argument of a given complex number. It is denoted as sign(x) and is defined as follows: sign(x) = x/|x| for x ≠ 0 and sign(0) = 0. The naive sign function, on the other hand, only considers real numbers and returns a simpler output of +1, -1, or 0.
Yes, the naive sign function can be applied to all real numbers. It is defined for all real numbers and will always return a value of +1, -1, or 0.
One limitation of the naive sign function is that it does not consider the magnitude of the real number. It only returns the sign, regardless of how large or small the number is. Additionally, it is not defined for complex numbers, so it cannot be used for those types of calculations.