## 203 - Running Lights Visibility Calculator

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 203 - Running Lights Visibility Calculator

You define EPSILON but never use it. To compare floating point numbers:
Instead of a == b, fabs(a - b) <= EPS
Instead of a < b, a < b - EPS
Instead of a <= b, a <= b + EPS

http://floating-point-gui.de/
Check input and AC output for thousands of problems on uDebug!

r2ro
New poster
Posts: 38
Joined: Thu Sep 25, 2008 9:26 am

### Re: 203 - Running Lights Visibility Calculator

Hi, thanks for that.

Do I also add the Epsilon conditions for the lights? I've done it for the conditional statements which are relevant in the computational stuff. Adding it to determine the lights messes my outputs up.

Is there anything wrong with the algorithm for determining the lights? I feel like that is where I'm going wrong, although I match the sample outputs.

Code: Select all

``````Accepted at last!
``````
Last edited by r2ro on Tue Nov 04, 2014 2:21 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 203 - Running Lights Visibility Calculator

In my AC code I add EPS = 1e-7 every time I print a floating point number. I also check that I never print 360.00
Here are the other parts of my code where I use EPS

Code: Select all

``````      if(s[i].dist > 10.0 + EPS)
printf("Lights not visible\n");
else if(s[i].bearing >= 360.0 - EPS || s[i].bearing <= 0.0 + EPS)
printf("Green Masthead Red\n");
else if(s[i].bearing > 0.0 + EPS && s[i].bearing < 2.5 - EPS)
printf("Masthead Green Red\n");
else if(s[i].bearing >= 2.5 - EPS && s[i].bearing <= 110.0 + EPS)
printf("Masthead Green\n");
else if(s[i].bearing > 110.0 + EPS && s[i].bearing < 115.0 - EPS)
printf("Stern Masthead Green\n");
else if(s[i].bearing >= 115.0 - EPS && s[i].bearing < 180.0 - EPS)
printf("Stern Masthead\n");
else if(s[i].bearing >= 180.0 - EPS && s[i].bearing <= 245.0 + EPS)
printf("Masthead Stern\n");
else if(s[i].bearing > 245.0 + EPS && s[i].bearing < 250.0 - EPS)
printf("Red Masthead Stern\n");
else if(s[i].bearing >= 250.0 - EPS && s[i].bearing <= 357.5 + EPS)
printf("Red Masthead\n");
else if(s[i].bearing > 357.5 + EPS && s[i].bearing < 360.0 - EPS)
printf("Green Red Masthead\n");
...
if(s[i].dist <= 10.0 + EPS && s[i].dist_after_3_min < s[i].dist - EPS && bearing_diff(s[i].bearing, s[i].bearing_after_3_min) <= 2.0 + EPS)
printf("** Collision warning
``````
Check input and AC output for thousands of problems on uDebug!

r2ro
New poster
Posts: 38
Joined: Thu Sep 25, 2008 9:26 am

### Re: 203 - Running Lights Visibility Calculator

That's so helpful. Thanks! Could you clarify how you handled the epsilon when displaying floating point numbers? I finally found a test case where my program and uDebug (your solution) outputs different values:

Code: Select all

``````Gotcha!
2
Ownship
0.0 0.0 0.0 1.0
ShouldBe1.01
1.005 0.0 111.1 2.2
ShouldBe1.02
1.015 0.0 222.2 2.3
``````
This is my output (which is wrong, apparently)

Code: Select all

``````Scenario: Gotcha!

Boat ID         Bearing      Distance   Lights (left to right)
---------------------------------------------------------------
ShouldBe1.01     158.90        1.00     Stern Masthead
ShouldBe1.02      47.80        1.01     Masthead Green
***************************************************************
``````
And the output of uDebug is:

Code: Select all

``````Scenario: Gotcha!

Boat ID         Bearing      Distance   Lights (left to right)
---------------------------------------------------------------
ShouldBe1.01     158.90        1.01     Stern Masthead
ShouldBe1.02      47.80        1.02     Masthead Green
***************************************************************
``````
Thanks!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 203 - Running Lights Visibility Calculator

Here are some more sections of my AC code:

Code: Select all

``````      char st[100];
sprintf(st, "%.2lf", s[i].bearing + EPS);
if(!strcmp(st, "360.00"))
strcpy(st, "0.00");
printf("%s ", st);
...
printf("%.2lf", s[i].dist + EPS);
``````
FYI, http://www.udebug.com/UVa/203 is not my code.
Check input and AC output for thousands of problems on uDebug!

r2ro
New poster
Posts: 38
Joined: Thu Sep 25, 2008 9:26 am

### Re: 203 - Running Lights Visibility Calculator

Finally! Accepted after a lot of frustration! Oh my goodness, sharing with me where you put those EPSILONs really, really, really, really helped!!!

Thank you so much brianfry713! I can't thank you enough!

LlatzerandToni
New poster
Posts: 15
Joined: Sun Apr 23, 2006 1:35 pm

### Re: 203 - Running Lights Visibility Calculator

Input

Code: Select all

``````Test
1
Ownship
0 0 0 0
Ship
-10 0 90.00001 0.0001``````
Output

Code: Select all

``````Scenario: Test

Boat ID         Bearing      Distance   Lights (left to right)
---------------------------------------------------------------
Ship               0.00       10.00     Green Red Masthead
** Collision warning -->Ship:  [b]Distance =  10.00[/b]
***************************************************************
``````
Look out with the double space before the 10.00.
Lol

RandyWaterhouse
New poster
Posts: 4
Joined: Tue Dec 13, 2016 1:41 pm

### Re: 203 - Running Lights Visibility Calculator

I'm doing this problem in Python3. My code's output is identical to the "Accepted Output" for the two test data sets in udebug (https://www.udebug.com/UVa/203) yet it still gets a "wrong answer" from UVA. I suspect it still has something to do with the EPS and/or the rounding of output values but I'm at a loss regarding how to debug it further.

Any help appreciated!

Thanks, Peter