recursion : moron-level question

R

Roedy Green

private void FindApproximateZeroRecursive(Double min, Double max, Double
last)

Normally calculations are done with doubles not Doubles.

You would only use a Double if you needed to store the result in a
collection.
 
S

Stefan Ram

The variable "last" is null directly before the
return-statement is executed because it was initialized to be
"null" and not changed afterwards.
 
J

Jason

Hiya,

can anyone tell me why last == null?

private void FindApproximateZeroRecursive(Double min, Double max, Double
last)
{
last = new Double( (min.doubleValue()+max.doubleValue())/2 );
do {
if (Function(min.doubleValue()) * Function(last.doubleValue()) <
0)
max = last;
else
min = last;
} while ( IsApproximateZero(last.doubleValue()) );
}

private double FindApproximateZero()
{
// Use object wrappers so that values can be passed by reference. -
doesn't work :(
Double min = new Double(0);
Double max = new Double(Double.MAX_VALUE);
Double last = null;

FindApproximateZeroRecursive(min, max, last);
return last.doubleValue();
}

in c# I would just have put a ref keyword in front of each parameter and hey
presto!.

thanks,
-jason.
 
J

Jason

hmmm.. silly me: that's not a recursive function! but still, why last ==
null after executing FindApproximateZero?
 
R

Ryan Stewart

Jason said:
Hiya,

can anyone tell me why last == null?

private void FindApproximateZeroRecursive(Double min, Double max, Double
last)
{
last = new Double( (min.doubleValue()+max.doubleValue())/2 );
do {
if (Function(min.doubleValue()) * Function(last.doubleValue()) <
0)
max = last;
else
min = last;
} while ( IsApproximateZero(last.doubleValue()) );
}

private double FindApproximateZero()
{
// Use object wrappers so that values can be passed by reference. -
doesn't work :(
Because values are never passed by reference. You're passing object
references by value. And they're immutable objects at that, so you can never
pass a reference to one into a method and expect it to have a different
state after the method returns.
Double min = new Double(0);
Double max = new Double(Double.MAX_VALUE);
Double last = null;

FindApproximateZeroRecursive(min, max, last);
return last.doubleValue();
}
First, as Roedy pointed out, you should be using double, not Double. Second,
your method should return a double, not void. Then you would code:
double last = findApproximateZeroRecursive(min, max);

You seem to lack an understanding of some basic Java principles. I suggest
reading a bit more about the language and posting simple problems like this
to comp.lang.java.help.
 
R

Roedy Green

Is there some other way that avoids memory copies?

If you want to record a history of current, previous lowwater and
highwater marks, I see no way of doing it other than maintaining three
mutable fields in a object.

If you were doing something like maintaining the sum or max of a list,
you could compute it fresh each time or cache it with a variety of
procrastination schemes.
 
R

Roedy Green

They pay a big penalty for that feature. You can't tell just by
looking at the invoking code if any of the local variables will
suddenly change value on you at any method call that references them.

Just that POSSIBILITY really slows down code comprehension.

To do it in a maintainable way, the invocation should look different
too, with a an explicit ref, or with the IDE warning you a parameter
is mutable with some special colour coding or font.

See http://mindprod.com/projects/scid.html
 
J

Jason

thanks all...

I was using Double's in the hope that the doubleValue inside them would be
changed - in effect returning values by reference. I didn't know Double's
were immutable: I guess it was pretty obvious, but I didn't see it.

The reason I wanted to return values by reference rather than using the
return type of the function is that I wanted the modified values of both min
& max. From Jim Cochraine's post i gather that the only way to achieve this
would be to wrap the doubles up in a class and pass that backwards and
forwards.

Is there some other way that avoids memory copies?

thanks again,
-jason.
 
J

Jason

it does :) but this isn't a c# forum, so I won't linger on.
btw: http://mindprod.com/projects/scid.html wow! talk about forward
thinking!
Visual Studio does many of the simpler features you described - the
model-driven architecture bit is definately way better. But then, the
Rational boys were always way up front. I wanted to build my own MDA system
a while back but gave up when I realised the complexity involved and
compared it to the amount of free time I have :p I see you've implemented
pretty impressive looking one already - being a commercial product I bet
that took some time and a few extra hands!

thanks for posting,
-jason.
 
A

Andrew Thompson

"All truth passes through three stages. First, it is ridiculed.
Second, it is violently opposed. Third, it is accepted as being
self-evident."
~ Arthur Schopenhauer

Fourth, it is considered passe. Fifth, it
is discredited and passed off as "Old Wive's
Tales". Sixth it passes into the realm of
legends.. only to be later..

Rediscovered, ready to enter stage one.

;-)
 
M

Mark McConnell

Jason said:
private void FindApproximateZeroRecursive(Double min, Double max, Double
last)
{
last = new Double( (min.doubleValue()+max.doubleValue())/2 );
do {
if (Function(min.doubleValue()) * Function(last.doubleValue()) <
0)
max = last;
else
min = last;
} while ( IsApproximateZero(last.doubleValue()) );
}

The other posters explained the main points. Use double, not Double,
for efficiency, and return 'last' as a value from the FindApprox...
function.

One more thing: don't you need the '!' in
while (!IsApproximateZero(...)) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top