Created
April 14, 2013 14:16
-
-
Save jacobabrahamb4/5382888 to your computer and use it in GitHub Desktop.
absolute value of float
This file contains hidden or 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> | |
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