[ create a new paste ] login | about

Link: http://codepad.org/9wy4a7D8    [ raw code | output | fork ]

AaronMiller - C, pasted on Mar 12:
#include <stdio.h>

float FloatSign(float a) {
  union { float f; int i; } v;

  v.f = a;
  v.i = (v.i&0x80000000)|0x3F800000; /*0x3F800000 = 1.0f*/

  return v.f;
}

void Test(float f) {
  printf("%f -> %f\n", f, FloatSign(f));
}

int main() {
  Test(-1000.0f);
  Test(-1024.824f);
  Test(1.25f);
  Test(3.1415926535f);
  Test(-3.1415926535f);

  return 0;
}


Output:
1
2
3
4
5
-1000.000000 -> -1.000000
-1024.823975 -> -1.000000
1.250000 -> 1.000000
3.141593 -> 1.000000
-3.141593 -> -1.000000


Create a new paste based on this one


Comments: