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

219 questions
19
votes
4 answers

Pythonic way to find maximum absolute value of list

Given the following: lst = [3, 7, -10] I want to find the maximum value according to absolute values. For the above list it will be 10 (abs(-10) = 10). I can do it as follows: max_abs_value = lst[0] for num in lst: if abs(num) > max_abs_value: …
trojek
  • 3,078
  • 2
  • 29
  • 52
18
votes
2 answers

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
  • 321
  • 1
  • 2
  • 9
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
  • 263
  • 1
  • 3
  • 9
17
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
  • 9,977
  • 9
  • 47
  • 77
16
votes
1 answer

How do I compute the absolute value of a vector in Eigen?

How do I compute the absolute value of a vector in Eigen? Since the obvious way Eigen::VectorXf v(-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0); v.abs(); // Compute abs value. does not work.
Reed Richards
  • 4,178
  • 8
  • 41
  • 55
16
votes
3 answers

Why does std::abs return signed types

I'm getting warning for signed vs. unsigned comparison when I'm comparing a std::abs(int) against an unsigned. And indeed, std::abs returns signed values. Why was that choice made? It would have solved the problem of negative values whose…
akim
  • 8,255
  • 3
  • 44
  • 60
14
votes
11 answers

Finding minimal absolute sum of a subarray

There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.: A = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum I've started by implementing a…
NPS
  • 6,003
  • 11
  • 53
  • 90
14
votes
2 answers

Is there a safe way to get the unsigned absolute value of a signed integer, without triggering overflow?

Consider a typical absolute value function (where for the sake of argument the integral type of maximum size is long): unsigned long abs(long input); A naive implementation of this might look something like: unsigned long abs(long input) { if…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
12
votes
5 answers

Take the maximum in absolute value from different columns and filter out NaN Python

This was my try. For example df = pd.DataFrame({'a':[5,0,1,np.nan], 'b':[np.nan,1,4,3], 'c':[-3,-2,0,0]}) df.dropna(axis=1).max(axis=1,key=abs) Filters out well the NaN values but it gets 0 or negative values instead of the highes in absolute…
gis20
  • 1,024
  • 2
  • 15
  • 33
11
votes
1 answer

How would fabs(double) be implemented on x86? Is it an expensive operation?

High-level programming languages often provide a function to determine the absolute-value of a floating-point value. For example, in the C standard library, there is the fabs(double) function. How is this library function actually implemented for…
AlexG
  • 342
  • 4
  • 15
11
votes
5 answers

Integer absolute value in MIPS?

Do you have any simple ways to make a value in a register in MIPS as an absolute value?
aherlambang
  • 14,290
  • 50
  • 150
  • 253
11
votes
1 answer

jfreechart customize piechart to show absolute values and percentages

How can this compilable minimal code snippet example, which uses JFreeChart as plotting API, adapted in order to show both absoulte values AND percentages? I couldn't extract this information neither from any code snippet on the internet nor from…
kiltek
  • 3,183
  • 6
  • 47
  • 70
11
votes
4 answers

Absolute value in vb.net

How do you get the absolute value of a number in vb.net? Is there a function built in? I know I can simply code a function myself, but I want to know if there is one already there first. It seems so simple, I could probably make it in three lines,…
Cyclone
  • 17,939
  • 45
  • 124
  • 193
10
votes
8 answers

isn't there an operator in c to change the sign of a int float etc from negative to positive or vice versa?

trying to find absolute value and i thought there was a simple way to just invert the sign with '~' or something.
nickthedude
  • 4,925
  • 10
  • 36
  • 51
9
votes
3 answers

Absolute value for floats in core OCaml

I am in need of an absolute value function for floats in OCaml and the core language doesn't seem to possess one, so I wrote the following: let absF (f:float) = if f > 0.0 then f else (f *. -1.0);; which seems to work for positives but not for…
Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
1
2
3
14 15