javascript - 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)
解决方案
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);
推荐阅读
- c# - c# 多线程字典 - 使用一组新的值最佳实践刷新实时字典。创建新的字典,还是逐项重新加载旧的字典?
- keras - 辍学训练参数
- javascript - Axios 请求失败,触发无法读取未定义的属性“地图”
- github - 有没有办法获得分支的层次结构(文件夹)视图?
- java - 从扫描仪对象实例传递的数组值
- javascript - 使用多个模块时的 Nodejs API 初始化行为需要
- php - PHP 如何摆脱 Oracle 9i 上的空白?
- dataframe - Pyspark - 将 DataFrame 导出为文本
- javascript - 获取已经过去的时间,同时考虑到用户的 TimeZone with Moment
- sql - 在oracle中将数字转换为日期时间