Git Product home page Git Product logo

Comments (1)

milesight-sway avatar milesight-sway commented on September 15, 2024
/**
 * Sign: 1
 * Exponent: 5, offset 15
 * Mantissa: 10
 */
function readFloat16LE(bytes) {
    var bits = (bytes[1] << 8) | bytes[0];
    var sign = bits >>> 15 === 0 ? 1.0 : -1.0;
    var e = (bits >>> 10) & 0x1f;
    var m = e === 0 ? (bits & 0x3ff) << 1 : (bits & 0x3ff) | 0x400;
    var f = sign * m * Math.pow(2, e - 25);
    return f;
}

/**
 * Sign: 1
 * Exponent: 8, offset 127
 * Mantissa: 23
 */
function readFloat32LE(bytes) {
    var bits = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
    var sign = bits >>> 31 === 0 ? 1.0 : -1.0;
    var e = (bits >>> 23) & 0xff;
    var m = e === 0 ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
    var f = sign * m * Math.pow(2, e - 150);
    return f;
}

/**
 * Sign: 1
 * Exponent: 11, offset 1023
 * Mantissa: 52
 */
function readFloat64LE(bytes) {
    var lowBits = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
    var highBits = (bytes[7] << 24) | (bytes[6] << 16) | (bytes[5] << 8) | bytes[4];
    
    var sign = highBits >>> 31 === 0 ? 1.0 : -1.0;
    var e = (highBits >>> 20) & 0x7ff;
    var m = e === 0 ? 
        (highBits & 0xfffff) * Math.pow(2, 32) + lowBits :
        (highBits & 0xfffff) * Math.pow(2, 32) + lowBits + Math.pow(2, 52);
    
    var f = sign * m * Math.pow(2, e - 1075);
    return f;
}

from sensordecoders.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.