Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* translate 1-sum-interface/solution.md * translate 1-sum-interface.task.md * translate 2-why-rounder-down task.md * translate 2 * translate 3-repeat-until-number/task.md * translate 3-repeat-until-number solution.md * translate 4-endless-loop-error * translate -random-min-max * translate 0-random-int-min-max * translate article.md to 334 * finish translate wait proofreading * update symbol * update some not perfect translate * Update solution.md * Update solution.md * Update task.md * Update solution.md * Update solution.md * Update task.md * Update solution.md * Update solution.md * Update task.md * Update solution.md * Update solution.md * Update task.md * Update task.md * Update solution.md * update * Update task.md * Update task.md * change space for num & word * change half punctutions to full * update * update * Update solution.md * Update solution.md * Update task.md * Update solution.md * Update task.md * Update solution.md * Update solution.md * Update solution.md * Update article.md * Update article.md * Update solution.md * Update task.md * Update solution.md * Update solution.md * Update solution.md * Update article.md * Update article.md
- Loading branch information
1 parent
2dc4c95
commit a09b860
Showing
13 changed files
with
186 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 8 additions & 9 deletions
17
1-js/05-data-types/02-number/2-why-rounded-down/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss. | ||
在 `6.35` 内部,小数部分是一个无限的二进制。在这种情况下,它存储的时候有一个精度的缺失。 | ||
|
||
Let's see: | ||
让我们来看看: | ||
|
||
```js run | ||
alert( 6.35.toFixed(20) ); // 6.34999999999999964473 | ||
``` | ||
|
||
The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down. | ||
精度缺失可能会导致数字的增加和减小。在这种特殊情况下,数字可能会变小了一点,这就是为什么它减小了。 | ||
|
||
And what's for `1.35`? | ||
那么 `1.35` 是怎样样的呢? | ||
|
||
```js run | ||
alert( 1.35.toFixed(20) ); // 1.35000000000000008882 | ||
``` | ||
|
||
Here the precision loss made the number a little bit greater, so it rounded up. | ||
在这里,精度缺失使得这个数字更大一些,所以这个数字变大了一些。 | ||
|
||
**How can we fix the problem with `6.35` if we want it to be rounded the right way?** | ||
**如果我们希望以正确的方式四舍五入,我们如何使用 `6.35` 为例来解决这个问题?** | ||
|
||
We should bring it closer to an integer prior to rounding: | ||
在四舍五入之前,我们应该把它接近整数: | ||
|
||
```js run | ||
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000 | ||
``` | ||
|
||
Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it: | ||
请注意 `63.5` 完全没有精度缺失。这是因为小数部分 `0.5` 实际上是 `1 / 2`。除以2的幂的分数在二进制系统中被精确地表示,现在我们可以围绕它来解决问题: | ||
|
||
|
||
```js run | ||
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(rounded) -> 6.4 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.