Exponent Engima

  • Sorin Petcu (8/8/2008)


    I think you don't understand or you don't want to understand.

    Let me see: how it is represented the number -5 in computer? as binar form as 1111111111111111111111111111111111111111111111111111111111111011 and this number would be reconverted as 18446744073709551611 in decimal format.

    So, SQRT(-5) would be what? guess what:

    select SQRT(18446744073709551611) = 4294967296

    So we are talking only about numbers and not math theory. You said forth and forth about complex numbers. Wrong! Why you want to receive a complex number from a function built in a programming language? Why you want all the time that float number can't be asimilated as complex number? It is nothing to do with complex numbers in a programming language or an engine like sql server. Complex numbers are in math theory. So wake up!

    Sorry wrong.

    -5 in binary is what is below for a 64 bit integer (notice how it's 65 bits long).

    1111111111111111111111111111111111111111111111111111111111111011 when converted to an unsigned integer is 18446744073709551611, when converted to a signed integer it's -5.

    Just because you're using Calc to do the conversions... it doesn't apparently handle converting negative numbers to binary and back. Take -5 convert it to binary... convert it back... it should be

    -5 again.

    I'm sure you will agree that 18446744073709551611 is not equal to -5.

    Do you understand how to actually convert binary to decimal?

    http://en.wikipedia.org/wiki/Two's_complement

    It's interesting that you've perhaps pointed out a bug in calc.exe though.. if you use excel and do a DEC2BIN of -5 you get 1111111011 and if you use BIN2DEC to convert it back you'll get the correct answer which is -5.

    Here's an example in SQL

    select convert(binary,-5)

    select convert(int,0x0000000000000000000000000000000000000000000000000000FFFFFFFB)

    select convert(int,convert(binary,-5))



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • no comment!!

    In Theory, theory and practice are the same...In practice, they are not.
  • Good question. Makes a good differentiation between the bit Operator XOR and the Math function POWER.

  • Thanks, Mohamed. I got the idea for this question because I keep making this mistake myself.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • mtassin (8/8/2008)

    The Square Root of -1 is i, which is an imaginary/complex number. It cannot be returned as a float data type because it is out of the domain of the float data type.

    Mark, your argument assumes that the reader knows that the result of passing a negative parameter to the function results in an answer that is out of range or of the wrong data type. For instance, if I typed SELECT SQRT(-23) and I got the error "a domain error occurred", how would I know that this is wasn't because I used an odd number rather than because I used a negative number? I totally agree with you that this behaviour is correct: I'm just saying that I think the range of permissible values should be documented in Books Online. Look at the documentation for the ASIN function, for how it should be done:

    float_expression

    Is an expression of the type float or of a type that can be implicitly converted to float, with a value from -1 through 1. Values outside this range return NULL and report a domain error.

    By the way, I was fooled by the @b-2 thing, just like James. I read "negative" instead of "non-negative" and so I immediately discounted the correct answer!

    John

  • Nice question. I think the math behind the question is revealing for the answer.

    Good Job on this Barry (p.s. got it correct).

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nice job Jason. This is one tough question, and I know that I'm biased but it's really one of my favorite questions ever. (and I am really glad that the two mistakes that I made in it did not affect the correct answer, whew!) Even the discussion here has been great.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • I too liked the discussion as I reread it today.

    Involve math, SQL, and a good discussion and you have a good QOD!!:cool:

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 8 posts - 46 through 52 (of 52 total)

You must be logged in to reply to this topic. Login to reply