Thread: My grandpa told me a good way to get the absolute value of a number was to...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    My grandpa told me a good way to get the absolute value of a number was to...

    So you have some float, a. My grandpappy says that a good way of getting the absolute value is to square the number then square root the result.

    I facepalmed so hard.

    Did we not just flip sign bits back then or what? Mind you, this was all done in Basic too which I'm not sure what that is.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like your grandpa is good at trolling you
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This technique is used, among other things, to find the "average" value of a waveform that has data both in the positive and negative regions. It is the R and S in RMS.

    For instance, the average of a perfect sine wave is zero (the positive area == the negative area, which when combined, result in zero).

    So by squaring the points of a sine wave, you make all negative points positive. Then you find the mean of those squares. Then you take the square root to arrive at the RMS value.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by MutantJohn View Post
    Did we not just flip sign bits back then or what?
    Not meaning to be pedantic, but this wrong... Square rooting doesn't provide a positive number, it returns an unsigned number (one that is neither positive nor negative).

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Not meaning to be pedantic, but this wrong... Square rooting doesn't provide a positive number, it returns an unsigned number (one that is neither positive nor negative).
    O_o

    Actually, the "square rooting" of a nonnegative real produces both a positive value and a negative value.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by phantomotap View Post
    Actually, the "square rooting" of a nonnegative real produces both a positive value and a negative value.
    To add to this, the mathematical notation for the result (y) of the square root of x is y=−√x and y=√x. It's not -y and y. The idea that the square root returns a unsigned number and then we add both signs to each result is incorrect. The only number which isn't either positive or negative is 0.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    The square root function (by the mathematical definition) returns a single value, the principal square root, always positive number. The negative of that value also solves the equation x^2 = y. This is a coincidental, because the root in this case is 2 (an even number), the "negative root" works: but if the root is odd, it does not work.

    For example, pow(8, 1/3) = 2, but (-2)*(-2)*(-2) = -8.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The square root function (by the mathematical definition) returns a single value, the principal square root, always positive number. The negative of that value also solves the equation x^2 = y. This is a coincidental, because the root in this case is 2 (an even number), the "negative root" works: but if the root is odd, it does not work.

    For example, pow(8, 1/3) = 2, but (-2)*(-2)*(-2) = -8.
    O_o

    Do you realize that you used a specific flavor of square root ([Edit]I'm speaking of the result; I'm not speaking of the operation.[/Edit]) to draw a conclusion about the value provided by the square root operation in general?

    Do you realize that you used an example which isn't a square root to draw a conclusion about the value provided by the square root operation in general?

    I'm not sure what you are trying to say, but you have failed.

    The word "principal" in "principal square root" is important in context precisely because it is the principal square root which is the nonnegative. In other words, the square root function returns two values the nonnegative of which we've chosen to distinguish in materials as the "principal".

    As far as it goes, your example of a cubed root is correct. However, you are the first person to reference cubed roots. As is obvious, a cubed root is not a square root. The cubed root example simply shows nothing about the principal square root or square roots in general.

    The square root operation does not return a single value. The square root operations returns two values. You can speak specifically about the principal square root, but the principal square root is not an operation. The principal square root is the nonnegative result of applying the square root operation to a nonnegative real.

    [Edit]
    Also to be absolutely clear, there is nothing whatsoever coincidental about these operations, the results, or the language used to describe those results.

    We described the operation. Part of the description of the square root operation is the results of that operation. The language "principal square root" is similarly defined to allow people communicating about mathematics to speak specifically about one of those results.

    Similarly, the "negative root" works, and the result is not any way coincidental.

    Consider:

    Code:
    pow(16, 1/2) = 4 and -4
    4 * 4 =  16
    -4 * -4 =  16
    That is not coincidence.

    That is fact.

    This fact arises because the very definition of roots and multiplication.
    [/Edit]

    Soma
    Last edited by phantomotap; 09-15-2014 at 07:23 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Yarin View Post
    Not meaning to be pedantic, but this wrong... Square rooting doesn't provide a positive number, it returns an unsigned number (one that is neither positive nor negative).
    Then this is even more reason not to use it!

    The only way to give the roots any significance is to assign a sign (for both negative and positive values) which requires an unsigned to int conversion which creates the possibility for rounding errors.

    *Sigh*

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MutantJohn
    Then this is even more reason not to use it!

    The only way to give the roots any significance is to assign a sign (for both negative and positive values) which requires an unsigned to int conversion which creates the possibility for rounding errors.
    I think you're confusing Yarin's mathematical assertion with a particular implementation in some programming language. Yarin's use of "unsigned" here just means "has no sign", which was explained as "neither positive nor negative", not "unsigned int".

    Anyway, Yarin's claim is new to me: I learnt it the way phantomotap and Mario F. stated in posts #5 and #6. The claim sounds wrong since I understand a real number greater than zero to be positive, not "unsigned" just because it is a square root.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by MutantJohn View Post
    Then this is even more reason not to use it!

    The only way to give the roots any significance is to assign a sign (for both negative and positive values) which requires an unsigned to int conversion which creates the possibility for rounding errors.

    *Sigh*
    Rounding is good. Roots aren't meant to be square. Haven't you ever seen a tree? Sounds like your Grandpa knows what's going on!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by laserlight View Post
    Yarin's use of "unsigned" here just means "has no sign", which was explained as "neither positive nor negative", not "unsigned int"
    It would even be best to just drop the word "unsigned". It has no meaning in mathematics. It is strictly a computational term.

    In computing unsigned refers to a value that can be either positive or negative. In mathematics, 0 is neither positive or negative. While the square root of a positive number returns both a positive and a negative result.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by laserlight View Post
    Yarin's use of "unsigned" here just means "has no sign", which was explained as "neither positive nor negative", not "unsigned int".
    Exactly.


    Quote Originally Posted by Mario F. View Post
    It has no meaning in mathematics.
    An ironic thing to say considering Wiki has an article entitled "Sign (mathematics)".


    Quote Originally Posted by Mario F. View Post
    It would even be best to just drop the word "unsigned".
    Probably true nonetheless.


    Quote Originally Posted by Mario F. View Post
    While the square root of a positive number returns both a positive and a negative result.
    Quote Originally Posted by laserlight View Post
    The claim sounds wrong since I understand a real number greater than zero to be positive, not "unsigned" just because it is a square root.
    Indeed, the standard definition of a square root is
    Code:
    {x, -x}
    , because there's no way of knowing whether or not the source was negative or positive (tis a give-in, everyone knows this).
    However, I prefer to think of "x" and "+x" as "unsigned number" and "positively signed number" rather than the same things, specifically in the context of absolute numbers as presented by the OP, because when you say "+x" it's understood that you are considering a number that is positive as opposed to being negative, but a singular return value of a square root (again, in the context the OP set) cannot be known to be positive or negative, yet it's still clearly a number, hence "unsigned".

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Yarin View Post
    An ironic thing to say considering Wiki has an article entitled "Sign (mathematics)".
    And yet makes no mention of unsigned numbers...

    Indeed, the standard definition of a square root is {x, -x}, because there's no way of knowing whether or not the source was negative or positive (tis a give-in, everyone knows this).
    What do you mean we don't know the source? The square root of a negative number is a complex number and doesn't follow the same formal definition of the square root of a positive number.

    However, I prefer to think of "x" and "+x" as "unsigned number" and "positively signed number" rather than the same things, specifically in the context of absolute numbers as presented by the OP, because when you say "+x" it's understood that you are considering a number that is positive as opposed to being negative, but a singular return value of a square root (again, in the context the OP set) cannot be known to be positive or negative, yet it's still clearly a number, hence "unsigned".
    You are looking at this from a computing perspective. The OP grandfather was almost certainly speaking about mathematics.
    Last edited by Mario F.; 09-16-2014 at 01:58 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Yarin
    However, I prefer to think of "x" and "+x" as "unsigned number" and "positively signed number" rather than the same things, specifically in the context of absolute numbers as presented by the OP, because when you say "+x" it's understood that you are considering a number that is positive as opposed to being negative, but a singular return value of a square root (again, in the context the OP set) cannot be known to be positive or negative, yet it's still clearly a number, hence "unsigned".
    The context that MutantJohn set is about "absolute value". Therefore, considering the positive and negative square roots of the square of the original (positive real) number, we should obviously choose the positive. There is no need for an additional notion of an "unsigned" number in this context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good Ole Random Number
    By jimmyn in forum C Programming
    Replies: 9
    Last Post: 10-02-2012, 02:42 PM
  2. I need a really good technical explantion on the absolute value
    By Overworked_PhD in forum C Programming
    Replies: 6
    Last Post: 01-06-2008, 10:43 PM
  3. Told ya so...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 09-12-2007, 08:12 PM
  4. The computer told me...
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-17-2003, 03:48 AM
  5. i was told something and it screwed me up!
    By DarkViper in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 02:02 PM