0

What would be the best/fastest way to convert Double A to Single B which would guarantee that when casting B back to Double the following is always true (with minimal loss of precision):

A>=(Double)B

(or <= in symmetrical case).

The assumption is that A range fits into Single.

Example:

double A = 1234.5678901234;

float B = (float) A; //2nd step - truncation occurs

double C = B;

I want to modify 2nd step in such way that for any value of A, always A>=C (or A<=C in symmetrical case).

Karol Kolenda
  • 1,660
  • 2
  • 25
  • 37
  • 1
    https://msdn.microsoft.com/en-us/library/system.single%28v=vs.110%29.aspx You both could've Google'd that, but it in fact is a `float`. – Dion V. Feb 26 '15 at 10:00
  • 1
    @DionV. - I've never heard of it, thanks, At the same time, the OP could have shown their own research/effort ;) – Sayse Feb 26 '15 at 10:01
  • 1
    Sorry for the confusion, I just followed .Net convention: c# float - .Net Single, c# double - .Net Double – Karol Kolenda Feb 26 '15 at 10:03
  • [Related question](http://stackoverflow.com/q/9743044/1324033) – Sayse Feb 26 '15 at 10:06
  • @Sayse But I am going from double to float (which leads to double truncation) and back to double. Therefore after a round trip I have a different double, but I want to make sure that initial conversion to float will produce slightly smaller (or larger) float so the second "normal" cast will guarantee A>=(Double)B – Karol Kolenda Feb 26 '15 at 10:11
  • Hm... are you looking for something like `var b = (float)a + Single.Epsilon;`? -- Or `-` depending on which way you want it. – Corak Feb 26 '15 at 10:13
  • @Corak I don't think so. If a is big a + Single.Epsilon == a (at least this is what I understand) – Karol Kolenda Feb 26 '15 at 10:15
  • @KarolKolenda - Is your question regarding the min/max values converting? (i.e `double.MinValue`)? there isn't a lot that would make that greater or equal when round tripping – Sayse Feb 26 '15 at 10:16
  • OK, I will try to explain at the example: double A = 1234.56789012345; float B = (float)A; //This will truncate A to something like 1234.567 - of course we are talking float arithmetic, so that's not precise. And finally: double C = B; Now I want to make sure that A<=C (or A>=C in symmetrical case). Clearly A!=C as we lost some precision. So the question is how to modify 2nd step: float B = (float)A; to give me that guarantee. – Karol Kolenda Feb 26 '15 at 10:21

0 Answers0