Results 1 to 15 of 15

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

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    25

    Resolved 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))
    Last edited by nforystek; Jan 12th, 2022 at 08:30 PM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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?

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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)
    Last edited by OptionBase1; Jan 13th, 2022 at 09:21 AM.

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    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>

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

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

    Quote Originally Posted by wqweto View Post
    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.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

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

    Quote Originally Posted by OptionBase1 View Post
    Those definitions fail when value = 0.
    Works for me.

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

    cheers,
    </wqw>

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

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

    Show your work.

    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.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

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

    Quote Originally Posted by OptionBase1 View Post
    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>

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

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

    Quote Originally Posted by wqweto View Post
    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.

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    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.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

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

    Quote Originally Posted by wqweto View Post
    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.

  12. #12
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    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
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    25

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

    Quote Originally Posted by OptionBase1 View Post
    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.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    25

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

    Quote Originally Posted by OptionBase1 View Post
    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.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    25

    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)

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width