Skip to content

Instantly share code, notes, and snippets.

@jojosati
Created February 1, 2025 05:49
Show Gist options
  • Save jojosati/7b64d8c6fc888700d690efe5918cfd62 to your computer and use it in GitHub Desktop.
Save jojosati/7b64d8c6fc888700d690efe5918cfd62 to your computer and use it in GitHub Desktop.
JavaScript, Rounded Number Inaccurate Outcome
const num1 = {min: 1, max: 2, step: 0.1}
const num2 = {min: 1, max: 50, step: 0.25}
num1.val = num1.min
while (num1.val < num1.max) {
let result1 = num1.val + num1.step
let frac1 = result1.toString().split('.')[1] || ''
let adj1 = 0
if (frac1.length > 10 && frac1.slice(2,4)=='49') {
adj1 = 0.0000000001
// console.log(`num1: ${num1.val} + ${num1.step} = ${result1} :::: ${Math.round((result1 + adj1) * 100) / 100} `)
}
num1.val = Math.round((result1 + adj1) * 100) / 100
num2.val = num2.min
while (num2.val < num2.max) {
let result2 = num2.val + num2.step
let frac2 = result2.toString().split('.')[1] || ''
let adj2 = 0
if (frac2.length > 10 && frac2.slice(2,4)=='49') {
adj2 = 0.0000000001
// console.log(`num2: ${num2.val} + ${num2.step} = ${result2} :::: ${Math.round((result2 + adj2) * 1000) / 1000}`)
}
num2.val = Math.round((result2 + adj2) * 100) / 100
let result = num1.val * num2.val
let frac = result.toString().split('.')[1] || ''
let adj = 0
if (frac.length > 10 && frac.slice(2,4)=='49') {
adj = 0.0000000001
let r = Math.round((result) * 100) / 100
let radj = Math.round((result + adj) * 100) / 100
console.log(`(num1*num2): ${num1.val} * ${num2.val} = ${result} >> round ${r} ${r==radj? '----' : '****'} w/adj ${Math.round((result) * 1000) / 1000} >> ${radj}`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment