Skip to content

Instantly share code, notes, and snippets.

@YangKeao
Created July 8, 2014 06:26
Show Gist options
  • Save YangKeao/60997bb414268ad807ce to your computer and use it in GitHub Desktop.
Save YangKeao/60997bb414268ad807ce to your computer and use it in GitHub Desktop.
#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