Created
July 8, 2014 06:26
-
-
Save YangKeao/60997bb414268ad807ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
using namespace std; | |
long long q; | |
float CarmSqrt(float x){ | |
union{ | |
int intPart; | |
float floatPart; | |
} convertor; | |
union{ | |
int intPart; | |
float floatPart; | |
} convertor2; | |
convertor.floatPart = x; | |
convertor2.floatPart = x; | |
convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1); | |
convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1); | |
return 0.5f*(convertor.floatPart + (x * convertor2.floatPart)); | |
}//Real Carmack | |
float gen(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*q; | |
}//easy | |
int main(){ | |
scanf("%d",&q); | |
printf("%d\n%d",int(CarmSqrt(q)),int(gen(q))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment