Solved

Converting a negative number to a positive number.

Posted on 2004-05-02
9
1,456 Views
Last Modified: 2010-04-17
Hi All,
I need to convert a negative number to a positive number.
int i=0,j=0;
i--;
if( j == 100)
{
   //I need to change the -i number to a +i number
   //if i = -1050 I need to change it to i = +1050 by using
   //the value of i.
   //i will be a random number
}
if(j== 50)
{
   //I need to change i back to the negative number
   //by using the value of i.
}

In other words check the negative value and change it
to it's positive counter part and vice versa.

Is there a simple way to do this?

Thank you very much for your time,
AJ.
0
Comment
Question by:arnold100
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 7

Expert Comment

by:madwax
ID: 10971701
Hi arnold100,

Maybe I misunderstood you question but can't you just multiple the number by -1?

if(number < 0)
{
    number = number * -1;
}


and vice versa if you want to convert a postivie number to a negative...
//jan
0
 
LVL 4

Expert Comment

by:SimonTay
ID: 10972009
Sounds good to me, or you can use the ABS(i) function in most languages, so

i=ABS(i) will make it positive always.
0
 

Author Comment

by:arnold100
ID: 10972023
Hi madwax,
i--;

if(i==-1080)//-1080 will be a random - number
{                //so i needs to be converted from it's
                 //negative side to it's positive side
                 //using the value of  i
     i = i*-1;//I tried with no luck.
}

When i is -1080, i needs to be converted to +1080.
In other words no mather what the value of -i is, it needs
to be fliped to it's +i side. Same value just + instead of -.
Hope this helps.

Thanks,
AJ.

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 7

Expert Comment

by:madwax
ID: 10972118
Hi again,

What programming language are you using? This will be nescessary to know if that below doesn't work...

i = i * (-1);

Good luck,
//madwax
0
 

Author Comment

by:arnold100
ID: 10972186
I am working in c++. I tried the abs(); function that SimonTay listed above and it works for negative to positive. Is there a function for positive to negative?

Thanks SimonTay and madwax,
Aj.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10973480
simply multiply the original value by -1

Negative become postive, Postive becomes negative

X = 12;
Y = -1 * X;  //Y = -12   now



X = -12;
Y = -1 * X;   // y = 12  now


Is that what you need?

AW
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10973616
to convert from positive to negative or from negative to positive simpy use the urnary - operator;

i = -i;

for(int i = -5; i < 5; i++)
{
  int opposite = -i;
  cout << opposite << endl;
}

OUTPUT:
5
4
3
2
1
0
-1
-2
-3
-4
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 125 total points
ID: 10973628
int i=0,j=0;
i--;
if( j == 100)
{
   //I need to change the -i number to a +i number
   if( i == -1050)
       i = -i;  // i now equals 1050;
}
if(j== 50)
{
   //I need to change i back to the negative number
   j = -j;  //j now equals -50
}
0
 

Author Comment

by:arnold100
ID: 10973877
Hi PerryDK,
The urnary is the answer. I had tried it before with what I thought
was no luck, but I tried it again now and you are 100% right. I run my program with it and I find that the number is changing from
positive to negative and then back in a split second. I have a bug in my code some where and now I have to track it down and fix it. I except your answer.

Thank you very much,
AJ.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question