#include <iostream>
#include <cassert>
#include <cmath>

double from_storage(unsigned short bits) { //only lowest 10 bits used, upper are ignored
	double a = bits&0x1FF;
	a /= 5.11967985;
	a = a*a;//std::pow(a, 2); // inverse of number in to_storage
	return double(bits&0x200 ? -a : a);
}

unsigned short to_storage(double a) { //only lowest 10 bits set/relevant
	assert(a<=10000.0);
	assert(a>=-10000.0);
	if (a >= 0) {
		a = std::pow(a, .5); // inverse of number in from_storage
		a *= 5.11967985;
		unsigned short b = ((unsigned short)(a));
		assert((b&0x200)==0);
		return b;
	} else {
		a = std::pow(-a, .5); // inverse of number in from_storage
		a *= 5.11967985;
		unsigned short b = ((unsigned short)(a));
		assert((b&0x200)==0);
		return b | 0x200;
	}
}

void test(double a) {
	unsigned short v = to_storage(a);
	double r = from_storage(v);
	std::cout << a << '\t' << v << '\t' << r << '\n';
}

int main() {
	test(-10000.0);
	test( -9950.0);
	test( -5000.0);
	test( -2500.0);
	test( -1000.0);
	test(  -500.0);
	test(  -250.0);
	test(  -100.0);
	test(   -50.0);
	test(   -25.0);
	for(double d = -10.0; d < 11.5; d += 1)
		test(d);
	test(   25.0);
	test(   50.0);
	test(  100.0);
	test(  250.0);
	test(  500.0);
	test( 1000.0);
	test( 2500.0);
	test( 5000.0);
	test( 9950.0);
	test(10000.0);
	return 0;
}