Thread: Checking for integer after square root

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    A less elegant, yet highly precise solution would have you simply convert the resultant value to a string and analyze that.

    Code:
    bool is_sqrt_int(double your_value) {
      std::ostringstream ostr;
      ostr << square_root_of(your_value);
      return ostr.str().find(".") == std::string::npos;
    }
    [edit]
    As far as calculating the square root is concerned, I prefer Newton's iteration:
    Code:
    result = 1
    
    for i = 1 to N
      result = (result + (value / result)) / 2
    where N is the number of iterations you'd like. (The more iterations, the more precise the value, but I find just a few to be enough. Experiment yourself.)
    [/edit]
    Last edited by LuckY; 01-04-2006 at 03:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM