## Exponent Engima

 Author Message Jamie-2229 SSCrazy Group: General Forum Members Points: 2955 Visits: 831 "Exclusive or" ouch - I was thinking exponential also. Arguments expressing dismay that mixing numeric types provide incorrect results, hmmm, isn't that a big part of programming? declare@x moneyset @x=9.99set @x=@x+.00489999Print @x --no changeset @x=9.999Print @x --duh - 10It's always about the math. Jamie mtassin SSCarpal Tunnel Group: General Forum Members Points: 4101 Visits: 72512 Sorin Petcu (8/8/2008)mtassin (8/7/2008)Sorin Petcu (8/7/2008)Allof you, you have had wrong! I know from school, and everybody knows, that sqrt function did not support negative numbers (except complex numbers). But with this in your mind, you will be wrong in computers world. You should be guided by documentation of language. And this documentation is clear enough: sqrt supports negative float numbers. I know, you will say about ansi sql and other stuff like superior programming languages. But this issue is obviously clear.No... because SQRT returns a float. AND THE FLOAT DATATYPE DOES NOT SUPPORT COMPLEX/IMAGINARY NUMBERS. Hence SQRT cannot receive a negative number as an input and a domain error will occur because the result of the SQRT(-1) or SQRT(-.01) is outside of the domain of a FLOAT datatype.Again, other guy who make wrong suppositions. Theoretical, from math point of view, "SQRT cannot receive a negative number as an input", but here, we are talking about computer universe. Please, lay down the theory and read carrefully the documentation (BOL). On the other side, FLOAT has negative values, also.This is nothing to do with theory. Read Books Online about the float data type and tell me where it supports complex numbers. The return data type of SQRT is a float, a float data type cannot contain an imaginary or a complex number.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.It's right there in BOL. Books OnlineReturns the square root of the given expression.Syntax SQRT ( float_expression ) Argumentsfloat_expression An expression whose type can be implicitly converted to float.Return Valuefloat And here is the BOL entry for the float data type.Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly. Note: The SQL-92 synonym for real is float(24). Data type Range Storage float - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 Depends on the value of n real - 3.40E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38 4 Bytes Transact-SQL Syntax ConventionsWhere is i listed as an allowed value for a float data type? If the SQRT function returns a float value, the return value has to be within the allowed values for it. The value of SQRT(-1) is outside of the allowed returnable values for the function.This isn't theory... this is fact.As a much simpler approach, take a calculator and try to take the SQRT of -1... if you have a really neato calculator you'll get a complex number back, if you don't you'll get an error.http://en.wikipedia.org/wiki/Imaginary_number --Mark Tassin MCITP - SQL Server DBAProud member of the Anti-RBAR alliance.For help with Performance click this linkFor tips on how to post your problems Sorin Petcu Say Hey Kid Group: General Forum Members Points: 680 Visits: 346 mtassin (8/8/2008)Sorin Petcu (8/8/2008)mtassin (8/7/2008)Sorin Petcu (8/7/2008)Allof you, you have had wrong! I know from school, and everybody knows, that sqrt function did not support negative numbers (except complex numbers). But with this in your mind, you will be wrong in computers world. You should be guided by documentation of language. And this documentation is clear enough: sqrt supports negative float numbers. I know, you will say about ansi sql and other stuff like superior programming languages. But this issue is obviously clear.No... because SQRT returns a float. AND THE FLOAT DATATYPE DOES NOT SUPPORT COMPLEX/IMAGINARY NUMBERS. Hence SQRT cannot receive a negative number as an input and a domain error will occur because the result of the SQRT(-1) or SQRT(-.01) is outside of the domain of a FLOAT datatype.Again, other guy who make wrong suppositions. Theoretical, from math point of view, "SQRT cannot receive a negative number as an input", but here, we are talking about computer universe. Please, lay down the theory and read carefully the documentation (BOL). On the other side, FLOAT has negative values, also.This is nothing to do with theory. Read Books Online about the float data type and tell me where it supports complex numbers. The return data type of SQRT is a float, a float data type cannot contain an imaginary or a complex number.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.It's right there in BOL. Books OnlineReturns the square root of the given expression.Syntax SQRT ( float_expression ) Argumentsfloat_expression An expression whose type can be implicitly converted to float.Return Valuefloat And here is the BOL entry for the float data type.Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly. Note: The SQL-92 synonym for real is float(24). Data type Range Storage float - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 Depends on the value of n real - 3.40E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38 4 Bytes Transact-SQL Syntax ConventionsWhere is i listed as an allowed value for a float data type? If the SQRT function returns a float value, the return value has to be within the allowed values for it. The value of SQRT(-1) is outside of the allowed returnable values for the function.This isn't theory... this is fact.As a much simpler approach, take a calculator and try to take the SQRT of -1... if you have a really neato calculator you'll get a complex number back, if you don't you'll get an error.http://en.wikipedia.org/wiki/Imaginary_numberAgain you have fallen in mistake. Again you assume the fact that "The return data type of SQRT is a float, a float data type cannot contain an imaginary or a complex number.The Square Root of -1 is i, which is an imaginary/complex number."The computer didn't know nothing about complex numbers. You assume that SQRT function, which is written by a computer programmer, should return i for -1 argument. Why? you wrong here. I could write my SQRT function how my muscles want. It is about programming and not about theory. Negative numbers are represented in computer different way by the way you know from theory. So, please don't mix the math theory with what is inside a computer. In Theory, theory and practice are the same...In practice, they are not. mtassin SSCarpal Tunnel Group: General Forum Members Points: 4101 Visits: 72512 Sorin Petcu (8/8/2008)The Square Root of -1 is i, which is an imaginary/complex number."The computer didn't know nothing about complex numbers. You assume that SQRT function, which is written by a computer programmer, should return i for -1 argument. Why? you wrong here. I could write my SQRT function how my muscles want. It is about programming and not about theory. Negative numbers are represented in computer different way by the way you know from theory. So, please don't mix the math theory with what is inside a computer.We're not talking about negative numbers, negative numbers are just the input to a function, we're talking about the output of the function. The Square Root of a Negative number (the output of the function) is represented in mathematics as an imaginary (pure) or complex (mixed a +bi) number.The SQRT function used by SQL server returns a float. The Float data type is not able to contain a complex number. Thus the return data type of the SQRT of -1 is out of the domain of the float data type and we get an error message. How the function determines this (either it actually tries to calculate it and gets an error after attempting to find this value, or if it throws the error as soon as it sees the negative I don't know.. and frankly, I don't care).I do know that -1 * -1 = 1 and that SQRT(1) = +/- 1.If you think that SQRT(-1) is a negative number... tell me what negative number squared yeilds a negative number, because what I was taught in math was that -1*-1 = 1. In fact, `SELECT -1 * -1`On my SQL box that returns 1, not -1. So -1 Squared = 1... and SQRT(-1) is a domain error.If you write your SQRT function to behave in a way that violates how mathematics work, then your company wouldn't be able to sell the software. That would be like if I decided that my multiplication function should randomly divide all results by .75 because I feel like it. Yah I could write that function, but nobody would sell it, and if they did, it would be acknowledged as a bug (see Intel chips with broken floating point mathematics circa 1994), in the case of software like SQL server this would result in a hotfix released because if your math functions don't follow the rules of mathematics, then companies don't use them. --Mark Tassin MCITP - SQL Server DBAProud member of the Anti-RBAR alliance.For help with Performance click this linkFor tips on how to post your problems Sorin Petcu Say Hey Kid Group: General Forum Members Points: 680 Visits: 346 mtassin (8/8/2008)We're not talking about negative numbers, negative numbers are just the input to a function, we're talking about the output of the function. The Square Root of a Negative number (the output of the function) is represented in mathematics as an imaginary (pure) or complex (mixed a +bi) number.The SQRT function used by SQL server returns a float. The Float data type is not able to contain a complex number. Thus the return data type of the SQRT of -1 is out of the domain of the float data type and we get an error message. How the function determines this (either it actually tries to calculate it and gets an error after attempting to find this value, or if it throws the error as soon as it sees the negative I don't know.. and frankly, I don't care).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) = 4294967296So 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! In Theory, theory and practice are the same...In practice, they are not. mtassin SSCarpal Tunnel Group: General Forum Members Points: 4101 Visits: 72512 Sorin Petcu (8/8/2008)[quote]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) = 4294967296So 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_complementIt'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 DBAProud member of the Anti-RBAR alliance.For help with Performance click this linkFor tips on how to post your problems Sorin Petcu Say Hey Kid Group: General Forum Members Points: 680 Visits: 346 no comment!! In Theory, theory and practice are the same...In practice, they are not. Mohamed-401375 SSC Eights! Group: General Forum Members Points: 889 Visits: 97 Good question. Makes a good differentiation between the bit Operator XOR and the Math function POWER. RBarryYoung SSCrazy Eights Group: General Forum Members Points: 9436 Visits: 9517 Thanks, Mohamed. I got the idea for this question because I keep making this mistake myself. -- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoungProactive Performance Solutions, Inc. "Performance is our middle name." John Mitchell-245523 SSCertifiable Group: General Forum Members Points: 7393 Visits: 15105 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 thing, just like James. I read "negative" instead of "non-negative" and so I immediately discounted the correct answer!John