Acknowledged

Cannot Calculate Mod (%) with Doubles

I get an error when I try to calculate a mod with two doubles. There is no error when doing the equivalent calculation with a formula.

var a = 5 as Double;  // or 5d
var b = 2 as Double;  // or 2d
var c = a % b;  // ERROR: Cannot perform operation 'mod' on types '$.Toybox.Lang.Double' and '$.Toybox.Lang.Double'.
var d = a - (Math.floor(a/b)) * b;  // No error

Environment:

Mac OS Sequoia 15.1.1 (24B2091)

Connect IQ SDK 7.4.3

Monkey C Extension 1.1.0

VS Code 1.96.4

----------

EDIT: It appears that it's normal for the mod operator to not work with doubles. I would like to recommend mentioning this in the Reference Guide Arithmetic Operators section along with any other exceptions.

Parents
  • More examples:

    var a = 5; // Number (no decimal point, and no suffix)
    var b = 5l; // Long (l suffix)
    var c = 5.0; // Float (decimal point, and no suffix)
    var d = 5f; // Float (f suffix)
    var e = 5.0f; // Float (f suffix)
    var f = 5d; // Double (d suffix)
    var g = 5.0d; // Double (d suffix)
Comment
  • More examples:

    var a = 5; // Number (no decimal point, and no suffix)
    var b = 5l; // Long (l suffix)
    var c = 5.0; // Float (decimal point, and no suffix)
    var d = 5f; // Float (f suffix)
    var e = 5.0f; // Float (f suffix)
    var f = 5d; // Double (d suffix)
    var g = 5.0d; // Double (d suffix)
Children
  • A suffix is used for other things too..

    var ba=[0,1]b;         //bytrArray

    var na=[0,1];          //array of numbers

  • Thanks! Are the suffixes typically used only at the ends of actual numbers/digits, and I assume the Lang functions are more appropriate after a variable or formula? For example:

    var a = 5.toDouble()  // unusual notation? use suffix instead?
    var b = (5 + 10)d  // unusual notation? use .toDouble() instead?
    var c = 8  // number
    var d = c.toDouble()  // necessary because "cd" would have a different meaning