Solved

Absolute Value

Posted on 2000-03-14
5
210 Views
Last Modified: 2010-04-02
How do you find the absolute value of a number, any number
0
Comment
Question by:SBuntin
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 20 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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.

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

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 95
FMX TCameraComponent Problem 2 82
Why isn't object file created? 6 68
object oriented programming comparison 5 77
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

839 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