All files / lib/serialize F64.ts

68.18% Statements 60/88
52.63% Branches 20/38
80% Functions 28/35
68.6% Lines 59/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 3381x                   1x             428x 428x 2x   426x                   334x 334x 334x                 4x 4x                 4x 4x             1x 1x             1x                                                                                       206x             196x             81x               77x                             11x 11x 88x       11x 11x 11x   11x             4x 4x             2x 2x             4x 4x             1x 1x                             24x     24x 23x       1x                                   8x             3x 3x 2x   1x             31x             2x 2x                 4x 2x   2x             4x 2x   2x                                                                   1x       1x       1x       1x       1x       1x      
import Decimal from 'decimal.js';
 
/**
 * IEEE 754 double-precision floating-point number implementation
 *
 * Format: 1 bit sign + 11 bits exponent + 52 bits mantissa
 *
 * This class works directly with the binary representation to avoid
 * precision loss when serializing/deserializing f64 values to/from buffers.
 */
export class F64 {
  private _buffer: Buffer; // 8 bytes storing the IEEE 754 representation
 
  /**
   * Create an F64 from raw bytes (IEEE 754 format)
   */
  constructor(buffer?: Buffer) {
    Eif (buffer) {
      if (buffer.length !== 8) {
        throw new Error('F64 buffer must be exactly 8 bytes');
      }
      this._buffer = Buffer.from(buffer);
    } else {
      this._buffer = Buffer.alloc(8);
    }
  }
 
  /**
   * Create F64 from JavaScript number
   */
  static fromNumber(num: number): F64 {
    const buffer = Buffer.alloc(8);
    buffer.writeDoubleBE(num, 0);
    return new F64(buffer);
  }
 
  /**
   * Create F64 from Decimal.js value
   * Uses the most precise conversion possible
   */
  static fromDecimal(decimal: Decimal): F64 {
    // Convert to number and let IEEE 754 handle the precision
    const num = decimal.toNumber();
    return F64.fromNumber(num);
  }
 
  /**
   * Create F64 from string representation
   * Uses Decimal.js to parse the string without precision loss from JavaScript Number
   */
  static fromString(str: string): F64 {
    // Parse string using Decimal.js to avoid JavaScript Number precision loss
    const decimal = new Decimal(str);
    return F64.fromDecimal(decimal);
  }
 
  /**
   * Create F64 from bigint (treating as integer value)
   */
  static fromBigInt(bigint: bigint): F64 {
    const num = Number(bigint);
    return F64.fromNumber(num);
  }
 
  /**
   * Create F64 representing zero (static method for API compatibility)
   */
  static zero(): F64 {
    return F64.fromNumber(0);
  }
 
  /**
   * Create F64 with specific IEEE 754 components
   */
  static fromComponents(
    sign: boolean,
    exponent: number,
    mantissa: bigint,
  ): F64 {
    if (exponent < 0 || exponent > 2047) {
      throw new Error('Exponent must be between 0 and 2047');
    }
    if (mantissa < BigInt(0) || mantissa >= BigInt(1) << BigInt(52)) {
      throw new Error('Mantissa must be between 0 and 2^52-1');
    }
 
    const buffer = Buffer.alloc(8);
 
    // Pack into 64-bit big-endian format
    // Bit layout: SEEEEEEE EEEEMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM
    const signBit = sign ? BigInt(1) : BigInt(0);
    const expBits = BigInt(exponent);
    const mantissaBits = mantissa;
 
    const ieee754Bits =
      (signBit << BigInt(63)) | (expBits << BigInt(52)) | mantissaBits;
 
    // Write as big-endian bytes
    for (let i = 0; i < 8; i++) {
      const byteValue = Number(
        (ieee754Bits >> BigInt(56 - i * 8)) & BigInt(0xff),
      );
      buffer.writeUInt8(byteValue, i);
    }
 
    return new F64(buffer);
  }
 
  /**
   * Get the raw buffer containing IEEE 754 representation
   */
  getBuffer(): Buffer {
    return Buffer.from(this._buffer);
  }
 
  /**
   * Serialize to buffer (big-endian)
   */
  serialize(): Buffer {
    return this.getBuffer();
  }
 
  /**
   * Deserialize from buffer (big-endian)
   */
  static deserialize(buffer: Buffer): F64 {
    return new F64(buffer);
  }
 
  /**
   * Convert to JavaScript number
   * This may lose precision for very large or very precise values
   */
  toNumber(): number {
    return this._buffer.readDoubleBE(0);
  }
 
  /**
   * Convert to Decimal.js for arbitrary precision
   */
  toDecimal(): Decimal {
    return new Decimal(this.toNumber());
  }
 
  /**
   * Extract IEEE 754 components
   */
  getComponents(): { sign: boolean; exponent: number; mantissa: bigint } {
    // Read as big-endian 64-bit integer
    let ieee754Bits = BigInt(0);
    for (let i = 0; i < 8; i++) {
      ieee754Bits =
        (ieee754Bits << BigInt(8)) | BigInt(this._buffer.readUInt8(i));
    }
 
    const sign = (ieee754Bits & (BigInt(1) << BigInt(63))) !== BigInt(0);
    const exponent = Number((ieee754Bits >> BigInt(52)) & BigInt(0x7ff));
    const mantissa = ieee754Bits & ((BigInt(1) << BigInt(52)) - BigInt(1));
 
    return { sign, exponent, mantissa };
  }
 
  /**
   * Check if the value is finite
   */
  isFinite(): boolean {
    const { exponent } = this.getComponents();
    return exponent !== 2047; // NaN and Infinity have exponent = 2047
  }
 
  /**
   * Check if the value is NaN
   */
  isNaN(): boolean {
    const { exponent, mantissa } = this.getComponents();
    return exponent === 2047 && mantissa !== BigInt(0);
  }
 
  /**
   * Check if the value is infinite
   */
  isInfinite(): boolean {
    const { exponent, mantissa } = this.getComponents();
    return exponent === 2047 && mantissa === BigInt(0);
  }
 
  /**
   * Check if the value is zero
   */
  isZero(): boolean {
    const { exponent, mantissa } = this.getComponents();
    return exponent === 0 && mantissa === BigInt(0);
  }
 
  /**
   * Get string representation
   */
  toString(): string {
    return this.toNumber().toString();
  }
 
  /**
   * Convert to JSON-safe value: number if within safe range, string if too large
   * This preserves precision for very large numbers that exceed JavaScript's MAX_SAFE_INTEGER
   */
  toJSONValue(): number | string {
    const num = this.toNumber();
 
    // Check if the number is within JavaScript's safe integer range
    if (num <= Number.MAX_SAFE_INTEGER && num >= Number.MIN_SAFE_INTEGER) {
      return num;
    }
 
    // For very large numbers, return as string to preserve precision
    return num.toString();
  }
 
  /**
   * Create F64 from JSON value (handles both number and string inputs)
   */
  static fromJSONValue(value: number | string): F64 {
    if (typeof value === 'string') {
      return F64.fromString(value);
    } else {
      return F64.fromNumber(value);
    }
  }
 
  /**
   * Get hex representation of the raw bytes
   */
  toHex(): string {
    return this._buffer.toString('hex');
  }
 
  /**
   * Create F64 from hex string
   */
  static fromHex(hex: string): F64 {
    const buffer = Buffer.from(hex, 'hex');
    if (buffer.length !== 8) {
      throw new Error('Hex string must represent exactly 8 bytes');
    }
    return new F64(buffer);
  }
 
  /**
   * Compare with another F64
   */
  equals(other: F64): boolean {
    return this._buffer.equals(other._buffer);
  }
 
  /**
   * Equality comparison (alias for equals, for API compatibility)
   */
  eq(other: F64 | number): boolean {
    Eif (typeof other === 'number') {
      return this.toNumber() === other;
    }
    return this.equals(other);
  }
 
  /**
   * Greater than comparison
   */
  gt(other: F64 | number): boolean {
    if (typeof other === 'number') {
      return this.toNumber() > other;
    }
    return this.toNumber() > other.toNumber();
  }
 
  /**
   * Less than comparison
   */
  lt(other: F64 | number): boolean {
    if (typeof other === 'number') {
      return this.toNumber() < other;
    }
    return this.toNumber() < other.toNumber();
  }
 
  /**
   * Greater than or equal comparison
   */
  gte(other: F64 | number): boolean {
    if (typeof other === 'number') {
      return this.toNumber() >= other;
    }
    return this.toNumber() >= other.toNumber();
  }
 
  /**
   * Less than or equal comparison
   */
  lte(other: F64 | number): boolean {
    if (typeof other === 'number') {
      return this.toNumber() <= other;
    }
    return this.toNumber() <= other.toNumber();
  }
 
  /**
   * Create a copy
   */
  clone(): F64 {
    return new F64(this._buffer);
  }
 
  /**
   * Constants
   */
  static get ZERO(): F64 {
    return F64.fromNumber(0);
  }
 
  static get ONE(): F64 {
    return F64.fromNumber(1);
  }
 
  static get NEGATIVE_ONE(): F64 {
    return F64.fromNumber(-1);
  }
 
  static get INFINITY(): F64 {
    return F64.fromNumber(Number.POSITIVE_INFINITY);
  }
 
  static get NEGATIVE_INFINITY(): F64 {
    return F64.fromNumber(Number.NEGATIVE_INFINITY);
  }
 
  static get NaN(): F64 {
    return F64.fromNumber(Number.NaN);
  }
}