Page 2 of 2
Re: 203 - Running Lights Visibility Calculator
Posted: Thu Oct 30, 2014 12:21 am
by brianfry713
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/
Re: 203 - Running Lights Visibility Calculator
Posted: Thu Oct 30, 2014 2:04 am
by r2ro
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.
Re: 203 - Running Lights Visibility Calculator
Posted: Fri Oct 31, 2014 8:02 pm
by brianfry713
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
Re: 203 - Running Lights Visibility Calculator
Posted: Sat Nov 01, 2014 8:04 am
by r2ro
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!
Re: 203 - Running Lights Visibility Calculator
Posted: Tue Nov 04, 2014 12:12 am
by brianfry713
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.
Re: 203 - Running Lights Visibility Calculator
Posted: Tue Nov 04, 2014 2:19 am
by r2ro
Re: 203 - Running Lights Visibility Calculator
Posted: Tue May 12, 2015 12:07 pm
by LlatzerandToni
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.
Re: 203 - Running Lights Visibility Calculator
Posted: Tue Dec 13, 2016 1:46 pm
by RandyWaterhouse
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