You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Javascript/Tricky-JS-Problems/Collection-of-Tricky-JS-Questlions.md
+23-35
Original file line number
Diff line number
Diff line change
@@ -144,22 +144,11 @@ In particular only certain sums of powers of two are exactly representable. 0.5
144
144
145
145
From [The Floating-Point Guide][1]:
146
146
147
-
> **Why don’t my numbers, like 0.1 + 0.2
148
-
> add up to a nice round 0.3, and
149
-
> instead I get a weird result like
150
-
> 0.30000000000000004?**
151
-
>
152
-
> Because internally, computers use a
153
-
> format (binary floating-point) that
154
-
> cannot accurately represent a number
155
-
> like 0.1, 0.2 or 0.3 at all.
156
-
>
157
-
> When the code is compiled or
158
-
> interpreted, your “0.1” is already
159
-
> rounded to the nearest number in that
160
-
> format, which results in a small
161
-
> rounding error even before the
162
-
> calculation happens.
147
+
**Why don’t my numbers, like `0.1 + 0.2` add up to a nice round `0.3`, and
148
+
instead I get a weird result like `0.30000000000000004`?**
149
+
150
+
Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.
151
+
When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.
163
152
164
153
The site has detailed explanations as well as information on how to fix the problem (and how to decide whether it is a problem at all in your case).
165
154
@@ -203,25 +192,24 @@ It is not a peculiarity of javascript but common computer science principle.
203
192
204
193
From http://en.wikipedia.org/wiki/NaN:
205
194
206
-
> There are three kinds of operation
207
-
> which return NaN:
208
-
>
209
-
> Operations with a NaN as at least one operand
210
-
>
211
-
> Indeterminate forms
212
-
>
213
-
> - The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
214
-
> - The multiplications 0×∞ and 0×−∞
215
-
> - The power 1^∞
216
-
> - The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.
217
-
>
218
-
> Real operations with complex results:
219
-
>
220
-
> - The square root of a negative number
221
-
> - The logarithm of a negative number
222
-
> - The tangent of an odd multiple of 90 degrees (or π/2 radians)
223
-
> - The inverse sine or cosine of a number which is less than −1 or
224
-
> greater than +1.
195
+
There are three kinds of operation which return NaN:
196
+
197
+
Operations with a NaN as at least one operand
198
+
199
+
Indeterminate forms
200
+
201
+
- The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
202
+
- The multiplications 0×∞ and 0×−∞
203
+
- The power 1^∞
204
+
- The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.
205
+
206
+
Real operations with complex results:
207
+
208
+
- The square root of a negative number
209
+
- The logarithm of a negative number
210
+
- The tangent of an odd multiple of 90 degrees (or π/2 radians)
211
+
- The inverse sine or cosine of a number which is less than −1 or
212
+
greater than +1.
225
213
226
214
All these values may not be the same. A simple test for a NaN is to test `value == value` is false.
0 commit comments