Complete
over 2 years ago

Not a CIQ issue. For more context, here's a link to the Bug Reports FAQ, https://forums.garmin.com/developer/connect-iq/w/wiki/5/bug-reports-faq.

Next time, consider starting a discussion. A discussion can be escalated to a bug report.

Unable to test km to mile conversion of half marathon

```

  (:distance)
  module Distance {

    function toMiles(dist) {
      var mi = dist.toDouble() * 0.6213711922d;
      return mi.toDouble();
    }

  }

I have the following Test:

// t.is() == test.assertEqualmessage
// t.ok() == test.assertMessage()
// t.pass() == test.assertMessage(true, msg)
// t.fail() == test.assertMessage(false, msg)

    (:test)
    function testDistance(logger) {

      t.is(dist.toMiles(1), 0.6213711922d, "1km is 0.6213711922 miles");

      var mi = dist.toMiles(21.0975);
      Sys.println(mi); // prints 13.109379

      var mile = 13.109379d;
      Sys.println(mile); // print 13.109379

      if (mile == mi) {
        t.pass("A mile is a mile");
      }
      else {
        t.fail("A mile isn't a mile");
      }

      t.is(dist.toMiles(21.0975), 13.109378728d, "Half marathon distance");
      t.ok(dist.toMiles(21.0975) == 13.109379d, "Half marathon distance");

      return true;
    }

This fails on Exception: ASSERTION FAILED: A mile isn't a mile    File: UnitTests

I'm kinda expecting it all to work.


The problem is because I cannot see what the test sees (as described here https://forums.garmin.com/developer/connect-iq/i/bug-reports/test-assertequalmessage-and-friends-to-show-incorrectness), so from there I'm sitting the values should be correct. Working around it by adding a toString() doesn't work, it still thinks it isn't the same value.

Parents
  • IMO the actual problem here is that System.println(<double>) doesn't print all the digits by default, but instead rounds to 6 places. This is probably because they figured nobody wants to output more than 6 decimals on a watch display.

    For example, javascript behaves differently:

    > function toMiles(dist) { return 0.6213711922 * dist; }
    undefined
    > toMiles(21.0975) == 13.109379
    false
    > toMiles(21.0975)
    13.1093787274395
    > toMiles(21.0975) == 13.1093787274395
    true
    In monkey C:

    function toMiles(dist) {
          var mi = dist.toDouble() * 0.6213711922d;
          return mi.toDouble();
    }
    // test code
    var mi = toMiles(21.0975);
    System.println("miles = " + mi.format("%.20g")); // prints "13.109378632625780625"
    System.println(mi == 13.109378632625780625d ? "true" : "false"); // prints "true"
Comment
  • IMO the actual problem here is that System.println(<double>) doesn't print all the digits by default, but instead rounds to 6 places. This is probably because they figured nobody wants to output more than 6 decimals on a watch display.

    For example, javascript behaves differently:

    > function toMiles(dist) { return 0.6213711922 * dist; }
    undefined
    > toMiles(21.0975) == 13.109379
    false
    > toMiles(21.0975)
    13.1093787274395
    > toMiles(21.0975) == 13.1093787274395
    true
    In monkey C:

    function toMiles(dist) {
          var mi = dist.toDouble() * 0.6213711922d;
          return mi.toDouble();
    }
    // test code
    var mi = toMiles(21.0975);
    System.println("miles = " + mi.format("%.20g")); // prints "13.109378632625780625"
    System.println(mi == 13.109378632625780625d ? "true" : "false"); // prints "true"
Children