快速平方根

实现代码如下:

unsigned int root(unsigned int x)
{
    unsigned int a,b;
    b = x;
    a = x = 0x3f;
    x = b/x;
    a = x = (x+a)>>1;
    x = b/x;
    a = x = (x+a)>>1;
    x = b/x;
    x = (x+a)>>1;
    return(x);
}

测试工程代码(附件为整个测试工程  http://files.cnblogs.com/files/libra13179/root_test.zip)

root_num.C

#include <stdio.h>
#include <stdlib.h>
#include "my_root.h"

int main(void)
{
    unsigned int iii;                        /* test variable     */
    FILE* stream;
    
    stream = fopen("root_date.txt", "w");
    for(iii=0; iii<(0xfffc); iii=iii+4) 
    {
       fprintf(stream,"
 %3.1f; %u ",(float)sqrt(iii), root(iii));

    }
    
    fclose(stream);
    printf("creates text data file for Excel ");
    system("pause");
    
    return 0;
}

my_root.C

#include "my_root.h"  

unsigned int root(unsigned int x)
{
    unsigned int a,b;
    b = x;
    a = x = 0x3f;
    x = b/x;
    a = x = (x+a)>>1;
    x = b/x;
    a = x = (x+a)>>1;
    x = b/x;
    x = (x+a)>>1;
    return(x);
}
View Code

my_root

    unsigned int root(unsigned int x);

Makefile

root_num.exe: root_num.o my_root.o  
    gcc -o root_num.exe root_num.o my_root.o  
root_num.o: root_num.c my_root.h  
    gcc -c root_num.c  
my_root.o: my_root.c my_root.h  
    gcc -c my_root.c  
View Code

现在开始生成我们的测试使用EXE文件

我们双击一下这个exe。可以生成我们关心的数据。

root_date.TXT中的

将数据生成图表

原文地址:https://www.cnblogs.com/libra13179/p/6731787.html