Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to keep doubleing a number

 
Adam Burda
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I feel like DUH....
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic