Beta

Draw a Clock

Description
Loading description...
Date Time
Fundamentals
  • Please sign in or sign up to leave a comment.
  • donaldsebleung Avatar

    This comment has been hidden.

    • jimmyhay Avatar

      Yes I realised this is a problem. I have not factored in that the hour hand with move to the location of the minutes. I'll fix this asap. Thanks!

    • jimmyhay Avatar

      The hour hand should now move in increments of a minute and then minute hand should just move in increments of a minute as it should have been doing so before.

      Question marked resolved by jimmyhay 8 years ago
  • Voile Avatar

    There are still occasionally cases in random tests like this:

    x-coord of hours was wrong - Expected actual value -60 to approximately equal expected value -61 (accepted relative error: 1e-9)
    

    It happens when the coordinates are exactly an integer but get floating point errors, e.g y: 14.000000000000004.


    Suggestion: If you're using AssertApproxEquals anyways, you don't need to round/floor/truncate the results anymore, so might as well return the numbers directly?

    • jimmyhay Avatar

      Okay, I think I've fixed this now. I have floor'ed the final answer given by the solution so it should match up with the requested answer. Could you give me your solution so I can test it again and see if it works with yours?

    • JohanWiltink Avatar

      If you test with assertApproxEquals, you should not do any rounding. It defeats the purpose.

    • jimmyhay Avatar

      Okay, I removed the floor's. Could you tell me how assertApproxEquals works, because it seems to cause some issues with my Example Test Cases now. What is the accuracy that it expects from the answer?

    • JohanWiltink Avatar

      See below. .toString works on assertApproxEquals.

    • JohanWiltink Avatar

      You did not remove the floors.

    • jimmyhay Avatar

      I think I have fixed this now, could you give it a test and see what happens?

    • JohanWiltink Avatar

      Flooring is out. Now you have reversed expected values of x and y for minutes and seconds, but not hours. Also, there's a - too much in there somewhere.

      Could you please check your stuff before you publish it? Just solve the kata yourself like a normal user, and check your answers for plausibility? Add a couple of easy, fixed tests for this, 1200, 0300, 0730, and check the bloody signs of the expected values. I'm getting a bit tired of this incremental stuff.

    • donaldsebleung Avatar

      Thanks for editing the actual Test Cases accordingly but I think you forgot to edit the Sample Test Cases as well ;)

      As for whether the actual Test Cases work, they are buggy when the expected value is extremely close to zero (somewhere around 1e-15) but that is not your fault (in fact it is mine, I implemented this particular method for the Codewars JS testing framework but I proposed a fix here a while ago) so you may leave it as is :)

    • jimmyhay Avatar

      Obviously I have already done what you suggested. I have put in easy solutions and tested them against the results and on my side they seem to be all correct. Sorry to trouble you.

    • JohanWiltink Avatar

      What I see is you're testing the reference solution against itself and you don't have any fixed Submit Tests. You won't find (existing or newly-introduced) bugs in your reference solution that way.

      Either add some fixed Submit Tests, or have (very!) different example and reference solutions (or both. and then fixed-test your reference solution as well. you can take that out before publishing).

    • JohanWiltink Avatar

      Oh, you have (actual,expected) switched in the arguments of assertApproxEquals. That makes for wrong messages on failure. Both Example and Submit tests.

    • JohanWiltink Avatar

      But my solution just passed!

      It would seem the tests are now correct. Let's hear from some other people as well, but this looks hopeful.

    • jimmyhay Avatar

      Phew! Yeh it seems to be working now! I think some of my maths was wrong in the original solution. Sorry it was so drawn out, I was about to give up!

    • smile67 Avatar

      For me it seems correct now (haven't tested/submitted before);-)... So well done:-)...

    • jimmyhay Avatar

      I think this is all working now.

      Issue marked resolved by jimmyhay 8 years ago
  • JohanWiltink Avatar

    Tests are not equipped to handle -0 values. These occur. If -0 is not accepted as 0, I feel this should at least be mentioned. But I really feel they should be accepted as 0.

  • JohanWiltink Avatar

    On analog clocks, the hands move continuously, not instantaneously. You are expecting wrong values because you do not account for this.

    (Even then, there are rounding errors.)

    • jimmyhay Avatar

      I'm sure there are some analog clocks that have a ticking mechanism, in which case they should move in an instantaneous manner wouldn't you say? I know what you mean, there are some clocks that do work with a continuous motion, but I think my problem is still a valid task.

    • JohanWiltink Avatar

      Having the hours hand only move to the next full hour on the whole hour is a valid task, but it's not what people expect when they read "analog clock". Would you expect the hours hand to be at twelve at 00:30?

      It's worth an explicit mention in the description.

    • jimmyhay Avatar

      Ah yes I see what you mean. I will try and code it so that the hour hand has to be in the correct position depending on the number of minutes to the nearest minute and the minute hand with just click to the nearest minute. Do you think that would be a good solution?

    • JohanWiltink Avatar

      I initially had all three hands moving once per second, but you can have the hour hand moving once per minute, no problem. Just specify it.

    • jimmyhay Avatar

      The hour hand should now move once per minute.

      Issue marked resolved by jimmyhay 8 years ago
  • JohanWiltink Avatar

    Aside from the inaccuracy mistakes below, y seems to increase down ??? which is a legal choice, but definitely one to mention in the description.

    The example in the description is not a valid output of the requested function and is missing y: in the hours part.

    Also, you may want to use assertDeepEquals instead of assertSimilar. The output is prettier.

    • jimmyhay Avatar

      I had stated in the description that it is set to cartestian co-ordinates but the code didn't represent this. This should be fixed now. Y should be positive when heading north and negative when heading south from (0,0). I also switched it from assertSimilar to assertDeepEquals, thanks.

      Suggestion marked resolved by jimmyhay 8 years ago
    • JohanWiltink Avatar

      OK, that seems all fixed.

  • Voile Avatar

    Please explain how the values are turned into integers. As far as I can observe from the example tests none of the floor, ceil, round, trunc works.

    • jimmyhay Avatar

      So the values are turned into integers with Math.floor when you calculate the position using the trigonometric function. There was an issue in there with the Y values being the wrong way around (positive when they should be negative and vice versa), but this should be fixed now. Perhaps try again and see if you can get solve it.

    • JohanWiltink Avatar

      Still some rounding differences. Values are never off by more than one. Seems to happen on all hands; if the difference is in x expected value is one more than my actual value, if in y it's one less.

    • JohanWiltink Avatar

      Tried to submit until I got lucky.

      Seeing the reference solution, I'm not surprised there are rounding errors.

      The best way to do this kind of thing is not rounding any values, and comparing them with a margin for error. Test.assertApproxEquals was invented for this situation. If you specify floor, and have final values very close to integers, it is little surprising it sometimes comes out plus or minus one. With all the calculating you do, you introduce a lot of inaccuracies, small ones, but they accumulate, and then they get amplified by the flooring.

    • JohanWiltink Avatar

      Please explain how the values are turned into integers.

      Can't be done.

      At least not better than is already in there.

    • jimmyhay Avatar

      Could you give me an example of how to use Test.assertApproxEquals with object properties, so that I can compare all the seperate values for each hand?

    • JohanWiltink Avatar
      let actual = { hours: { x:1, y: 2 }, minutes: { x: 3, y: 4 }, seconds: { x: 5, y: 6 } };
      let expected = { same same }
      Test.assertApproxEquals( actual.hours.x, expected.hours.x, "x-coord of hours was wrong" );
      Test.assertApproxEquals( actual.hours.y, expected.hours.y, "y-coord of hours was wrong" );
      Test.assertApproxEquals( actual.minutes.x, expected.minutes.x, "x-coord of minutes was wrong" );
      and same same
      

      You have to compare each and every property, which is a bit of writing repetitive code, but you just can't approxEquals composite data types. You could put it in a nested for loop and assert( actual[hand][coord], expected[same][same], `smart failure message` ), but that's probably as much work as just writing it out.

    • jimmyhay Avatar

      Okay I've put the new test cases in so it should test for an approximate answer. Do you know what level of approximation it tests for, like what are the limits?

      Issue marked resolved by jimmyhay 8 years ago
    • JohanWiltink Avatar

      from the Test.assertApproxEquals source:

      if (Math.abs(expected) <= 1) {
        Test.expect(Math.abs(expected - actual) <= 1e-9, msg, options);
      } else {
        Test.expect(Math.abs((expected - actual) / expected) <= 1e-9, msg, options);
      }