首页 > 解决方案 > Floating Point Number is greater than another floating point by how many points

问题描述

I have function that accepts a random floating point number as a parameter . The if condition in the function checks whether the floating number is equal to or greater than, by a specific number of points , say 0.002 another floating point number x that is constant. Below is the function that i had created.

function checkRandom({ value }) {
  const x = 0.98334
  const diff = value - x
  if (value == x || (value > x) diff) {
    console.log("Match Successful")
  } else {
    console.log("")
  }
}
checkRandom(0.97)

标签: javascript

解决方案


Perhaps you meant this?

Passing {value}

function checkRandom({value}) {
  const x = 0.98334
  const diff = value - x;
  console.log(diff)
  if (value == x || Math.abs(diff) < .002) {
    console.log("Match Successful")
  } else {
    console.log("Difference is greater than .002")
  }
}
checkRandom({value:0.98332})

Just passing a number

function checkRandom(num) {
  const x = 0.98334
  const diff = num - x;
  console.log(diff)
  if (num == x || Math.abs(diff) < .002) {
    console.log("Match Successful")
  } else {
    console.log("Difference is greater than .002")
  }
}
checkRandom(0.98332);


推荐阅读