Skip to content

Instantly share code, notes, and snippets.

@jacobabrahamb4
Created April 14, 2013 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobabrahamb4/5382888 to your computer and use it in GitHub Desktop.
Save jacobabrahamb4/5382888 to your computer and use it in GitHub Desktop.
absolute value of float
#include<stdio.h>
int main()
{
float x;
printf("Enter the float number:\n");
scanf("%f", &x);
// copy and re-interpret as 32 bit integer
int casted_to_int = *(int*)&x;// float pointer is converted to an integer pointer and dereferenced.
// clear highest bit
casted_to_int&=0x7FFFFFFFF;
// re-interpret as float
printf("Absolute value of float is %d \n",*(float*)&casted_to_int);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment