Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Absolute Value

Posted on 2000-03-14
5
Medium Priority
?
215 Views
Last Modified: 2010-04-02
How do you find the absolute value of a number, any number
0
Comment
Question by:SBuntin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2617238
what do you mean by absolute?
0
 

Expert Comment

by:kpcapel
ID: 2617325
Are you looking for the non-fractional part of a number?  You can cast a floating point number to an integral type.  

double x = 9999.999;

int getAbsolute(double n){
      
   return (int)n;
}

If you pass x to getAbsolute, it returns 9999.  It couldn't be that simple, I assume there's a bit more to what you're trying to do.  Can you elaborate on the question?
0
 
LVL 1

Accepted Solution

by:
Binder earned 80 total points
ID: 2617330
  If the number is an integer use

        absolute = abs(number);

   If the number is long use

        absolute = labs(number);

   If the number is float use

        absolute = fabs(number);
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2619803
For any number, do something like this

template <typename T> inline T ABS(T t) {
  return -t < t ? t : -t;
}

In fact, this works for ANY type for which '<' and unary '-' are defined

(This is superior to return t<0?-t:t as it does not require a comparison with the number zero).

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2621545
guess you didn't want the general single solution that works for any number :-(
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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