• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

iterating a double

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I do something like this:

strange stuff happens. I don't get a compile error or a runtime error, but it is doing something that doesn't seem right. I get 55 for my aDouble variable.
What is the correct way to iterate a double?
Thanks.
 
Conroy Mulligan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that i would want to, but how would one be able to iterate as many times equal to the MAX_VALUE value of double? Is the extent of iteration limited to an int?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get 55 for my aDouble variable.
And there's something wrong with that? That's the correct answer.
0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Not that i would want to, but how would one be able to iterate as many times equal to the MAX_VALUE value of double? Is the extent of iteration limited to an int?
Ummm... you can easily loop with a long rather than int, so it's not limited to int. However using a double is a bad idea. If you keep adding 1 to it, eventually the number will be so big that when you add 1, it has no effect, due to roundoff error. E.g. if you use doubles to add 1e100 + 1, you just get 1e100 again. So you'll never reach Double.MAX_VALUE.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic