问题
I'm reading the following code(in ch. 5, inheritance, of Horstman's Core Java):
double dx = (to - from) / (n-1);
for (double x = from; x <= to; x += dx)
{
double y = (Double) f.invoke(null, x);
System.out.printf(%10.4f | %10.4f%n" + y, x, y);
}
Why would you use double variables at all in a for-loop? And what is invoke used for? thanks
Edit: f is something related to Field
回答1:
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 <=.
回答2:
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.
回答3:
For example, if you want to loop from 1.5 up to no more than 8 in increments of 0.11, you would need double variables in the for loop.
回答4:
There's no reason not to. Typically you loop with integers, but there's no reason you couldn't do the same thing with doubles applying an arbitrary instruction to the end of the loop iteration like x = x*1.2 or something like that. Also we need to know what f is to tell you what invoke does ;)
回答5:
This is a very bad idea. For large n (relative to to - from), dx will be so small that x += dx won't change the value of x, leaving you with a loop condition that will always be true, and you'll be stuck in the loop.
Do not use imprecise data (such as float and double) to control the flow of your program.
Instead, you should use an integer (e.g. ranging from 0 inclusive to n exclusive) to control the loop. There are a few ways to do it (and most of them are wrong when it comes to handling rounding errors), but at least you can be sure that your loop will terminate.
Regarding your question about invoke: I think f is a Method rather than a Field. java.lang.reflect is a set of classes for reflection. Using reflection, the programmer made it so that f points to a method, and invoke can be used to call that method. Since it's being invoked with null as the first argument (which would normally specify what this is), f must refer to some static method in order to succeed.
来源:https://stackoverflow.com/questions/17373965/why-would-you-use-double-variables-at-all-in-a-for-loop