-
Jan 12th, 2022, 08:01 PM
#1
Thread Starter
Junior Member
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.
-
Jan 13th, 2022, 09:15 AM
#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?
-
Jan 13th, 2022, 09:18 AM
#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)
Last edited by OptionBase1; Jan 13th, 2022 at 09:21 AM.
-
Jan 13th, 2022, 11:37 AM
#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>
-
Jan 13th, 2022, 12:29 PM
#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.
-
Jan 14th, 2022, 01:12 AM
#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>
-
Jan 14th, 2022, 08:57 AM
#7
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.
-
Jan 14th, 2022, 09:04 AM
#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>
-
Jan 14th, 2022, 11:42 AM
#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.
-
Jan 14th, 2022, 12:25 PM
#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.
-
Jan 14th, 2022, 01:56 PM
#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.
-
Jan 15th, 2022, 05:53 AM
#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
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)
-
Jan 22nd, 2022, 09:38 AM
#13
Thread Starter
Junior Member
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.
-
Jan 22nd, 2022, 09:42 AM
#14
Thread Starter
Junior Member
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.
-
Jan 22nd, 2022, 10:01 AM
#15
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|