package io.lacuna.bifurcan.utils;

/**
 * @author ztellman
 */
public class Encodings {

  private static long NAN = doubleToLong(Double.NaN);
  private static long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0);
  private static long NEGATIVE_EPSILON = doubleToLong(-2.23e-308);

  /**
   * The inverse operation for {@code doubleToLong()}.
   */
  public static double longToDouble(long value) {
    if (value == NAN) {
      return Double.NaN;
    } else if (value == NEGATIVE_EPSILON) {
      return -0.0;
    } else if (value < 0.0) {
      value ^= Long.MAX_VALUE;
    }
    return Double.longBitsToDouble(value);
  }

  /**
   * Converts a double into a corresponding long that shares the same comparison semantics.
   */
  public static long doubleToLong(double value) {
    long v = Double.doubleToRawLongBits(value);
    if (v == NEGATIVE_ZERO) {
      return NEGATIVE_EPSILON;
    }

    if (value < 0.0) {
      v ^= Long.MAX_VALUE;
    }
    return v;
  }
}

@@ -288,56 +288,91 @@
   * 
-The inverse operation for %7B@code doubleToLong()%7D
+Converts a double into a corresponding long that shares the same ordering semantics
 .%0A  
@@ -395,111 +395,99 @@
 tic 
+long 
 double
- longToDouble(long value) %7B%0A    if (value == NAN) %7B%0A      return Double.NaN;%0A    %7D else
+ToLong(double value) %7B%0A    long v = Double.doubleToRawLongBits(value);%0A   
  if (v
-alue
  == 
@@ -495,23 +495,20 @@
 EGATIVE_
-EPSILON
+ZERO
 ) %7B%0A    
@@ -520,24 +520,36 @@
 urn 
--0.0
+NEGATIVE_EPSILON
 ;%0A    %7D
- else
+%0A%0A   
  if 
@@ -571,20 +571,16 @@
 %0A      v
-alue
  %5E= Long
@@ -612,141 +612,75 @@
 urn 
-Double.longBitsToDouble(value);%0A  %7D%0A%0A  /**%0A   * Converts a double into a corresponding long that shares the same comparison semantics
+v;%0A  %7D%0A%0A  /**%0A   * The inverse operation for %7B@code doubleToLong()%7D
 .%0A  
@@ -703,99 +703,111 @@
 tic 
-long 
 double
-ToLong(double value) %7B%0A    long v = Double.doubleToRawLongBits(value);%0A   
+ longToDouble(long value) %7B%0A    if (value == NAN) %7B%0A      return Double.NaN;%0A    %7D else
  if (v
+alue
  == 
@@ -807,36 +807,39 @@
 lue == NEGATIVE_
-ZERO
+EPSILON
 ) %7B%0A      return
@@ -843,36 +843,24 @@
 urn 
-NEGATIVE_EPSILON
+-0.0
 ;%0A    %7D
-%0A%0A   
+ else
  if 
@@ -874,32 +874,36 @@
 %3C 0.0) %7B%0A      v
+alue
  %5E= Long.MAX_VAL
@@ -919,21 +919,50 @@
 %0A    return 
-v
+Double.longBitsToDouble(value)
 ;%0A  %7D%0A%7D%0A

tweak docs