Thread: sqrt()

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    sqrt()

    Can someone explain how the sqrt() function works internally? Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    asm VC++ sintax
    Code:
    __declspec(naked) float sqrt(float num){
        __asm{
    	FLD dword ptr [esp+4]
    	FSQRT
    	FIST dword ptr [esp+4]
    	mov eax, dword ptr [esp+4]
    	ret
        }
    }
    
    #include<stdio.h>
    int main(){
    	float i;
    	for(i=0;i<20;i++)
    		printf("sqrt of %f is %f\n",i,sqrt(i));
    	return 0;
    
    }
    Hum. I was having troubles when using 'num' instead of 'dword ptr [esp+4]' because the compiler was translating the num var adress as esp+8..... so I hard typed the adress every time.
    EDIT
    actually if you debug the app and view the dissassembly, sqrt will call about 40 asm instrutions... don't ask me why...
    Last edited by xErath; 06-01-2005 at 12:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  2. sqrt() function help
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 09:07 PM
  3. SQRT Mystery...
    By KneeLess in forum C Programming
    Replies: 7
    Last Post: 03-23-2004, 07:49 AM
  4. sqrt
    By modec in forum C Programming
    Replies: 3
    Last Post: 04-16-2003, 07:19 AM
  5. how sqrt ?
    By sambs1978 in forum C Programming
    Replies: 3
    Last Post: 09-20-2001, 08:14 AM