How to keep floating point number at 2 bytes?

Yes, I know the IEEE-754 standard for half-lives, and yes, I know about the work done in this area. Simply put, I'm trying to save a floating point prime (like, 52.1or 1.25) in just 2 bytes.

I tried some implementations in Java and in C # but they destroy the input value by decoding a different number. You load 32.1, and after coding-decoding you receive 32.0985.

Is there any way to save floating point numbers in just 16 bits without ruining the input value?

Many thanks.

+5
source share
6 answers

BCD :

52.1 = 521 * 10 ^ -1 => 0x1521
1.25 = 125 * 10 ^ -2 => 0x2125

0.0000000000000001 999. , , , , 0.0000000001 999000000.


, , . - . ( != .)

public static short Encode(double value) {
  int cnt = 0;
  while (value != Math.Floor(value)) {
    value *= 10.0;
    cnt++;
  }
  return (short)((cnt << 12) + (int)value);
}

public static double Decode(short value) {
  int cnt = value >> 12;
  double result = value & 0xfff;
  while (cnt > 0) {
    result /= 10.0;
    cnt--;
  }
  return result;
}

:

Console.WriteLine(Encode(52.1));
Console.WriteLine(Decode(4617));

:

4617
52.1
+5

# , .

8,8 (8 , 8 ):

float value = 123.45;
ushort fixedIntValue = (ushort)(value * 256);

, : XXXXXXXX, XXXXXXXX

, :

float value = fixedIntValue / 256f;
+6

, -, float double?

, short , , , 100, ? (, 52.1 1.25 5210 125). , .

, x ( 3), , ( , , - , ).

+5

, 32.1 .

32.099998. , -, 32.0985.

, .

+3

4 278 190 080 32- , NaN . 65 536 16 . , .

?

(, 4 8, 8), 8 388 608 , .

. , , . , ?

+2

3 . "" 11 4- 4 4 2 .

, , !

+1

All Articles