11
votes
8answers
8k views

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); is true. I understand that integer in Java is 32 bit and can't go above 2^31-1, but I can't understand why adding 1 to its MAX_VALUE ...
5
votes
2answers
132 views

Is there a library for HUGE integers [closed]

I am looking for a Java library that can handle truly huge numbers or suggestions as to how to implement this myself. We are talking way beyond BigInteger. How about 2^39614081257132168796771974655+1 ...
5
votes
3answers
2k views

Saturated addition of two signed Java 'long' values

How can one add two long values in Java so that if the result overflows then it is clamped to the range Long.MIN_VALUE..Long.MAX_VALUE? For adding ints one can perform the arithmetic in long ...
0
votes
2answers
56 views

php multiply strange integer overflow behavior

In scala(java) scala> 8218553819005469347L * 31 res75: Long = -3479248642764172867 But in php (5.5 / 64bit linux system) <?php echo (int)(8218553819005469347 * 31); it prints ...
1
vote
2answers
43 views

different overflow policy for Double and Integer. why?

this code System.out.println(Double.MAX_VALUE+12345 == Double.MAX_VALUE); System.out.println(Integer.MAX_VALUE+12345 == Integer.MAX_VALUE); returns true false Please clarify this difference.
1
vote
4answers
101 views

Java Hashcode gives integer overflow

Background information: In my project I'm applying Reinforcement Learning (RL) to the Mario domain. For my state representation I chose to use a hashtable with custom objects as keys. My custom ...
1
vote
3answers
76 views

Calculate Java Int Overflow

Is there a formula to calculate what the overflow of a Java int would be? Example: if I add 1 to Integer.MAX_VALUE; the answer is not 2147483648, but rather -2147483648. Question: if I wanted to ...
0
votes
3answers
53 views

Number overflow in Java

I am tring to implement C/C++ atoi function in Java, below is the code snippet for (int j = 0; j < s.length(); j++) { int digit = Character.digit(s.charAt(j), 10); if (sum ...
2
votes
3answers
191 views

How to detect overflow of “unsigned” long multiplication in Java?

Java does not have "unsigned" long values, of course, but sometimes signed long values are effectively treated as unsigned long values (the result of System.nanoTime(), for example). In this sense, ...
3
votes
2answers
109 views

Why does (int)(1.0 / x) where x = 0 results in In32.MinValue rather than Int32.MaxValue?

In Java, int x = 0; (int)(-1.0 / x) -> Integer.MinValue (int)(1.0 / x) -> Integer.MaxValue But in C#, int x = 0; (int)(-1.0 / x) -> Int32.MinValue (int)(1.0 / x) -> Int32.MinValue!! ...
0
votes
0answers
76 views

Why does Java int behave differently [duplicate]

I was reading a book by Robert Sedgewick and Kevin Wayne and I came across this. I don't seem to understand why no. b, d, e and f produced such results. a. System.out.println(a); //2147483647 b. ...
0
votes
2answers
95 views

How to get rid of this overflow error?

I am having a code, which gives incorrect output as i am working with big numbers. I want a solution to this that how could i improve this to accommodate big numbers. Which datatype i should use? ...
0
votes
2answers
268 views

Java, converting epoch to milliseconds

My flot graph is not displaying any lines, problem multiplying epoch by 1000 using Integer in Java? This is my raw data, stored in a .txt file epoch,value 1383229104,55559 1383229121,55559 ...
6
votes
6answers
584 views

Explanation of the safe average of two numbers

Whenever I need to average two numbers for an algorithm like binary search, I always do something like this: int mid = low + ((high - low) / 2); I recently saw another way to do it in this post, ...
15
votes
1answer
449 views

Can a non-empty string have a hashcode of zero?

By "non-empty", I mean in this question a string which contains at least one non-zero character. For reference, here's the hashCode implementation : 1493 public int hashCode() { 1494 int h ...
2
votes
4answers
596 views

Often big numbers become negative

Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type. ...
1
vote
2answers
114 views

Why is comparing ints safe in Java?

Why, given: int a = ..., b = ... ; is this unsafe: a - b but this is safe: a > b By safe I mean guaranteed not to be affected by overflows (I'm writing a Comparator of ints).
2
votes
1answer
87 views

Differences in Overflow Semantics Between C# and Java

Take the following piece of code: const float fValue = 5.5f; const float globalMin = 0.0f; const float globalMax = 5.0f; float vFactor = (float)(2e9 / (globalMax - globalMin)); int iValue = ...
3
votes
7answers
979 views

doubt with range of int variable

i have a doubt with the range of int value int x=2147483647; /*NO Error--this number is the maximum range of int value no error*/ int y=2147483648; /*Error--one more ...
5
votes
1answer
100 views

Strange implementation of Guava LongMath.checkedAdd [duplicate]

public static long checkedAdd(long a, long b) { long result = a + b; checkNoOverflow((a ^ b) < 0 | (a ^ result) >= 0); return result; } I am interested why boolean logical | is ...
0
votes
3answers
544 views

Why is my calculated percentage turning negative in Android SDK?

I am hoping that this is going to be a silly thing that I am missing, but I have been banging my head against the keyboard trying to figure out where I am going wrong. I am trying to update a ...
5
votes
2answers
148 views

What would happen if I were to make more references to Objects than 32 bits can account for?

So I just learned when you declare a variable of type Object ( i.e. Object a; ), a 32-bit space is allocated for that variable. Inside this variable/reference, there is a memory address to an actual ...
10
votes
3answers
5k views

What does BigInteger having no limit mean?

I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics): In the BigInteger class, I have no limits and there are ...
4
votes
2answers
227 views

Why do integer datatypes overflow silently rather than throwing exception

I have learnt(atleast in java) that integer/long values overflow silently and their values start over from minimum value on overflow rather than throwing any exception. I was using an external api ...
16
votes
5answers
344 views

Are fixed-width integers distributive over multiplication?

For three n-bit signed integers a, b, and c (such as 32-bit), is it always true that a * (b + c) == (a * b) + (a * c), taking into account integer overflow? I think this is language-independent, but ...
1
vote
1answer
415 views

Convert long string to integer without parseLong or parseInt

Here's is the situation ... i have a binary file which contains 32-bit binary strings of characters (e.g 1011011100100110101010101011010 ) and i want to convert this to integer ... I have already ...
3
votes
2answers
390 views

How to catch the Integer overflow error in Spring MVC?

I am using Spring MVC 3.0.5. Spring happily ignores an Integer overflow for a field that is mapped to an Integer. How can I report a proper error on that ?
66
votes
5answers
3k views

Is this a JVM bug or “expected behavior”?

I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't ...
1
vote
1answer
578 views

Why is Collatz Conjecture Program Not Working for Large Integers in Java

This is a program I made to simulate the Collatz Conjecture on Java: import java.util.*; public class Collatz { public static void main(String args[]){ Scanner raj= new Scanner(System.in); ...
2
votes
1answer
468 views

Silent long overflow while implementing Fibonacci with Callables

I am trying to implement the Fibonacci sequence using callables and seeded the initial values of my Fibonacci callable with 3,4,5,6 and 2000. The output I get is as follows: 3 5 8 13 ...
-2
votes
3answers
135 views

How to represent integers bigger as (10^6)! to use in equations to solve in java

How can we solve equations having N! constants in it , where N can be of range 1<=N<=10^6 BigInteger can only perform upto 128 bits right? Even when one do logarithm on both sides, it leaves ...
6
votes
3answers
953 views

How do I fix wrong numbers produced by integer overflow?

I had a bug that caused an integer overflow, resulting in wrong (negative) timestamps being written to the database. The code is fixed already, but I want to fix the wrong data, too. I thought, I ...
11
votes
3answers
455 views

Why doesn't compound assignment in Java catch overflow problems?

To my shock, it turns out that the following code will compile without even warnings: public void test() { int value = 2000000000; long increment = 1000000000; value += increment; } ...
3
votes
4answers
1k views

java arithmetic

why this code returns wrong value? int i=Integer.MAX_VALUE+1; long l=Integer.MAX_VALUE+1; System.out.println(l); System.out.println(i);
0
votes
2answers
115 views

How to find and handle binary/numerical overflow in J2ME

I am building a unit convertor program that uses the MathFP library. Typically unit conversion occurs in the formula of: U1 (unit1) * K (constant) = U2 (unit2) I want to be able to detect when the ...
0
votes
2answers
675 views

Linear Regression and Java Dates

I am trying to find the linear trend line for a set of data. The set contains pairs of dates (x values) and scores (y values). I am using a version of this code as the basis of my algorithm. The ...
7
votes
10answers
4k views

Java multiply operation behavior

I wrote a method to convert a given number from days to milliseconds: private long expireTimeInMilliseconds; ... public void setExpireTimeInDays(int expireTimeInDays) { expireTimeInMilliseconds = ...