today in the process of checking information, inadvertently see such a magic code, decided to reprint to their CSDN blog, but find a half-day, Leng did not find csdn reprint function, before often see others reproduced articles, and then the heart has been thinking, is not reproduced is a hidden function, Or you can use whatever command you have. So deliberately google, the original csdn reprint function, there is no quick reprint, a key reprint, only to the original copy, paste, and then in their own blog re-typesetting, finally in the release of the article when the choice of reprint, so that the article has become a turn-off article. So the design also makes sense, only the person who is not too troublesome, will be patient to complete the reprint, is to improve the reprint of the threshold, to avoid the occurrence of a large number of repeated articles. However, the side effect of this design is to waste a lot of time and energy. This is where the spit groove is, or look at this magical fast open square and take the countdown code:
float invsqrt (float x) {Float xhalf = 0.5f * x;int i = * (int *) & x;i = 0X5F3759DF-(i>>1); x = * (float *) & ; i;x = x * (1.5f-xhalf * x * x); return x;}
For more instructions on this code, please refer to this article, "Mathematical Principles of 0X5F3759DF".
The following simple test code I wrote:
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <math.h >//Open Square take the reciprocal float invsqrt (float x) {Float xhalf = 0.5f * x;int i = * (int *) & x;i = 0X5F3759DF-(i>>1); x = * ( float *) & i;x = x * (1.5f-xhalf * x * x); return x;} int main () {//comparison precision float val = 0.0f;val = 1.0f;printf ("Calculation accuracy comparison: \ n");p rintf ("Input value:%f fast algorithm:%f VC function:%f \ n", Val, Inv Sqrt (Val), 1.0f/sqrt (Val)); val = 16.0f;printf ("Input value:%f fast algorithm:%f VC function:%f \ n", Val, Invsqrt (Val), 1.0f/sqrt (Val) val = 25.0f;printf ("Input value:%f fast algorithm:%f VC function:%f \ n", Val, Invsqrt (Val), 1.0f/sqrt (Val)); val = 100.0f;printf ("Input value: %f fast algorithm:%f VC function:%f \ n ", Val, Invsqrt (Val), 1.0f/sqrt (val));p rintf (" \ n Compute performance comparison: \ n "); int count = 1000000;dword Tim Estart = 0, timeend = 0;timestart = GetTickCount (), for (int i = 0; i < count; i++) {val = invsqrt (100.0f); } timeend = GetTickCount (); printf ("Fast algorithm time:%f \ n", (timeend-timestart) * 0.001); Timestart = GettIckcount (); for (int i = 0; i < count; i++) {val = 1.0f/sqrt (100.0f); } timeend = GetTickCount (); printf ("VC function time:%f \ n", (timeend-timestart) * 0.001); printf ("\ n"); system ("pause"); return 0;}Here and sqrt () respectively compared the calculation accuracy and calculation performance, test environment for VS2005, ordinary PC notebook (in fact, is a long time, play the game, write code of the small black). From the comparison results, the fast algorithm has a little error in the calculation results, but the computational performance is considerable. For comparison results:
A fast open square algorithm for inverse arithmetic