Tagged Questions
-1
votes
0answers
25 views
why numeric overflow isn't an exception in java [duplicate]
Numeric overflow is a potential risk, because we may get a wrong result during runtime.
So why not design the numeric overflow as an exception in Java Language ?
int x=Integer.MAX_VALUE;
x++;
System....
2
votes
2answers
66 views
Java integer overflow
I'm totally new to Java and I'm implementing a simple function to convert string to integer on Leetcode.
public int myAtoi(String str) {
if(str.length() == 0){
return 0;
}
str = ...
3
votes
2answers
55 views
Overflow detection when subtracting 2 longs
I have been going through Apache's common math libs Link
In the below code snippet lines A, B, C doesn't make sense to me. Can someone shed some light on this ?
public static long subAndCheck(long a,...
0
votes
2answers
40 views
Increment (overflow) Short.MAX_VALUE to zero, not Short.MIN_VALUE
I'm working on a 'tabindex' defect in an application that works fine in Firefox and Chrome, but not Internet Explorer.
The 'tabindex' is set through a JSP by a static 'int', which is incremented each ...
0
votes
1answer
76 views
C# vs Java overflow checking crazy performanse deviation
I am trying to measure time difference between checked and unchecked integer overflow in Java and C#.
I have noticed that checked Java code gets faster and faster while C# doesn't. Why is that?
...
1
vote
4answers
193 views
Java finding the midpoint between two integers
I'm attempting to find the integer midpoint between two integers. For example mid(2,3) would be 2, not 2.5. I had the below which works fine, but i'd like to work with numbers from MIN_VALUE to ...
0
votes
2answers
78 views
What datatype to use for 40 digit integers in java
For example
first number = 123456.....40 digits
second number = 123456.....40digits
Then I need to store the number
third = first * second;
after that I need to print third and again I need to ...
0
votes
3answers
110 views
Why isn't overflow checked by default
I found some questions on SO about checking operations before executions for over/underflow behavior. It seems that there are ways to do this quite easy. So why isn't there an option to automatically ...
3
votes
3answers
42 views
The effect of assigning parameter types on integer overflow
I am having difficulty of understanding the behavior of the java in the following scenario. For example, I have a multiply method, which simply multiplies two int value and print the result to the ...
-2
votes
2answers
39 views
Why the program is not throwing an exception when there is an overflow or underflow [duplicate]
When the maximum value of integer is exceeded (overflow) then the minimum value is assigned to it.
Ex- System.out.println(Integer.MAX_VALUE+1); Output: -2147483648
Here instead of throwing an ...
0
votes
4answers
61 views
How to find sum of Primes below 2000000?
public static void main(String[] args) {
int sum = 2;
int isPrime;
for(int x = 3; x < 2000000; x+=2){//x is the number we check for if it is a prime
isPrime = 0;
int y = ...
11
votes
1answer
175 views
Do the C# and Java specifications spell out the same behavior on signed integer overflow?
In C and C++, the behavior of signed integer overflow or underflow is undefined.
In Java and C# (unchecked contexts), the behavior seems to be defined to an extent.
From the Java specification, we ...
19
votes
4answers
527 views
How to compare two nano time values? [javadoc confusion]
I have read the javadoc for System.nanoTime() and it all seems clear. Until I reached the final paragraph:
To compare two nanoTime values
long t0 = System.nanoTime();
...
long t1 = ...
13
votes
4answers
235 views
How to detect overflow on power in Java
I know that java.lang.Math provides a set of static methods to perform some operations (sum, difference, multiply, increment, decrement, negate, toInt), throwing an ArithmeticException on overflow.
...
2
votes
1answer
63 views
Sudden infinite loop above certain input argument?
While learning Java I'm redoing some of the Project Euler problems.
This is about Problem 14 - Longest Collatz sequence: https://projecteuler.net/problem=14
My programm runs just fine for a lower ...
6
votes
1answer
227 views
Why does this multiplication integer overflow result in zero?
After answering this question, I was confused about why the overflowing integer in this code resulted in 0 instead of a negative number. It's strange, why such an exact number? Why 0?
public class ...
10
votes
3answers
867 views
Can doubles or BigDecimal overflow?
Java 8 gave us Math.addExact() for integers but not decimals.
Is it possible for double and BigDecimal to overflow? Judging by Double.MAX_VALUE and How to get biggest BigDecimal value I'd say the ...
0
votes
2answers
59 views
Understanding an overflow issue in Java
Given a sorted integer array without duplicates, return the summary of
its ranges for consecutive numbers.
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
I proposed the ...
1
vote
1answer
67 views
BigDecimal division breaks down when dividing by factorials
I'm writing a brute-force solution to a combinatorics problem that involves dividing by very large numbers. I turned to BigInteger and BigDecimal to handle these, but I've come upon an error when the ...
27
votes
3answers
139 views
Why is this long overflowing to -1, instead of the minimum value for the type?
I have the following code that returns the number of nodes in a tree when a complete Binary Tree is layer layers tall:
public static long nNodesUpToLayer(int layer) {
if (layer < 0) throw ...
17
votes
6answers
962 views
Overflow occurs with multiplication
long m = 24 * 60 * 60 * 1000 * 1000;
The above code creates overflow and doesn't print the correct result.
long m2 = 24L * 60 * 60 * 1000 * 1000;
long m3 = 24 * 60 * 60 * 1000 * 1000L;
The above 2 ...
4
votes
2answers
81 views
Do C# and Java longs form a commutative ring?
A ring is a standard mathematical structure describing objects which can be added and multiplied. Do C# and Java signed longs obey all the properties of a ring? For example, is multiplication by ...
2
votes
1answer
104 views
Why is this Java long overflowing? [closed]
This is very weird.
A class I wrote has the following data member:
final static long MAX_FILE_SIZE_BYTES = 50000000000L;
at one point in my code the following block is run
System.out.println("...
0
votes
3answers
54 views
Two Ways to Interpret Integer Overflow
I have read here (http://stackoverflow.com/a/27762490/4415632) that when integer overflow occurs, the most significant bits are simply cut off.
However, I have also read here (http://stackoverflow....
0
votes
2answers
65 views
Overflow while bit shifting in java
I have situation which I dont undestand.
When I do
System.out.println(1<<30);
I got very big positive number 1073741824.
But when I do
System.out.println(1<<31);
I got very low ...
0
votes
1answer
183 views
How to shorten method that checks if multiplying gives overflow
For class I have to write a method that checks if multiplying a certain rational number with an integer results in an overflow or not.
I have written following code and it works but I have a feeling ...
131
votes
9answers
27k views
Why does Java think that the product of all numbers from 10 to 99 is 0?
The following block of codes gives the output as 0.
public class HelloWorld{
public static void main(String []args){
int product = 1;
for (int i = 10; i <= 99; i++) {
...
0
votes
1answer
367 views
Reverse of a number taking care of boundary conditions
I'm trying to write a simple method in Java that return the reverse of a number (in the mathematical way, not string-wise). I want to take care of boundary conditions since a number whose reverse is ...
0
votes
1answer
84 views
Write a function that prints out every digit of n^100
I was asked to write a Java function that takes an integer, n, and prints out the value n^100.
I have no idea how to approach this. I know by conventional means it would overflow as n grows. Answers ...
0
votes
3answers
302 views
Is there any other way to implement “overflow safe” arithmetic operations in Java other than overriding arithmetic operators?
So here is my question. Since I learnt Java, I have been aware that arithmetic overflow and underflow can exist and java compiler will not complain it to you. Now I come up with a Operation class ...
0
votes
2answers
126 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
54 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
1k 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
132 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
473 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 <...
3
votes
2answers
134 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!!
...
-1
votes
0answers
83 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. ...
2
votes
3answers
774 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, ...
0
votes
2answers
213 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
424 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
1383229787,...
6
votes
6answers
2k 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, but ...
20
votes
1answer
2k 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 ...
1
vote
2answers
140 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
115 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 = (int)((...
5
votes
1answer
129 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 used ...
0
votes
3answers
2k 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 ...
6
votes
2answers
192 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 ...
7
votes
4answers
5k 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.
...
4
votes
2answers
156 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
2answers
502 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 ...