Thread: Sign and Signn Functions (w/o IF's, math only!)

1. Sign and Signn Functions (w/o IF's, math only!)

Calculus functions include no programming statements,
so I just had to figure out these for myself for proof!!!!

Abs = (-((-(Value * -1) * Value) ^ (1 / 2) * -1))
Sign = ((-(Abs(Value - 1) - Value) - (-Abs(Value + 1) + Value)) * 0.5)
Signn = ((-((Value \ 1 <> 0) * 1) + -1) + -(((-Value \ 1 + -1) = 0) * 1))  Reply With Quote

2. Re: Sign and Signn Functions (w/o IF's, math only!)

For signn, doesn't your use of a comparison violate the rule of no programming statements? Or is the <> a typo?  Reply With Quote

3. Re: Sign and Signn Functions (w/o IF's, math only!)

Also, I haven't thought about your ABS example thoroughly, but I thought abs was traditionally simplified down to (n^2)^(1/2)  Reply With Quote

4. Re: Sign and Signn Functions (w/o IF's, math only!)

Sign = 1 + 2 * (Value < 0)

LOL :-))

You cannot use comparison because its too easy.

You cannot use Abs because Abs = Value / Sign(Value) i.e. Sign = Value / Abs(Value) which is just too easy.

cheers,
</wqw>  Reply With Quote

5. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by wqweto Sign = 1 + 2 * (Value < 0)

LOL :-))

You cannot use comparison because its too easy.

You cannot use Abs because Abs = Value / Sign(Value) i.e. Sign = Value / Abs(Value) which is just too easy.

cheers,
</wqw>
Those definitions fail when value = 0.  Reply With Quote

6. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by OptionBase1 Those definitions fail when value = 0.
Works for me.

Just don't use it if you don't like it :-))

cheers,
</wqw>  Reply With Quote

7. Re: Sign and Signn Functions (w/o IF's, math only!)

By definition, the sign of 0 is 0
Using your "Sign = 1 + 2 * (Value < 0)" function in VB6, when value = 0, Sign = 1, which is wrong.

By definition, the absolute value of 0 is 0
Using your "Abs = Value / Sign(Value)" function, and using the proper evaluation of Sign(0), then when value = 0, Abs = 0 / 0, which is problematic.

The traditional mathematical definition of something like the sign function is a piecewise function.

Code:
sign(x) = {
-1, x < 0;
0, x = 0;
1, x > 0 }
A piecewise function of x is a mathematical way of expressing "if" logic and evaluating a different function based on the value of x. The OP's goal was to take a piecewise function and turn it into a standard function of x, where that single function is evaluated for all values of x and returns the same value as the piecewise function does.

So no, your definitions do not work for value = 0. But keep using them if you like them.  Reply With Quote

8. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by OptionBase1 By definition, the sign of 0 is 0
Using your "Sign = 1 + 2 * (Value < 0)" function in VB6, when value = 0, Sign = 1, which is wrong.
Already told you it works for me and it's not wrong and works for most other programmers too (outside of VB land apparently).

The definition I use is that Sign = 1 for Value >= 0 and I reap the benefits of not dealing with three-valued logic.

I don't care about the sign of zero particularly, so it can be 1, -1, 0 or 42

cheers,
</wqw>  Reply With Quote

9. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by wqweto Already told you it works for me and it's not wrong and works for most other programmers too (outside of VB land apparently).

The definition I use is that Sign = 1 for Value >= 0 and I reap the benefits of not dealing with three-valued logic.

I don't care about the sign of zero particularly, so it can be 1, -1, 0 or 42

cheers,
</wqw>
That's fantastic that you don't care about what the sign of 0 is, at least your code is consistent with that sentiment. This thread is about math, which is more stringent than code, as is demonstrated every day by people who write code that they think is good enough but fails edge cases.  Reply With Quote

10. Re: Sign and Signn Functions (w/o IF's, math only!)

All right  Ill use built-in Sgn function so my crapy code is based on solid math principles and never try clever hacks again, I promise.

Or I might borrow the succinct expression in OP instead, convoluted math cannot be a wrong choice.  Reply With Quote

11. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by wqweto All right  Ill use built-in Sgn function so my crapy code is based on solid math principles and never try clever hacks again, I promise.

Or I might borrow the succinct expression in OP instead, convoluted math cannot be a wrong choice.
Amazing. Solid math principles, like that the sign of 0 = 1? Oh yeah, your code is right because you don't care about the case(s) where it is provably wrong. What a great programming attitude there.

You do realize (and by that, I mean that it is obvious that you don't realize) that the entire purpose of this thread (which is in the math forum, by the way, just so you know...) is to discuss purely mathematical ways to calculate certain functions using no programming statements. It's not about code at all. NOT ABOUT CODE AT ALL. ONLY MATH.

That' being said, here's a fix for your cleverness.

sign = (1 + 2 * (Value < 0)) * (Value <> 0) ^ 2

You're welcome.  Reply With Quote

12. Re: Sign and Signn Functions (w/o IF's, math only!)

but doesn't this assume that the expressions (Value < 0) and (Value <> 0) produces values of -1 if true? This is not true for many programming languages.

I know that VB returns -1, but this isn't a VB forum but a maths one.

Don't you need something general like:

Code:
sign = (1 - (2 * (Value < 0)) ^ 2) * (Value <> 0) ^ 2  Reply With Quote

13. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by OptionBase1 For signn, doesn't your use of a comparison violate the rule of no programming statements? Or is the <> a typo?
Cohesion it does. But I made that one up. I am no ancient mathematician too so that is what they had too. Sign Negate. It might exist or in theory is basic as is. Return won't be the sign passed.  Reply With Quote

14. Re: Sign and Signn Functions (w/o IF's, math only!) Originally Posted by OptionBase1 Also, I haven't thought about your ABS example thoroughly, but I thought abs was traditionally simplified down to (n^2)^(1/2)
-63.34 comes up "Invalid procedure call or argument." Fixed that.  Reply With Quote

15. Re: Sign and Signn Functions (w/o IF's, math only!)

If you're up for a challenge I have this other one I cheated on and I am certain I didn't cheat once before I just kind of forgot how. AbsWhole and AbsDecimal. In VB it is easy a cheat AbsWhole = Value \ 1, and AbsDecimal = Value - AbsWhole(Value)  Reply With Quote

sign  Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•