[ create a new paste ] login | about

Link: http://codepad.org/5fcu5YUs    [ raw code | output | fork ]

AaronMiller - C++, pasted on Jan 7:
#include <cstdio>

// create a compressed float
// NOTE: in g++, references to this with constants are
//       automatically converted to constants, so this
//       call takes no time to execute.
static inline float cf(float f) {
	union { float f; unsigned int i; } v;

	v.f = f;
	v.i &= 0xFFFF0000;

	//this operation can be more complex if necessary to improve quality
	//as long as it's constant, it shouldn't increase the size of the
	//executable.

	return v.f;
}

int main(int argc, char **argv) {
  printf("%f\n", cf(0.04f));
  return 0;
}


Output:
1
0.039795


Create a new paste based on this one


Comments: