Absolute Value

Posted on 2000-03-14
Medium Priority
Last Modified: 2010-04-02
How do you find the absolute value of a number, any number
Question by:SBuntin

Expert Comment

ID: 2617238
what do you mean by absolute?

Expert Comment

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?

Accepted Solution

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);
LVL 10

Expert Comment

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).

LVL 10

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

624 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