math « Integer « Java Data Type Q&A

1. Fastest way to determine if an integer's square root is an integer

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer). I've done it the easy way, by ...

2. getting the bottom 16 bits of a Java int as a signed 16-bit value

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer.

public class SignExtend16 {
 public static ...

3. Pollard rho integer factorization

I am trying to implement Pollard Rho integer factorization in C/C++.Google gives me a Java implementation of the problem here. I don't know Java that well,so what I came up ...

4. Java arithmetic int vs. long

I've done some searching on google, but I can't seem to find what I'm looking for. I'm trying to find out some detailed information on the way arithmetic works in Java. ...

5. how to get a float from int

i has a int:

int f=1234;//or f=23 f=456 ...
i want to get:
float result=0.1234; // or 0.23 0.456 ...
dont useing:
float result = parseFloat ("0."+f);
what's best way to do? thanks

6. float, double and int math in Java

I had some doubts/questions about the math in Java, so I made a small program a screenshot of which you can see in this link:
How are such numbers even ...

7. Any trap which we should beware of Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE)

I realize the below code holds true

Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE)
This is because when we negate -2147483648, it should become +2147483648. Since the maximum positive integer can be represented in Java ...

8. What happens when you increment an integer beyond its max value?

In Java what happens when you increment an int (or byte/short/long) beyond it's max value ? Does it wrap around to the max negative value ? Does AtomicInteger.getAndIncrement() also behave in the ...

9. Are arithmetic operations on double variables with integer values exact?

Let's say I've got two integer values stored in double variables, e. g.:

double x = 100.0;
double y = 7.0;
May I safely assume that any arithmetic operation on these two double variables ...

10. Math.abs returns wrong value for Integer.Min_VALUE

This code:

Returns -2147483648 Should it not return the absolute value as 2147483648 ?

11. Setting a limit to an int value

I want to set a limit to an int value I have in Java. I'm creating a simple health system, and I want my health to stay between 0 and 100. ...

12. What is the best way to separate double into two parts "integer & fraction" in java

I have tried to separate 5.6 (for example) by the following method:

private static double[] method(double d)
    int integerPart = 0;
    double fractionPart = 0.0;

13. How to set decision variables types like binary, int, double in Apache Commons Math SimplexSolver?

How to set decision variables types like binary, int, double in Apache Commons Math SimplexSolver? The output of the program below is this:

I want decision variables to be of ...

14. Java divison with two integer operands not working?

For some reason my math just returns 0. The value are set, I have checked.

int currentSize = 4079;
int totalSize = 500802;

int percentage = ((currentSize/totalSize) * 100);
Percentage always equals percentage. Why?

15. How to calculate integer expression in Java

Suppose, I need to calculate 3 * (2 / 3) = 2 but 3 * (2 / 3) = 0 in integer arithmetic. So, it looks like I should use floating-point ...

16. x = (int) Math.ceil[ x/2 ] + Math.floor[ x/2 ]

There isn't a "mathematical" proof because you aren't asking about a mathematical concept, you're asking about how Java implements things. However there's a logical proof which would parallel the mathematical proof of that question. Assuming your "x" is an integer, there are four possibilities: x can be odd or even, and it can be negative or non-negative. Consider each of them ...

17. Performing Math on Integer Class

Does anyone know of an efficient way to perform math on an Integer class or a better way to store an integer value as an object that is mutable? I have a TreeMap that looks like this ... What is the best approach? public TreeMap adjustTreeMap(String key, TreeMap treeMap) { Integer count; if(treeMap.containsKey(key)) { count = treeMap.get(key).intValue(); int i = ...

18. simple integer generation using java math classes

I want to generate a integer which should be combination of 2 integer and one int and anytime I call generate it should generate same value for same input and for different combinations it should generate different value. for example I passed to the service two integers 24, 25 and one int 76 assume it generated 242576 I restart jvm to ...