I know that certain numbers will get slight variations from their original value. Eg. 0.1 + 0.2 -> 0.30000000000000004. But if I do Math.round(0.30000000000000004 * 100) / 100, I will get the correct answer -> 0.3. I ran a Javascript test and found that the results will accurate at least up to 1e+10. Are there any caveats to doing this?
Tag: floating-point
Why is 5726718050568503296 truncated in JS
As per the standard ES implements numbers as IEEE754 doubles. And per https://www.binaryconvert.com/result_double.html?decimal=053055050054055049056048053048053054056053048051050057054 and other programming languages https://play.golang.org/p/5QyT7iPHNim it looks like the 5726718050568503296 value can be represented exactly without losing precision. Why it loses 3 significant digits in JS (reproduced in latest stable google chrome and firefox) This question was triggered initially from the replicate javascript unsafe numbers in golang
JavaScript seems to be doing floating point wrong (compared to C)
From everything I’ve been able to find online, JavaScript allegedly uses IEEE 754 doubles for its numbers, but I have found numbers that can work in C doubles, but not in JavaScript. For example, prints 131621703842267136.000000 NOTE: IN AN EARLIER VERSION OF THE QUESTION I COPPIED THE WRONG NUMBER FOR C, but in JavaScript outputs 131621703842267140. From everything I’ve read
Converting float values to a grayscale hex color value
this question is quick and simple. I’ve a 2d array of floats ( 0,0000000 to 1,0000000 ) and i want to convert those numbers to a color value ( #000000 to #ffffff ). note that i’m talking about just grayscale values. 0 = black | … | 0.5 = middle gray | … | 1 = white does anyone know