JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
fractional values | Reply
I'm wondering how we should deal with comparing and returning fractional values in our programs to get the correct output. For example, either in the first round of this tournament or in an early beta, the problem was given to write a method that calculates when two trains or trucks heading toward each other would meet. In the challenge round I challenged someone's code, and my challenge succeeded because "the method returned 50.666667 when it should have returned 50.666666" or something like that. I'm guessing that this difference is just rounding error. The problem came up again for me in the Triangle problem in a beta last week. I stored the sides of triangle as doubles, and when I tested my code, sides that I know should have been equal tested not equal, I'm guessing because of rounding in the calculations. In this case, it may be that I don't know the proper way to compare doubles. At any rate, I'm wondering if anyone else has had similar problems or has any suggestions.
-montague
Re: fractional values (response to post by montague) | Reply
For the triangle problem you could have just stored the length of the sides squared. Since x^2=y^2 iff x=y, and x,y are in the range -100 to 100 they will fit in an integer.
Re: fractional values (response to post by montague) | Reply
Whenever there is any floating point calculation involved, there should be a specified minimum accuracy, such as 6 decimal places (aka error < 1/2*10^7). You should use some metric like this if you want to compare floats or doubles.
Re: fractional values (response to post by montague) | Reply
first of all, we've been trying to stay away from doubles as much as possible (as return types). the last couple of times we've used them (in betas), we've included a method to format your return value to 2 decimal places, and i imagine we would do something like that in the future as well.
RSS