--- title: 'Floating-Point Numbers' author: 'Hun Im' date: 2024-10-23T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript'] --- In JavaScript, floating-point numbers are represented using the 64-bit floating-point format defined by the IEEE 754 standard. Some numbers cannot be represented exactly, which can lead to errors. ### Issues with Floating-Point Numbers 1. **Limitations of Binary Representation**: Some decimal fractions cannot be represented exactly in binary form. For example, numbers like 0.1 or 0.2 become repeating decimals when converted to binary floating-point and are stored as approximations. ```js console.log(0.1 + 0.2); // μμ: 0.3, μ€μ : 0.30000000000000004 ``` 2. **Loss of Precision**: Small errors during calculations can accumulate into significant errors. This is particularly problematic in iterative calculations or financial computations. ### Solutions 1. Using the `toFixed()` Method The `toFixed()` method rounds a number to a specified number of decimal places and returns it as a string. ```js let sum = 0.1 + 0.2; console.log(sum.toFixed(2)); // "0.30" ``` **Note**: Since toFixed() returns a string, you'll need to convert it back to a number using parseFloat() or Number() if further calculations are required. 2. Calculations Considering Decimal Places Convert all numbers to integers before performing calculations, then convert the result back to a floating-point number. ```js let a = 0.1; let b = 0.2; let sum = (a * 100) + (b * 100); // 10 + 20 = 30 sum = sum / 100; // 0.3 console.log(sum); // 0.3 ``` Pros: Avoids floating-point errors. Cons: You need to manage scaling factors depending on the range of numbers you're processing. 3. Comparison Using `EPSILON` When comparing two floating-point numbers for near equality, use `Number.EPSILON`. ```js function areAlmostEqual(a, b) { return Math.abs(a - b) < Number.EPSILON; } console.log(areAlmostEqual(0.1 + 0.2, 0.3)); // true ``` 4. Using External Libraries * Decimal.js ```js const Decimal = require('decimal.js'); let a = new Decimal(0.1); let b = new Decimal(0.2); let sum = a.plus(b); console.log(sum.toString()); // "0.3" ``` * BigNumber.js ```js const BigNumber = require('bignumber.js'); let a = new BigNumber(0.1); let b = new BigNumber(0.2); let sum = a.plus(b); console.log(sum.toString()); // "0.3" ``` Pros: Allows for very high-precision calculations. Cons: Slower than using the basic number type, and the code can become more complex.
Β
--- title: 'λΆλ μμμ ' author: 'μν' date: 2024-10-23T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript'] --- JavaScriptμμ λΆλμμμ μ μ«μλ₯Ό νννκΈ° μν΄ IEEE 754 νμ€μ 64λΉνΈ λΆλμμμ νμμ μ¬μ©ν©λλ€. μΌλΆ μ«μλ₯Ό μ ννκ² νννμ§ λͺ»νκ³ μ€μ°¨κ° λ°μν μ μμ΅λλ€. ### λΆλμμμ μ λ¬Έμ μ 1. μ΄μ§μ ννμ νκ³: μΌλΆ μμ§μ μμλ μ΄μ§μλ‘ μ ννκ² ννλ μ μμ΅λλ€. μλ₯Ό λ€μ΄, 0.1μ΄λ 0.2μ κ°μ μ«μλ μ΄μ§ λΆλμμμ μΌλ‘ ννν λ 무νμμκ° λμ΄ κ·Όμ¬μΉλ‘ μ μ₯λ©λλ€. ```js console.log(0.1 + 0.2); // μμ: 0.3, μ€μ : 0.30000000000000004 ``` 2. μ λ°λ μμ€: κ³μ° κ³Όμ μμ μμ μ€μ°¨λ€μ΄ λμ λμ΄ ν° μ€μ°¨λ‘ μ΄μ΄μ§ μ μμ΅λλ€. μ΄λ νΉν λ°λ³΅ κ³μ°μ΄λ κΈμ΅ κ³μ°μμ λ¬Έμ κ° λ©λλ€. ### ν΄κ²°μ± 1. toFixed() λ©μλ μ¬μ© toFixed() λ©μλλ μ«μλ₯Ό μ§μ λ μμμ μ리κΉμ§ λ°μ¬λ¦Όνμ¬ λ¬Έμμ΄λ‘ λ°νν©λλ€. ```js let sum = 0.1 + 0.2; console.log(sum.toFixed(2)); // "0.30" ``` μ£Όμμ¬ν: toFixed()λ λ¬Έμμ΄μ λ°ννλ―λ‘, μΆκ° κ³μ°μ΄ νμνλ©΄ parseFloat()λ Number()λ‘ μ«μλ‘ λ³νν΄μΌ ν©λλ€. 2. μμμ μ리μλ₯Ό κ³ λ €ν κ³μ° λͺ¨λ μ«μλ₯Ό μ μλ‘ λ³ννμ¬ κ³μ°νκ³ , κ²°κ³Όλ₯Ό λ€μ μμλ‘ λ³νν©λλ€. ```js let a = 0.1; let b = 0.2; let sum = (a * 100) + (b * 100); // 10 + 20 = 30 sum = sum / 100; // 0.3 console.log(sum); // 0.3 ``` μ₯μ : λΆλμμμ μ€μ°¨λ₯Ό νΌν μ μμ΅λλ€. λ¨μ : μ²λ¦¬ν΄μΌ ν μ«μμ λ²μμ λ°λΌ μ€μΌμΌλ§ ν©ν°λ₯Ό κ΄λ¦¬ν΄μΌ ν©λλ€. 3. EPSILONμ μ¬μ©ν λΉκ΅ λ λΆλμμμ μ«μκ° κ±°μ κ°μμ§ λΉκ΅ν λ Number.EPSILONμ μ¬μ©ν©λλ€. ```js function areAlmostEqual(a, b) { return Math.abs(a - b) < Number.EPSILON; } console.log(areAlmostEqual(0.1 + 0.2, 0.3)); // true ``` 4. μΈλΆ λΌμ΄λΈλ¬λ¦¬ μ¬μ© * Decimal.js ```js const Decimal = require('decimal.js'); let a = new Decimal(0.1); let b = new Decimal(0.2); let sum = a.plus(b); console.log(sum.toString()); // "0.3" ``` * BigNumber.js ```js const BigNumber = require('bignumber.js'); let a = new BigNumber(0.1); let b = new BigNumber(0.2); let sum = a.plus(b); console.log(sum.toString()); // "0.3" ``` μ₯μ : λ§€μ° λμ μ λ°λμ κ³μ°μ΄ κ°λ₯ν©λλ€. λ¨μ : κΈ°λ³Έ μ«μ νμ λ³΄λ€ λλ¦¬κ³ , μ½λκ° λ³΅μ‘ν΄μ§ μ μμ΅λλ€.