?
Solved

how to convert -1 to 1

Posted on 2005-04-27
5
Medium Priority
?
196 Views
Last Modified: 2010-04-01
I can't remember the function that takes negative numbers and gives you the positive ones in return.  example:

 if I have a -2, run the func, and it returns a 2.

PT
0
Comment
Question by:ptrennum
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 13880433
Use 'abs()'
0
 
LVL 86

Expert Comment

by:jkr
ID: 13880453
Ooops, the sample from the docs :o)

/* ABS.C: This program computes and displays
 * the absolute values of several numbers.
 */

#include  <stdio.h>
#include  <math.h>
#include  <stdlib.h>

void main( void )
{
   int    ix = -4, iy;
   long   lx = -41567L, ly;
   double dx = -3.141593, dy;

   iy = abs( ix );
   printf( "The absolute value of %d is %d\n", ix, iy);

   ly = labs( lx );
   printf( "The absolute value of %ld is %ld\n", lx, ly);

   dy = fabs( dx );
   printf( "The absolute value of %f is %f\n", dx, dy );
}

BTW, for float/double values, there's 'fabs()', e.g.

/* ABS.C: This program computes and displays
 * the absolute values of several numbers.
 */

#include  <stdio.h>
#include  <math.h>
#include  <stdlib.h>

void main( void )
{
   int    ix = -4, iy;
   long   lx = -41567L, ly;
   double dx = -3.141593, dy;

   iy = abs( ix );
   printf( "The absolute value of %d is %d\n", ix, iy);

   ly = labs( lx );
   printf( "The absolute value of %ld is %ld\n", lx, ly);

   dy = fabs( dx );
   printf( "The absolute value of %f is %f\n", dx, dy );
}





0
 
LVL 86

Expert Comment

by:jkr
ID: 13880469
BTW, as an 'academic excercise, you could

int my_abs ( int n) { return n < 0 ? n * -1 : n;}
0
 

Author Comment

by:ptrennum
ID: 13880471
Thanks JKR!!
I wish I had an office down the hall from you, but I'd probably drive you crazy.

PT
0
 
LVL 86

Expert Comment

by:jkr
ID: 13880511
LOL, others do that already on a regular basis :o)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 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