Questions tagged [absolute-value]

Absolute value is a mathematical function that returns the non-negative value of a number without regard to its sign.

Absolute value is a mathematical function that returns the non-negative value of a number without regard to its sign.

For example, both 7 and -7 will have an absolute value of 7.

Many programming languages include a function for absolute value in their standard math libraries, often named abs() or similar.

More information: http://en.wikipedia.org/wiki/Absolute_value

183 questions
113
votes
6 answers

How to convert a negative number to positive?

How can I convert a negative number to positive in Python? (And keep a positive one.)
aneuryzm
  • 55,858
  • 96
  • 259
  • 471
96
votes
8 answers

Math.abs returns wrong value for Integer.Min_VALUE

This code: System.out.println(Math.abs(Integer.MIN_VALUE)); Returns -2147483648 Should it not return the absolute value as 2147483648 ?
user665319
  • 1,329
  • 2
  • 13
  • 13
77
votes
1 answer

Should I use np.absolute or np.abs?

Numpy provides both np.absolute and the alias np.abs defined via from .numeric import absolute as abs which seems to be in obvious violation of the zen of python: There should be one-- and preferably only one --obvious way to do it. So I'm…
Jonas Adler
  • 8,636
  • 2
  • 35
  • 71
55
votes
9 answers

Why use abs() or fabs() instead of conditional negation?

In C/C++, why should one use abs() or fabs() to find the absolute value of a variable without using the following code? int absoluteValue = value < 0 ? -value : value; Does it have something to do with fewer instructions at lower level?
Subhranil
  • 821
  • 1
  • 8
  • 21
52
votes
4 answers

Absolute value abs(x) using bitwise operators and Boolean logic

How does this work? The idea is to make abs(x) use bitwise operators for integers (assuming 32 bit words): y = x >> 31 (x + y) ^ y // This gives abs(x) (is ^ XOR)?
43
votes
15 answers

Which is the fastest way to get the absolute value of a number

Which is the fastest way to implement an operation that returns the absolute value of a number? x=root(x²) or if !isPositive(x): x=x*(-1) Actually this question can be translated as, how fast is an if (and why please). My college programing…
Diones
  • 1,335
  • 2
  • 18
  • 25
21
votes
4 answers

On the std::abs function

Is the std::abs() function well defined for ALL arithmetic types in C++11 and will return |x| with no problem of approximation? A weird thing is that with g++4.7, std::abs(char), std::abs(short int), std::abs(int), std::abs(long int) and…
Vincent
  • 50,257
  • 51
  • 171
  • 339
21
votes
10 answers

Finding absolute value of a number without using Math.abs()

Is there any way to find the absolute value of a number without using the Math.abs() method in java.
Theja
  • 714
  • 1
  • 7
  • 24
20
votes
7 answers

C safely taking absolute value of integer

Consider following program (C99): #include #include #include int main(void) { printf("Enter int in range %jd .. %jd:\n > ", INTMAX_MIN, INTMAX_MAX); intmax_t i; if (scanf("%jd", &i) == 1) …
hyde
  • 50,653
  • 19
  • 110
  • 158
20
votes
1 answer

abs() vs fabs() speed difference and advantage of fabs()

I ran some simple tests on abs() and fabs() functions and I don't understand what are the advantages of using fabs(), if it is: 1) slower 2) works only on floats 3) will throw an exception if used on a different type In [1]: %timeit abs(5) 10000000…
metabuddy
  • 518
  • 9
  • 19
17
votes
1 answer

Fastest way to compute absolute value using SSE

I am aware of 3 methods, but as far as I know, only the first 2 are generally used: Mask off the sign bit using andps or andnotps. Pros: One fast instruction if the mask is already in a register, which makes it perfect for doing this many times in…
Kumputer
  • 508
  • 5
  • 19
17
votes
8 answers

What is different about C++ math.h abs() compared to my abs()

I am currently writing some glsl like vector math classes in C++, and I just implemented an abs() function like this: template static inline T abs(T _a) { return _a < 0 ? -_a : _a; } I compared its speed to the default C++ abs from…
moka
  • 255
  • 1
  • 3
  • 9
16
votes
2 answers

mongodb - Find document with closest integer value

Let's assume I have a collection with documents with a ratio attribute that is a floating point number. {'ratio':1.437} How do I write a query to find the single document with the closest value to a given integer without loading them all into…
DeaconDesperado
  • 8,971
  • 6
  • 43
  • 75
15
votes
3 answers

Sort a list in python

I have this list [1,-5,10,6,3,-4,-9] But now I want the list to be sorted like this: [10,-9,6,-5,-4,3,1] As you can see I want to order from high to low no matter what sign each number has, but keeping the sign, is it clear?
14
votes
1 answer

abs 'implicit declaration...' error after including math.h

I used the abs() function and I added #include at the top of code. But I keep getting this error: hello.c:20:11: warning: implicit declaration of function 'abs' is invalid in C99 [-Wimplicit-function-declaration] int a =…
wakwakwak99
  • 181
  • 1
  • 1
  • 8
1
2 3
12 13