absolute-value

Sort a list in python

有些话、适合烂在心里 提交于 2019-12-17 18:55:50
问题 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? 回答1: Use abs as key to the sorted function or list.sort : >>> lis = [1,-5,10,6,3,-4,-9] >>> sorted(lis, key=abs, reverse=True) [10, -9, 6, -5, -4, 3, 1] 回答2: Use: l.sort(key= abs, reverse = True) Lists can be sorted using the sort() method. And the sort method has a

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

旧城冷巷雨未停 提交于 2019-12-17 17:32:27
问题 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 professors always told me to avoid if s for they are extremely slow, but I always forgot to ask how slow and why. Does anybody here know? 回答1: Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as

Math.abs returns wrong value for Integer.Min_VALUE

試著忘記壹切 提交于 2019-12-17 10:12:18
问题 This code: System.out.println(Math.abs(Integer.MIN_VALUE)); Returns -2147483648 Should it not return the absolute value as 2147483648 ? 回答1: Integer.MIN_VALUE is -2147483648 , but the highest value a 32 bit integer can contain is +2147483647 . Attempting to represent +2147483648 in a 32 bit int will effectively "roll over" to -2147483648 . This is because, when using signed integers, the two's complement binary representations of +2147483648 and -2147483648 are identical. This is not a

jfreechart customize piechart to show absolute values and percentages

本小妞迷上赌 提交于 2019-12-17 07:49:16
问题 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 the JFreechart manual itself. The code snippet produces a pie chart showing only percentages. The absolute values in my case also matter, so i need to display them right under the percentages. Here is the code: (Note it lacks the imports) public

jfreechart customize piechart to show absolute values and percentages

不打扰是莪最后的温柔 提交于 2019-12-17 07:48:12
问题 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 the JFreechart manual itself. The code snippet produces a pie chart showing only percentages. The absolute values in my case also matter, so i need to display them right under the percentages. Here is the code: (Note it lacks the imports) public

mean of absolute value of groupby object pandas

我的梦境 提交于 2019-12-14 03:58:42
问题 I want to compute the mean of the absolute value of a grouped object. I.e. grouped = df.groupby([pd.TimeGrouper(3MS)]) dct['x'] = grouped['profit'].agg('mean') / grouped['cost'].abs().agg('mean') However, the above code results in an error. I have tried various variants of the above code but so far all result in errors. There must be a simple way to do this. Update: This is the dataframe that is grouped vi pd.TimeGrouper(3MS). I want to take the absolute value of column cost 1, and then

“generic method” “absolute value” java

风格不统一 提交于 2019-12-13 02:57:15
问题 I want to make a stupid example for my students, but a don´t know if I can do what I´m thinking I want to do the abs method with generics. My idea is something similar to this: public class MiMath { public static <T extends Number> T abs(T objeto) { if (objeto.doubleValue()<0) return (T) -objeto.doubleValue(); else return objeto; } } in this linea return (T) -objeto.doubleValue(); eclipse says that (T) is not a Type 回答1: The problem is that what you are doing here with the (T) is not really a

Is there a fast fabsf replacement for “float” in C++?

瘦欲@ 提交于 2019-12-11 08:23:49
问题 I'm just doing some benchmarking and found out that fabsf() is often like 10x slower than fabs() . So I disassembled it and it turns out the double version is using fabs instruction, float version is not. Can this be improved? This is faster, but not so much and I'm afraid it may not work, it's a little too lowlevel: float mabs(float i) { (*reinterpret_cast<MUINT32*>(&i)) &= 0x7fffffff; return i; } Edit: Sorry forgot about the compiler - I still use the good old VS2005, no special libs. 回答1:

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

南笙酒味 提交于 2019-12-09 05:14:00
问题 I am currently writing some glsl like vector math classes in C++, and I just implemented an abs() function like this: template<class T> static inline T abs(T _a) { return _a < 0 ? -_a : _a; } I compared its speed to the default C++ abs from math.h like this: clock_t begin = clock(); for(int i=0; i<10000000; ++i) { float a = abs(-1.25); }; clock_t end = clock(); unsigned long time1 = (unsigned long)((float)(end-begin) / ((float)CLOCKS_PER_SEC/1000.0)); begin = clock(); for(int i=0; i<10000000;

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

只愿长相守 提交于 2019-12-08 16:23:53
问题 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 x86 targets? What would actually be happening "under the hood" when I call a high-level function like this? Is it an expensive operation (a combination of multiplication and taking the square root)? Or is the result found just by removing a negative