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
  • Correct, the suffixes are only for numerical literals (made of digits, optional minus sign, and optional decimal point). If you try to use them in other contexts, that will be a syntax error.

    You can use toDouble(), toLong(), etc. with a numerical literal, but it would be more efficient to use the appropriate suffix.

    var a = 5.toDouble(); // this is ok but it may make your code slightly less efficient. "var a = 5d;" would be preferred

    var b = (5 + 10)d; // this is invalid, suffixes such "d" only apply to numerical literals. You could use "5d + 10" or "5 + 10d" if you want. In this case, the value without a suffix will be promoted to Double. (Anytime you use an arithmetic operator such as "+" or "/" on a Double and another numeric value, the other value will be promoted to Double. It's not documented, but I think the promotion hierarchy is Number, Long, Float, Double. Any type that's lower in the hierarchy will be promoted to a type that's higher in the hierarchy when the two types are used together in arithmetic.)

    var c = 8; // Yes, this is a Number

    var d = c.toDouble(); // No, it's not possible to use a suffix after a variable, so c.toDouble() is your only option.

Comment
  • Correct, the suffixes are only for numerical literals (made of digits, optional minus sign, and optional decimal point). If you try to use them in other contexts, that will be a syntax error.

    You can use toDouble(), toLong(), etc. with a numerical literal, but it would be more efficient to use the appropriate suffix.

    var a = 5.toDouble(); // this is ok but it may make your code slightly less efficient. "var a = 5d;" would be preferred

    var b = (5 + 10)d; // this is invalid, suffixes such "d" only apply to numerical literals. You could use "5d + 10" or "5 + 10d" if you want. In this case, the value without a suffix will be promoted to Double. (Anytime you use an arithmetic operator such as "+" or "/" on a Double and another numeric value, the other value will be promoted to Double. It's not documented, but I think the promotion hierarchy is Number, Long, Float, Double. Any type that's lower in the hierarchy will be promoted to a type that's higher in the hierarchy when the two types are used together in arithmetic.)

    var c = 8; // Yes, this is a Number

    var d = c.toDouble(); // No, it's not possible to use a suffix after a variable, so c.toDouble() is your only option.

Children
No Data