-
Notifications
You must be signed in to change notification settings - Fork 229
Numbers #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Numbers #200
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 +1,12 @@ | ||
|
|
||
|
|
||
| ```js run demo | ||
| let a = +prompt("The first number?", ""); | ||
| let b = +prompt("The second number?", ""); | ||
| let a = +prompt("¿El primer número?", ""); | ||
| let b = +prompt("¿El segundo número?", ""); | ||
|
|
||
| alert( a + b ); | ||
| ``` | ||
|
|
||
| Note the unary plus `+` before `prompt`. It immediately converts the value to a number. | ||
| Toma nota de el más unario `+` antes del `prompt`. Este convierte inmediatamente el valor a `number`. | ||
|
|
||
| Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`. | ||
| De otra manera `a` and `b` serían `string`, y la suma, su concatenación: `"1" + "2" = "12"`. | ||
This file contains hidden or 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
18 changes: 9 additions & 9 deletions
18
1-js/05-data-types/02-number/2-why-rounded-down/solution.md
This file contains hidden or 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,33 @@ | ||
| Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss. | ||
| Internamente, la fracción decimal `6.35` resulta en binario sin fin. Como siempre en estos casos, es almacenado con pérdida de precisión. | ||
|
|
||
| Let's see: | ||
| Veamos: | ||
|
|
||
| ```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. | ||
| La pérdida de precisión puede causar que el número incremente o decremente. En este caso particular el número se vuelve ligeramente menor, por ello es redondeado hacia abajo. | ||
|
|
||
| And what's for `1.35`? | ||
| ¿Y qué pasa con `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. | ||
| Aquí la pérdida de precisión hace el número algo mayor, por ello redondea hacia arriba. | ||
|
|
||
| **How can we fix the problem with `6.35` if we want it to be rounded the right way?** | ||
| **¿Cómo podemos arreglar el problema con `6.35` si queremos redondearlo de manera correcta?** | ||
|
|
||
| We should bring it closer to an integer prior to rounding: | ||
| Debemos llevarlo más cerca de un entero antes del redondeo: | ||
|
|
||
| ```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: | ||
| observa que `63.5` no tiene pérdida de precisión en absoluto. Esto es porque la parte decimal `0.5` es realmente `1/2`. Fracciones divididas por potencias de `2` son representadas exactamente en el sistema binario, ahora podemos redondearlo: | ||
|
joaquinelio marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| ```js run | ||
| alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(rounded) -> 6.4 | ||
| alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(redondeado) -> 6.4 | ||
| ``` | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.