Floating-Point Numbers

--- 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" ``` μž₯점: 맀우 높은 μ •λ°€λ„μ˜ 계산이 κ°€λŠ₯ν•©λ‹ˆλ‹€. 단점: κΈ°λ³Έ 숫자 νƒ€μž…λ³΄λ‹€ 느리고, μ½”λ“œκ°€ λ³΅μž‘ν•΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.