wood burning stoves*
The moose likes Beginning Java and the fly likes How to keep doubleing a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to keep doubleing a number" Watch "How to keep doubleing a number" New topic
Author

How to keep doubleing a number

Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Need help. This should be simple, but i can not seem to get it.
I want to take a double like 0.01 and then times it by 2 for each output.
like this....
0.01
0.02
0.04
0.08
0.16
Do i use a loop of if, else?

Thank you.
Jahed Hossain
Greenhorn

Joined: Nov 30, 2012
Posts: 11

Adam Burda wrote:Need help. This should be simple, but i can not seem to get it.
I want to take a double like 0.01 and then times it by 2 for each output.
like this....
0.01
0.02
0.04
0.08
0.16
Do i use a loop of if, else?

Thank you.


You can use a loop like:
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Thank you. I feel like DUH....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

If you need to do something more than once, that suggests a loop. So then the question is which kind? If you know in advance how many times, then a for loop is the best fit. If you need to do it until a condition is met, then that's what while and do/while loops are for.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Adam Burda wrote:I want to take a double like 0.01 and then times it by 2 for each output.

Just to add to the other good advice, you should be careful with this sort of thing when using double.

For specifically multiplying by 2, you're probably OK; but you may run into difficulties with other multipliers.

The reason is that 0.01 does NOT equal 0.01 - at least, not exactly.

For more information, I suggest you read this or this. The first is "the Bible", but the second is probably a bit easier to understand.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to keep doubleing a number
 
Similar Threads
Double addition gives wrong result?
A probability problem
Anyone wanna sponsor me???
double addition error
Problems with JUnit Tests