The issue I see is that metaP effectively abuses enums into variables. The problem is that enums are treated internally as integers which precludes trying to get a floating point value out of them. However, you may be able to create your own floating point format that creates two results, an integer portion and an exponent. You'll still have to process this into a float, as Out = Sqrt<42>::mantissa * pow(10,Sqrt<42>::exponent);
. Actually determining the values is left as an exercise for the reader, but you will probably have to scale the input upwards (by an even power of 10), computing the root, and storing the -power/2 you used earlier.
To compute sqrt<42>, you would first set the exponent enum to a suitable power, such as '-4' (the lower, the more decimals, but watch for overflow). You then multiply the input by '10^(-2*exponent)'. In this case you get 42*10^8 = 4200000000. You then take the root of this value getting '64807' as the final value. At runtime, you calculate the "val * 10 ^ exponent" = "64807 * 10 ^ -4" = 64807 * 0.0001 = 6.4807m and store it to a float.
The extra conversion work kinda defeats the purpose, but you can reduce it a bit by storing the exponent as 10^k (ie 10^4) then doing out=sqrt<x>::mantissa/sqrt<x>::exponent
.
edit I just noticed that with the mantissa/exponent method, the choice of exponent is arbitrary as long as it's bigger than the integer portion of the final root. It can even be a constant, which eases the design of your meta functions. In the case of 42 for example you could select the 'exponent' to always be 6000. You then multiply the input by 6000^2, take the integer root of the product, then at runtime, divide the result by 6000 to get the root. Instead of treating the output as a*10^b, it uses the relation sqr(x*b^2)=sqr(x)*b. The math checks out:
- 42*6000*6000=1512000000
- sqr(1512000000)=38884
- 38884/6000=6.4806 (squared is 41.999)
sqrt
is a standard function I'd usesqrtt
instead. – Tetragonal