handling issues with double

A

aegis

I'm using recursion where I take some given value and a step size
and call the function on itself until the value reaches zero.

The problem is, say I have val = 1, step = .01, then
recursive calls will reduce the value by ~.01, however,
my base case messes up because it never reaches 0.

..1 - .1 ends up being some other double value.
What is a portable way of handling this?
 
I

Ian Collins

I'm using recursion where I take some given value and a step size
and call the function on itself until the value reaches zero.

The problem is, say I have val = 1, step = .01, then
recursive calls will reduce the value by ~.01, however,
my base case messes up because it never reaches 0.

.1 - .1 ends up being some other double value.
What is a portable way of handling this?

Don't test double values for equality, check for less than a minimum value.
 
J

James Kanze

On 07/ 6/10 01:17 PM, aegis wrote:
Don't test double values for equality, check for less than
a minimum value.

Don't use double for this sort of thing. If he uses 100, with
a step of -1, and uses the value val/100. in his loop, he'll get
the right results, without any ambiguity with regards to the end
condition.
 
J

Juha Nieminen

James Kanze said:
Don't use double for this sort of thing. If he uses 100, with
a step of -1, and uses the value val/100. in his loop

While I think that's correct, wouldn't it be better to use the
significantly less obfuscated syntax "val/100.0" (instead of "val/100.")?
While it probably doesn't syntactically make any difference, it's much
more readable.
 
V

Victor Bazarov

While I think that's correct, wouldn't it be better to use the
significantly less obfuscated syntax "val/100.0" (instead of "val/100.")?
While it probably doesn't syntactically make any difference, it's much
more readable.

If 'val' is a double it makes no difference. If it *is* not a double,
then the difference is significant and has to be addressed in a comment.

BTW, what obfuscation do you mean in this case?

V
 
J

James Kanze

While I think that's correct, wouldn't it be better to use the
significantly less obfuscated syntax "val/100.0" (instead of
"val/100.")? While it probably doesn't syntactically make any
difference, it's much more readable.

Your suggestion is more visible (and thus preferrable) in any
case. (I thought that that was what I'd written, but my finger
must have slipped when I was typing the final 0.)
 
J

Juha Nieminen

James Kanze said:
Your suggestion is more visible (and thus preferrable) in any
case. (I thought that that was what I'd written, but my finger
must have slipped when I was typing the final 0.)

Curiously, many C/C++ programmers actually don't know that "100." is
a completely valid literal of type double, and in practice identical
to "100.0". Of course it's rather confusing (especially if written
among prose).
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top