291B. Han Solo and Lazer Gunmysolution
I am using slopes concept in 2d geometry
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 162 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 151 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
291B. Han Solo and Lazer Gunmysolution
I am using slopes concept in 2d geometry
Name |
---|
Welcome to the wonderful world of floating-point numbers!
Can you explain me more clearly
Sorry, I wanted to write a little more, but then got distracted.
Basically floating point numbers are not precise. A computer can only hold a limited number of digits (here binary digits). E.g. executing the following C++ code gives
0.6666666865
.Therefore normally if you do some floating point arithmetik, then all your intermediate results will be slightly wrong, and you cannot compare values with
==
any more. E.g. the following code will assignb
with false, since for the computer0.3 * 3 + 0.1
will actually be slightly less than1
.If you want to compare two floating point values, you have to do it using a small epsilon.
Another thing. Always use
double
instead of float!Now to your code. I did some little testing, and actually this floating point problem might not actually be the case why your code doesn't work. Sorry. I guess I was a little bit to quick by judging your code.
Here's some small test case for you:
Your code returns
2
, although clearly all three points are on1
line. The reason is, that1/0
will give+infinity
, and-1/0
will give-infinity
. So you have to handle that case separately.