dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

isOdd(), isEven() #1229

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Methods isOdd()/isEven() seem appopriate for ___Math (e.g. IntMath) classes. 

It is easy to check if a number is odd or even, however:

1) There is a possibility of a mistake. For example, when checking a negative 
number, i%2 returns -1 for odd negative numbers [Java Puzzlers Item 1] 

2) It can be refined with bitwise operation, eg:
public static boolean isOdd(int i){
    return (i & 1) != 0;
}

Considering that there is often a need to check for odd/even and the existence 
of isPowerOfTwo() in ___Math classes, having isOdd() and/or isEven() might be 
appropriate.

Original issue reported on code.google.com by alex.kar...@gmail.com on 14 Dec 2012 at 3:58

GoogleCodeExporter commented 9 years ago
"seem appropriate" is not a strong enough argument to add a method to Guava.

I realize this computation is error-prone, but that's why we have, well, 
error-prone (http://error-prone.googlecode.com). Also we've found that isEven 
checks don't happen that often outside tests, and often are only used with 
positive numbers anyway.

Original comment by kevinb@google.com on 14 Dec 2012 at 5:02

GoogleCodeExporter commented 9 years ago
What about isPowerOfTwo() then?

Original comment by alex.kar...@gmail.com on 14 Dec 2012 at 5:06

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08