Question

I am trying to convert a double type to fixed type representation. Depending on the input selected, my raw values can be for example 0.00996, 989.15, 1600.25, 89.72 and so on. The point here is that all my input values are always positive but the number of digits before (and after) the decimal place can vary. I am trying to convert these double types to fixed point representation via suitable scaling. I have tried 2.14 scaling (multiply by 16,384) but am not sure how to extend that to the case of variable number of digits before the decimal place. Also, how can I best ensure the maximum accuracy/precision in the converted values? Any help would be greatly appreciated. Thank you.

Code I am using:

fixedScale = (int) Math.Pow(2.0, 64.0);
int new_val = (int) (fixedScale * value);
Was it helpful?

Solution

If you want a fixed point representation then you simply need to decide on the scale. Once you have decided that you convert from floating to fixed like this:

int fixedValue = (int)Math.Round(floatValue*Scale);

And in the other direction like this:

double floatValue = (double)fixedValue/Scale;

As to what scale to use, that depends on what you are trying to achieve and what the input data are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top