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
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.
10
-
11
-
And what's for `1.35`?
7
+
این از دست دادن دقت میتواند در هم مقدار عدد را افزایش یا کاهش دهد. در این حالت خاص، عدد اندکی کوچک میشود، به همین علت به پایین رند میشود.
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:
28
-
24
+
توجه داشته باشید که `63.5` هیچ از دست دادن دقتی ندارد. به خاطر قسمت اعشاری آن `0.5` که در اصل `1/2` است. کسرهای تقسیم شده بر دو به درستی در سیستم دودویی نمایش داده میشوند، حالا میتوانیم آن را رند کنیم:
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/3-repeat-until-number/solution.md
+3-4
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,8 @@ function readNumber() {
15
15
alert(`Read: ${readNumber()}`);
16
16
```
17
17
18
-
The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
18
+
جواب اندکی پیچیدهست که به خاطر این است که باید `null`/خالی را هم در نظر بگیریم.
19
19
20
-
So we actually accept the input until it is a "regular number". Both `null` (cancel) and empty line also fit that condition, because in numeric form they are `0`.
21
-
22
-
After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
20
+
پس ما تا وقتی ورودی را میپذیریم که عدد عادی باشد. هردوی `null` (cancel) و خط خالی هم در این شرایط میگنجد، چونکه در فرم عددی صفر هستند.
23
21
22
+
بعد از اینکه برنامه متوقف شد بایستی `null` و مخصوصا خطهای خالی (خروجی `null`) را در نظر بگیریم، چون تبدیل آنها به عدد صفر برمیگرداند.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/9-random-int-min-max/solution.md
+11-12
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# The simple but wrong solution
1
+
# جواب ساده اما اشتباه
2
2
3
-
The simplest, but wrong solution would be to generate a value from `min`to`max`and round it:
3
+
جواب اشتباه اما ساده این است که مقداری از `min`تا`max`رو تولید کنیم و رندش کنیم:
4
4
5
5
```js run
6
6
functionrandomInteger(min, max) {
@@ -11,23 +11,22 @@ function randomInteger(min, max) {
11
11
alert( randomInteger(1, 3) );
12
12
```
13
13
14
-
The function works, but it is incorrect. The probability to get edge values `min`and`max`is two times less than any other.
14
+
این تابع کار میکند اما غلط است. احتمال اینکه مقادیر لبه `min`و`max`را در نتیجه بگیریم، نصف بقیهست.
15
15
16
-
If you run the example above many times, you would easily see that `2` appears the most often.
16
+
اگر شما مثال بالا را به کرات اجرا کنید، میبینید که عدد ۲ اکثر اوقات ظاهر میشود.
17
17
18
-
That happens because `Math.round()`gets random numbers from the interval `1..3`and rounds them as follows:
18
+
به این علت است که `Math.round()`اعداد تصادفی از بازه `1..3`را میگیرد و به شکل زیر رند میکند.
19
19
20
20
```js no-beautify
21
21
values from 1... to 1.4999999999 become 1
22
22
values from 1.5... to 2.4999999999 become 2
23
23
values from 2.5... to 2.9999999999 become 3
24
24
```
25
25
26
-
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
26
+
حالا میتوانیم به وضوح ببینیم که عدد ۲، دو برابر عدد یک مقادیر به آن نسبت داده میشود. همینطور هم برای عدد ۳.
27
+
# راه حل صحیح
27
28
28
-
# The correct solution
29
-
30
-
There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
29
+
راه حالهای صحیح زیادی وجود دارد. یکی از آنها تنظیم نقاط مرزیست. برای اطمینان یافتن از ازینکه بازهها برابرند میتوانیم مقادیر را از `0.5 تا 3.5` تولید کنیم، سپس احتمال لازم صحیح را به حالات لبه نسبت دهیم:
31
30
32
31
```js run
33
32
*!*
@@ -41,7 +40,7 @@ function randomInteger(min, max) {
41
40
alert( randomInteger(1, 3) );
42
41
```
43
42
44
-
An alternative way could be to use `Math.floor`for a random number from `min`to`max+1`:
43
+
راه دیگر میتواند استفاده از `Math.floor`باشد برای یک عدد تصادفی از `min`تا`max+1`:
45
44
46
45
```js run
47
46
*!*
@@ -55,12 +54,12 @@ function randomInteger(min, max) {
55
54
alert( randomInteger(1, 3) );
56
55
```
57
56
58
-
Now all intervals are mapped this way:
57
+
حال، تمام بازه ها بدین شکل میشوند:
59
58
60
59
```js no-beautify
61
60
values from 1... to 1.9999999999 become 1
62
61
values from 2... to 2.9999999999 become 2
63
62
values from 3... to 3.9999999999 become 3
64
63
```
65
64
66
-
All intervals have the same length, making the final distribution uniform.
65
+
همه ی بازهها الان طول یکسانی دارند و در نهایت توزیع یکسانی دارند.
0 commit comments