1

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); 
8
  • 3
    Is there a reason you are using an int instead of a decimal? Decimal new_value = (Decimal)original_value Commented Jul 8, 2013 at 21:37
  • Do you want fixed point, or floating point? I'm confused. If you want fixed point, then simply decide your scale and multiply. I don't understand why you talk about variable number of digits. That doesn't sound very fixed point to me. Commented Jul 8, 2013 at 21:38
  • fixed-scale representations are int Commented Jul 8, 2013 at 21:38
  • I need fixed-point representations. Commented Jul 8, 2013 at 21:39
  • So, explain where variable numbers of digits comes into this? Commented Jul 8, 2013 at 21:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. The numbers are as indicated above. I need double type precision. Does that mean my scale should be 2^64?
I don't know. I've no idea what your constraints and goals are. I don't know what you mean by "I need double type precision". You are clearly going to sacrifice precision when you try to convert a double precision value to something that fits in an int. Also remember that those are not your numbers. Only 1600.25 is exactly representable in binary floating point. The other numbers cannot be exactly represented in a double.
I understand that I will lose precision. I guess I am trying to figure out the accuracy. If I use 2^14 scale then my accuracy will be 0.00006 and the fixed will fit into a word. Is that correct?
Fit into a word? Don't know what you mean. For a given scale, the fixed value will never be more than 0.5/scale different from the original floating value.
If you're concerned with scale, then to a certain extent precision, why not use this strategy on decimals instead of doubles ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.