Hi! I've seen many codes where 1e-11 (like this) is used to compare two double numbers. Why we use this? what it we do not use this?
Thanks in advance.
Hi! I've seen many codes where 1e-11 (like this) is used to compare two double numbers. Why we use this? what it we do not use this?
Thanks in advance.
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
| Название |
|---|



Doubles can only store a certain amount of digits. If you have a decimal with an unlimited amount of digits(eg. 1/3 or sqrt 2), then the computer can't possibly store all the digits. So you can get a lot of rounding errors with doubles. If you use == for comparing doubles, like so...
Then you can run into a lot of issues, even when d1 and d2 should be equal. The safer way is to create a tolerance value epsilon. You can imagine it like an error margin: for 2 doubles to be equal, they must have an error margin of epsilon or less:
This type of comparison will save you on a lot of problems.
Thanks ... In some problem , they want that error tolerance is 10^-6. What does it means? Is it enough to use 1e-6 or 1e-7?
I also want this answer
It means in some cases answer can't be exactly correct in some problems which we use double. So system accepts answer with 10 - 6 error. For example, answer is 1.0000021 and your program prints 1.000002, so system will accept your solution if error limit is 10 - 6 (because abs(1.0000021 - 1.000002) < 10 - 6).