To prevent being bitten by artifacts of floating point arithmetic, you might want to use an integer loop variable and derive the floating point value you need inside your loop:
for (int n = 0; n <= 40; n++) {
double i = 0.25 * n;
// ...
}
Answer from rsp on Stack OverflowTo prevent being bitten by artifacts of floating point arithmetic, you might want to use an integer loop variable and derive the floating point value you need inside your loop:
for (int n = 0; n <= 40; n++) {
double i = 0.25 * n;
// ...
}
You can use i += 0.25 instead.
You are using the primitive double and class type Double and there is a conversion going on between them and as they're both floating point you're seeing floating point imprecision.
The lack of precision of double types is well-known. Usually, a solution is to use BigDecimal instead. However in your specific case, since you're stepping 0.1 at each iteration, you could also work with integers, and divide them by 10 when needed.
Note that you should also change the way you store the data. That is, use:
TreeMap<BigDecimal, Integer>
(or TreeMap<Integer, Integer>) instead of TreeMap<Double, Integer>
See also the documentation of BigDecimal: http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html It's not as easy to use as double, but much safer.
java - How to use double in a for loop rather than an int? - Stack Overflow
Java loop for double working - Stack Overflow
java - Why would you use double variables at all in a for-loop - Stack Overflow
java - Creating a for loop to double the previous value - Stack Overflow
Videos
Rest assured that i is a double, but i will never be 2.29195.
Using a double in a for loop requires careful consideration since repeated addition of a constant to a floating point can cause accumulating total to "go off" due to inexact conversions from decimal to binary. (Try it with i += 0.1 as the incrementation and see for yourself). But for small whole numbers you'd be absolutely fine although then the use of a double is, of course, pointless.
If you want to effectively count up by units of 0.00001 then use something like
for (int i = 100000; i <= 500000; ++i){
double f = i * 0.00001;
}
Then f is as close to the number that you really want as an IEEE754 double allows.
You could do something like this:
public static void FindTheDouble() {
double LookFor = 2.29195;
for (double i = 1.0; i <= 5.0; i+=0.00005) {
System.out.println(i);
if (i == LookFor) {
System.out.println();
System.out.println("Found the double: "+LookFor);
break;
}
}
}
Your break statement is correct.
Your equality statement might fail with doubles. Instead you want to do something like this:
if (Math.abs((i - LookFor)) < 0.0000001)
The double can have extraneous values at extended decimal places that can interfere with the success of your equality comparison. Instead it is better to check that the two double values are within some degree of precision that is acceptable to you.
When you type one character and then press the enter key (<-|), the system delivers two characters to your program; hence 0 and 1 are printed after typing the first 'l' and 2 and 3 after typing the second 'l'.
You might print the codepoint of the character read, e.g.,
for (int i = 0; i < 4; i++){
char a = (char) System.in.read();
System.out.println( Character.getNumericValue( a ) );
}
in the loop to see what is going on.
Your code is also reading the newline character('\n') as you are hitting the enter key('\n') after every input('1','2','3' etc).
If you type one character and press enter key,System.in.read() will read two characters as it also reads the newline character.
Re-factored your code a bit. Enter all the values in one line(do not press enter key until you enter all the values). This will solve your problem.
for (i = 0; i < 4; i++)
{
a = (char)System.in.read();
System.out.println(a);
}
Input 4567
Output
4
5
6
7
The ideone code link is here http://ideone.com/IYcjyX. Hope it helps.
It's perfectly fine to do, especially if you want to loop through fractional values or increment by fractional values.
However I just wanted to add something to the other answers, which is that if you employ this technique, do not compare with == for the condition to end the loop. Due to precision errors, this can easily result in an infinite loop in spite of the best intentions. That's why your book's code uses <=.
It's not a good idea to use floating point values as for-loop variables because of subtleties in arithmetic and comparison. Your loop might not iterate through the values you expect because of rounding errors. An out-by-one error is likely.
For example, this loop
for (double x=0.0; x < 100.0; x += 0.1)
might be expected to pass through the values 0.0, 0.1, 0.2,... but because of rounding errors passes through slightly different values.
You can't add items to most standard implementations of List while iterating over them, unless you create an implementation that allows it!
ArrayList does not, however, see javadoc. Nor do most* (perhaps all) of the Java Collections Frameworks List implementations.
A solution would be to create a new list, temp, before iterating, add elements to temp while you iterate, and then add all of the elements in temp to the first.
Edit: used addAll(temp), thanks @Michael Easter
List<Road> temp = new ArrayList<Road>();
for(Road ra : roads){
for (Road rb : roads){
temp.add(xyz);
}
}
roads.addAll(temp);
If you use a ListIterator<E> instead, you will be able to add. The reason you are getting the exception is b/c of this(from the javadocs):
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
You cannot modify the list itself directly, but through the iterator, you may. The base Iterator<E> class does not have an add method, but ListIterator<E> does, which is what you are getting when you call the obj.listIterator() anywqay.
We have a practical exam coming up on my uni regarding nested for loop in which I struggle to. Can you help me understand how does it work. I know that it is a loop within a loop but I can't logically decide what values to put in the inner loop (the range and the increment). I can't wrap my head around it. ๐ญ
My professor typically give us exercises like these:
Exercise 1: 9 7 7 7 5 5 5 5 5 3 3 3 3 3 3 3 Exercise 2: 8 6 6 4 4 4 2 2 2 2 0 0 0 0 0 Exercise 3: 4 4 4 4 4 4 4 4 3 3 3 3 3 3 2 2 2 2 1 1 Exercise 4: 2 2 2 2 2 2 4 4 4 4 6 6 Exercise 5: 1 1 1 1 1 5 5 5 9 Exercise 6: 5 5 10 10 10 10 15 15 15 15 15 15 Exercise 7: 6 444 22222 Exercise 8: 6 444 2222