Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Fun with Unary Operators

Oct 16 2012 12:00AM by Madhivanan   

Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication and division. However the operators + ,- and ~ can also be used as unary operators to decide if a number is positive or negative. But we can have fun with the usage of these operators.

The following statement 

select -4,+4

returns –4 and 4 as result

Now execute the following statement
select -+-+4
The result is 4. But you may think it will give some syntax error

Now execute the following which also produces the result 4

select -+-+-+(-4)
select -+-+-+-+++4

Do you know why the following code produces -4 as result?
select -++-+-+-+++-4

Now execute the following and see the result
select -(-(-4))
select -(-(-(-4)))

The result is -4 and 4

The following statement returns -4 as the result
select +++++++++++++++++++++++++++++++++++++++++++++-4


So from these it is very clear that when there are even number of uniary minus the result is positive otherwise the result is negative

This is also true for unary ~ (Bitwise NOT)

select ~4
select ~~4
select ~~~~4

The results are -5,4 and 4 respectively.

The same can be applied to column of a table too. Cosider the following code

select 
	number as positive1, -number as negative1, +-+-number as positive2, -+-+-+(-(-number)) as negative2 
from
(
	select 1 as number union all
	select 2 as number union all
	select 3 as number 
) as t
positive1   negative1   positive2   negative2
----------- ----------- ----------- -----------
1           -1          1           -1
2           -2          2           -2
3           -3          3           -3

I do not see the practical usage of these leading unary + and - however for complex arithmetic calculation that involve in many additions and subtractions you can find out how the result is positive or negative using the above examples

Tags: sqlserver,tsql


Madhivanan
3 · 39% · 12440
10
 
0
Lifesaver
 
 
 
0
Incorrect



Submit

14  Comments  

  • I like your posts, but those are drifting into uselessness. C'mon equally useless would be if you add more (braces) to your examples...

    commented on Oct 16 2012 2:34AM
    chojrak11
    1009 · 0% · 25
  • Very interesting, Madhivanan! Thank-you for sharing!

    commented on Oct 16 2012 12:14PM
    Nakul Vachhrajani
    4 · 33% · 10587
  • Chojrak11, as I said, I do not see practical usage however it is good to know

    Thank you Nakul for your feedback

    commented on Oct 17 2012 12:12AM
    Madhivanan
    3 · 39% · 12440
  • Some people have objected to Madhivanan's post because it has no immediate practical application. Madhivanan has responded by agreeing that it doesn't, but that it is 'good to know'.

    He is right. Someone who has truly mastered a domain of knowledge has not just accumulated thousands of immediately-applicable 'chunks' of information, but has also absorbed a rich supporting matrix of background knowledge which allows him to generate new chunks when faced with novel situations.

    We don't know in advance what these novel situations will be, so we must learn as much as possible about anything that might be relevant. The very act of learning lots of things will make us smarter and more able to learn other things.

    Some non-human animals -- weaver-birds, beavers, termites -- are capable of astonishing feats of construction, but only in very limited situations, where exactly the right materials are available. A beaver can gnaw down trees and build a den in water, but put him in a situation where there are no trees, but rather rocks, and he cannot build a den from rocks.

    Humans, in contrast, can build their dwellings from a wide range of materials. So even if you're building your DBMS from bricks and mortar, take the time to learn something about nails and boards. You never know what this knowledge might come in handy.

    commented on Oct 17 2012 11:49AM
    DBMSAid
    1151 · 0% · 22
  • @DBMSAid

    Madhivanan is very talented and skilled person, and I don't want to question that.

    But come on, didn't you learn in elementary school that --4 = +4 and ---4 = -4 and ----4 = +4 ? How useful is ++-++-++-4 in your T-SQL code other than to obfuscate it? How innovative is that?

    commented on Oct 17 2012 1:46PM
    chojrak11
    1009 · 0% · 25
  • I did indeed learn that.

    But I also learned things in the world of pure mathematics which don't always quite apply in the world of computing, because of the necessity of representing real numbers in a finite number of bits. So I can't just immediately apply things which are abstractly true in the world of mathematics, to the world of software implementations of mathematics. Sometimes you get surprised.

    I'm not arguing that this particular example will possibly prove useful some day. Maybe the information about bitwise operators might, but I'll agree it's probably unlikely that the rest will.

    Rather, I'm arguing that, in general, things which may appear obscure or of little or no practical application, with respect to how a piece of software works, are still worth knowing about.

    I would hate to see Madhivanan believe that he had to restrict himself to immediately-applicable work-a-day examples. I'd rather he err on the side of being too wide-ranging in his choice of topics, than too narrow

    If the argument is that this example is too trivial ... well, I can guarantee you that there are at least a few SQL programmers out there who would not be sure about the answer to some of Madhivanan's questions in this post. (Granted, they are probably not following this series, but they should be!)

    commented on Oct 17 2012 3:49PM
    DBMSAid
    1151 · 0% · 22
  • Chojrak11,

    When I interviewed some sql developers, I asked a question about these leading unary operators but some candidates replied that it would give syntax error. So I thought of posting this to let everyone know that the same mathematical logic will work correctly in SQL Server. Also someone asked me how to use unary minus like how we use unary plus. While +++4 works correctly ---4 will not because SQL Server treats it as commented line. That is why in my post I used braces to correctly make use of unary minus like -(-(-4))

    As I specified in my post, I do not see very practical usage of these however it is good to know :)

    commented on Oct 18 2012 1:25AM
    Madhivanan
    3 · 39% · 12440
  • DBMSAid,

    Thanks for your feedback. It is good to know that you like other side of information too (regardless of whether they have practical usage or not).

    commented on Oct 18 2012 1:29AM
    Madhivanan
    3 · 39% · 12440
  • Hi, It is very nice and funny use of these operations.These are useful information for anyone.Thanks for sharing.

    commented on Oct 18 2012 5:50AM
    Crautepred
    2115 · 0% · 6
  • Hi, Wonderful and unique usage of Arithmetic operators.Thanks for sharing

    commented on Oct 19 2012 5:51AM
    Crautepred
    2115 · 0% · 6
  • hi you play very well with arithmetic operation.I dont know how to do third [operation][1]

    commented on Oct 19 2012 6:18AM
    Overy1970
    2895 · 0% · 2
  • Hi This is wonderful job and i am much impressed.

    commented on Oct 25 2012 8:18AM
    Crautepred
    2115 · 0% · 6
  • The naysayers have no imagination.

    select ~-n [n-1],n,-~n[n+1],-~-~n[n+2] from (values (1),(2),(3)) x(n);
    
    commented on Nov 30 2012 9:53PM
    Rick Bielawski
    284 · 0% · 149
  • Thanks Rick Bielawski. Thats very good method

    commented on Dec 1 2012 1:19AM
    Madhivanan
    3 · 39% · 12440

Your Comment


Sign Up or Login to post a comment.

"Fun with Unary Operators" rated 5 out of 5 by 10 readers
Fun with Unary Operators , 5.0 out of 5 based on 10 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]