Tech Off Thread

37 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

VB.Net How do i reverse the sign of an Integer?

Back to Forum: Tech Off
  • User profile image
    Lee_Dale

    I can't seem to do it Perplexed

  • User profile image
    Lee_Dale

    i can do it like this

    (myInt) - (myInt *2)

    is there a function in VB?

  • User profile image
    Yggdrasil

    leeappdalecom wrote:
    i can do it like this

    (myInt) - (myInt *2)

    is there a function in VB?


    A simpler way would be (myInt * -1).
    I had a look around the Math class, but couldn't find anything specific.

  • User profile image
    Anonymous.​Programmer

    I'm guessing, since I don't use Vb.Net, but I can't believe that you can't do this:

    MyInt = -MyInt

  • User profile image
    Lee_Dale

    Cool cheers guys, you would have though the Math class would have a function to do this, I'm a programmer I dont want to have to do maths manually thats what the computers for lol

  • User profile image
    figuerres

    leeappdalecom wrote:
    Cool cheers guys, you would have though the Math class would have a function to do this, I'm a programmer I dont want to have to do maths manually thats what the computers for lol


    the "standard" method is x*-1 as another post said.

    that bit of code is very small and if you built a function for it then the perfomance would suck... pushing and poping the stack etc...
    to do what is generaly about 2 or 3 machine level instructions.

    also the compiler can generate code from that expression for all kinds of data, byte, short int, long int, float, decimal and whatever.

    if you want the value "X" to have the inverse directly then you code:

    x =  x*-1

    where x is any valid data type such as an int.

    you can also use a binary operator if you want to get cute, invert the bits of an int and you have the same effect as I recall, been a while since I did that ... MASM for DOS back then.

  • User profile image
    AndyC

    figuerres wrote:
    

    you can also use a binary operator if you want to get cute, invert the bits of an int and you have the same effect as I recall, been a while since I did that ... MASM for DOS back then.


    Invert the bits then add 1 (two's complement arithmetic)

    Couldn't you use x = 0 - x, though?

  • User profile image
    Tensor

    errr.... has it been a long day or somthing?

    Dim myInt As Integer = 10

    Dim reversedInt As Integer

    reversedInt = -myint

  • User profile image
    Anonymous.​Programmer

    That's what I said Tensor.

    Can't believe this. It's the dumbest thread I've ever seen on here.  And there've been some pretty dumb threads.

    A function in System.Math to calculate the negative of an integer?  This has got to be trolling.

    Next you'll be wanting to see

    result = Math.Multiply(Math.Subtract(Math.Add(a, b), c), d);

    instead of:

    result = ((a + b) - c) * d;

    Who's going to submit this thread to the daily wtf then?

  • User profile image
    Lee_Dale

    lol chill out

  • User profile image
    TommyCarlier

    You could use a SQL Server database that has a precalculated table with all the possible integers and their negative. Then you could build an ASP.NET webservice using SOAP to access the database. Then you can consume this web service in your application. How's that for a solution?

  • User profile image
    littleguru

    leeappdalecom wrote:
    Cool cheers guys, you would have though the Math class would have a function to do this, I'm a programmer I dont want to have to do maths manually thats what the computers for lol


    The math function is...

    f(x) = x * -1

    Wink

  • User profile image
    Sven Groot

    TommyCarlier wrote:
    You could use a SQL Server database that has a precalculated table with all the possible integers and their negative. Then you could build an ASP.NET webservice using SOAP to access the database. Then you can consume this web service in your application. How's that for a solution?

    It needs more javascript.

  • User profile image
    Larsenal

    TommyCarlier wrote:
    You could use a SQL Server database that has a precalculated table with all the possible integers and their negative. Then you could build an ASP.NET webservice using SOAP to access the database. Then you can consume this web service in your application. How's that for a solution?


    Why not just fiddle with the actual bits?

  • User profile image
    Sven Groot

    Larsenal wrote:
    Why not just fiddle with the actual bits?

    That's not "Enterprise" enough. I still say it needs more javascript. Maybe some BizTalk too. Don't you read the daily WTF? Tongue Out

  • User profile image
    AndyC

    Sven Groot wrote:
    
    That's not "Enterprise" enough. I still say it needs more javascript. Maybe some BizTalk too. Don't you read the daily WTF?


    Don't forget to use XML to store everything.

    Mmmmm, enterprisey </homer>

  • User profile image
    Sven Groot

    AndyC wrote:
    
    Sven Groot wrote: 
    That's not "Enterprise" enough. I still say it needs more javascript. Maybe some BizTalk too. Don't you read the daily WTF?


    Don't forget to use XML to store everything.

    Of course, you have to store the XML as a string in your database. And then generate HTML from it in an sproc. If you're using Oracle, even better! Doing it in PL/SQL should be even more fun than T-SQL.

  • User profile image
    harumscarum

    Don't forget serialization and a mention of SOA.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.