• 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

Resolve the square root with the babilonian method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, im implementing the babylonian method to get the square root of a number as you can see here (in spanish) (wikipedia) .

Mi first code is:


And it works , but then i try with doubles:

And it loops for ever for numbers like "8".

I want to know why is this, and the same happens in C++ with doubles.

Thanks.
 
Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is correct, but it sounds like a rounding issue with doubles. Try to replace the exact "equals" comparison with something more tolerant, like:

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paolo Lemos wrote:I want to know why is this, and the same happens in C++ with doubles.


Simply put: because doubles are NOT exact, so it's quite possible that your divisions never end up equal.

And why would you think that 8 would be special? √8 is irrational from what I recall (it has been a long time though). √9 on the other hand...

I suspect that it works for float because calculations are actually done with doubles and then converted; but I wouldn't stake my life on it.

For more information, read this.

Winston
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is just lucky (or unlucky) that √8 “hunts”. That means that it works out √8.000000000001 followed by √7.999999999999999 repeatedly or similar. It says in the Java™ Language Specification that floating‑point arithmetic is done on 32 bits by default, so it can’t be that a float is converted to a double.
 
Paolo Lemos
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its work fine with:



Now i have:


And the output is:


2.828427
2.82842712474619
2.82842712474619



The new interesting thing is the sqrt3 method, i dont understand how the expression


transforms into :


In this last form, the condition works fine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic