Thread: Simple square-route function if you don't want to use cmath

  1. #1
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Simple square-route function if you don't want to use cmath

    Code:
    double My_sqrt(int ToSqrt){
    /*If you want, make the prompt here.
    
    
    */
    double answer=.0000001;
    while(answer*answer<ToSqrt){
    answer+=.0000001;}
    return answer;}
    It relies on loops.You can understand it(duh!).I it doesn't work, I just made it up on a dime.
    Later.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ...

    Took about 4 seconds to find the square root of 64...
    I think I'll rely on sqrt() which found it in nanoseconds...

    -_-
    Last edited by rmullen3; 01-14-2003 at 05:47 PM.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Wow, it took your computer 40 seconds to do only 8 million calculations? Mine took 1 second. Maybe you should upgrade...

    sqrt() is still obviously faster though!

  4. #4
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Oh, the bad thing is it is slow. I can do some operations faster than it.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ...

    mistype, meant 4 seconds... *edited*
    Still slow.

    "I can do some operations faster than it."

    Sorry I don't get what you mean?

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Things like that aren't meant to be questioned
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    I hope you were exaggerating, because I timed it using time() and it reported 4 seconds. I'm using a AMD K6-2 500MHz and compiled it on MSVC++.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    agh ... as I said, mistype... I'm running on 450 mhz, and it took about 4 seconds (4.24).

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Lightbulb

    Use the function pow()...
    it will do the job!
    none...

  10. #10
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Re: ...


    "I can do some operations faster than it."

    Sorry I don't get what you mean? [/B]
    I meant before it finishes, I'm done.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    
    int main( void ){
    
    	float	x = 64;
    	
    	__asm{					
    		FLD x			
    		FSQRT			
    		FST x			
    	}
    
    	std::cout << x << std::endl;
    
    	return 0;
    }

  12. #12
    Why use ASM for this? Faster?

  13. #13
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Munkey01
    Why use ASM for this? Faster?
    Maybe....I was just "adding to the mix"

  14. #14
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Simple square-route function if you don't want to use cmath

    Originally posted by fuh
    Code:
    double My_sqrt(int ToSqrt){
    /*If you want, make the prompt here.
    
    
    */
    double answer=.0000001;
    while(answer*answer<ToSqrt){
    answer+=.0000001;}
    return answer;}
    It relies on loops.You can understand it(duh!).I it doesn't work, I just made it up on a dime.
    Later.
    ugggggggg. very slow, very useless, arbitrairly bad precision.
    hello, internet!

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    __inline float RSqrt( float number ) {
        long i;
        float x2, y;
        const float threehalfs = 1.5f;
        x2 = number * 0.5f;
        y  = number;
        i  = * (long *) &y;
        i  = 0x5f3759df - (i >> 1);
        y  = * (float *) &i;
        y  = y * (threehalfs - (x2 * y * y));
        return y;
    }
    In all seriousness, I think i'm going to cry.

    If ANYONE can explain to me how that works, i'll make a shrine in your honour and pray at it every day.
    Last edited by Eibro; 01-14-2003 at 09:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM