Giter Site home page Giter Site logo

Comments (1)

qappleh avatar qappleh commented on May 27, 2024
function integerToBin (num) {
  // 64
  const result = []
  while (num / 2) {
    next = num % 2
    num = Math.floor(num / 2)
    result.unshift(next)
  }
  return result
}

function fractionalToBin (num) {
  const result = []
  let i = 0
  while (num !== 0 && i < 54) {
    num = num * 2
    
    next = num >= 1 ? 1 : 0
    num = num % 1
    i++
    result.push(next)
  }
  return result
}

function decToBinary (num) {
  // 1.5
  const [int, fraction] = String(num).split(/(?=\.)/).map((x, i) => {
    return i === 0 ? integerToBin(x) : fractionalToBin(x)
  })
  return [int, fraction]
}

function binToDec (num) {
  const [_int, _fraction] = String(num).split('.')
  const int = _int.split('').reduceRight((acc, x, i) => {
    return acc + x * 2 ** i
  }, 0)
  const fraction = _fraction ? _fraction.split('').reduce((acc, x, i) => {
    return acc + x * 2 ** -(i + 1)
  }, 0) : 0
  return `${int}${fraction ? '.' + fraction.toString().slice(2) : ''}`
}

console.log(16, integerToBin(16), Number(16).toString(2))
console.log(18, integerToBin(18), Number(18).toString(2))
console.log(0.5, fractionalToBin(0.5), Number(0.5).toString(2))
console.log(0.1, fractionalToBin(0.1), Number(0.1).toString(2))
console.log(1.1, decToBinary(1.1), Number(1.1).toString(2))

console.log(7.875, decToBinary(7.875), Number(7.875).toString(2))
console.log('111.111', binToDec('111.111'), parseInt('111.111', 2))

from interview.

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.