DESCRIPTION
This program to call GMP big integer functions to calculate the square root of single precision integer, it can get the result up to arbitrary accuracy.This program only use GMP big integer functions, it never call GMP Floating-point Functions to do it. The following GMP functions was used.
mpz_init
mpz_clear
mpz_set_ui
mpz_sizeinbase
mpz_cmp
mpz_add
mpz_sub
mpz_mul
mpz_mul_ui
mpz_pow_ui
mpz_tdiv_q
mpz_get_str
Arithmetic
The main Arithmetic is Newton's method. It first gets an approximate value of square root of a given integer. The Initial value was expressed with a fraction. Then it calls Newton's method to the value with more precise over and over. Final, it converts the value of square root of the integer from fraction format to a decimal string.
How to use it
1. Build it in Windows
You can build this project with VC6 or VC2008. You can open workspace file (sqrt_withGMP.dsw is for VC6 and sqrt_withGMP.sln is for VC2008) with VC6 or VC2008 to build it, in release mode, the executable file will be copied to ./bin folder. You can run it in "bin" folder directly.
2. Build it in Linux
Please make sure that you have installed GMP in your Linux PC first, then you can run below command to create an executable in source code folder.
gcc -lm -lgmp sqrt_withGMP.c -o sqrt_withGMP
Performance
Basically, the Performance is very good. The following data is time cost for calculating sqrt(2) to 10000,100000,1000000 digital
Result Digital Calculate time (ms) print time (ms)
10000 3 2.6
100000 47 31
1000000 986 894
AUTHOR
Baocheng Liang (liangbch@263.net), Feb-17, 2012
用牛顿迭代法计算整数的平方根到任意精度
4星 · 超过85%的资源 需积分: 48 16 浏览量
2012-02-17
16:40:43
上传
评论 1
收藏 274KB ZIP 举报
liangbch
- 粉丝: 278
- 资源: 6