Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                /**
 * Sequence of 0s and 1s.
 * @typedef {number[]} Bits
 */

/**
 * @typedef {{
 *   signBitsCount: number,
 *   exponentBitsCount: number,
 *   fractionBitsCount: number,
 * }} PrecisionConfig
 */

/**
 * @typedef {{
 *   half: PrecisionConfig,
 *   single: PrecisionConfig,
 *   double: PrecisionConfig
 * }} PrecisionConfigs
 */

/**
 * ┌───────────────── sign bit
 * │   ┌───────────── exponent bits
 * │   │       ┌───── fraction bits
 * │   │       │
 * X XXXXX XXXXXXXXXX
 *
 * @type {PrecisionConfigs}
 */
const precisionConfigs = {
  // @see: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
  half: {
    signBitsCount: 1,
    exponentBitsCount: 5,
    fractionBitsCount: 10,
  },
  // @see: https://en.wikipedia.org/wiki/Single-precision_floating-point_format
  single: {
    signBitsCount: 1,
    exponentBitsCount: 8,
    fractionBitsCount: 23,
  },
  // @see: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  double: {
    signBitsCount: 1,
    exponentBitsCount: 11,
    fractionBitsCount: 52,
  },
};

/**
 * Converts the binary representation of the floating point number to decimal float number.
 *
 * @param {Bits} bits - sequence of bits that represents the floating point number.
 * @param {PrecisionConfig} precisionConfig - half/single/double precision config.
 * @return {number} - floating point number decoded from its binary representation.
 */
function bitsToFloat(bits, precisionConfig) {
  const { signBitsCount, exponentBitsCount } = precisionConfig;

  // Figuring out the sign.
  const sign = (-1) ** bits[0]; // -1^1 = -1, -1^0 = 1

  // Calculating the exponent value.
  const exponentBias = 2 ** (exponentBitsCount - 1) - 1;
  const exponentBits = bits.slice(signBitsCount, signBitsCount + exponentBitsCount);
  const exponentUnbiased = exponentBits.reduce(
    (exponentSoFar, currentBit, bitIndex) => {
      const bitPowerOfTwo = 2 ** (exponentBitsCount - bitIndex - 1);
      return exponentSoFar + currentBit * bitPowerOfTwo;
    },
    0,
  );
  const exponent = exponentUnbiased - exponentBias;

  // Calculating the fraction value.
  const fractionBits = bits.slice(signBitsCount + exponentBitsCount);
  const fraction = fractionBits.reduce(
    (fractionSoFar, currentBit, bitIndex) => {
      const bitPowerOfTwo = 2 ** -(bitIndex + 1);
      return fractionSoFar + currentBit * bitPowerOfTwo;
    },
    0,
  );

  // Putting all parts together to calculate the final number.
  return sign * (2 ** exponent) * (1 + fraction);
}

/**
 *  Converts the 16-bit binary representation of the floating point number to decimal float number.
 *
 * @param {Bits} bits - sequence of bits that represents the floating point number.
 * @return {number} - floating point number decoded from its binary representation.
 */
export function bitsToFloat16(bits) {
  return bitsToFloat(bits, precisionConfigs.half);
}

/**
 * Converts the 32-bit binary representation of the floating point number to decimal float number.
 *
 * @param {Bits} bits - sequence of bits that represents the floating point number.
 * @return {number} - floating point number decoded from its binary representation.
 */
export function bitsToFloat32(bits) {
  return bitsToFloat(bits, precisionConfigs.single);
}

/**
 * Converts the 64-bit binary representation of the floating point number to decimal float number.
 *
 * @param {Bits} bits - sequence of bits that represents the floating point number.
 * @return {number} - floating point number decoded from its binary representation.
 */
export function bitsToFloat64(bits) {
  return bitsToFloat(bits, precisionConfigs.double);
}
              
            
!
999px

Console