1

Dart has an integer division operator that looks like this: ~/

final x = 22 ~/ 7;    // 3

The docs show it here, but they don't say what it is called. Am I using the correct name to call it an "integer division operator"? I've seen other people write it as the ~/ operator (here and here) but I don't know how to read that out loud. And how do you say 22 ~/ 7?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

3 Answers3

4

The name of the operator is ~/. It's a general operator that any class can implement. You could call it the "tilde slash operator" if you need to pronounce it.

The operation that ~/ performs on num/int/double is generally called "integer division" or "truncating division".

Other classes could implement a ~/ operator too, and what it would be called there would depend on what it does. If any existed. (BigInt does integer division as well, and I'm not aware of any other implementation of ~/ in common use).

There are a number of multi-symbol operators: ~/, >>, <<, [], []=. People do usually name them after the most common use: Shift operators, index/index-set operators, so it would not be inconsistent to use "integer division operator" about ~/ as a default, but it is potentially misleading. (Say, if someone inspired by C++ made streamController << x be equivalent to streamController.add(x), then reading it as "streamController left-shift x` would probably be confusing.)

lrn
  • 64,680
  • 7
  • 105
  • 121
  • 1
    So if you were making a text to speech engine for visually impaired people, what should it say when it gets to `~/`? (For example, say it is reading your answer here.) – Suragch Jun 30 '20 at 00:46
  • 2
    I'd probably go with "tilde slash operator". The `+` operator has the advantage of being a single existing mathematical operator, so it's named "plus" even when it's not used for adding. The `~/` is not a single operator, so reading it would be "tilde slash". It has no official existing name because it's not in common use. That said, people do call `>>` and `<<` shift operators, even though they are not necessarily used for shifts, so you can say "integer division operator" and probably not be misunderstood. – lrn Jun 30 '20 at 08:46
3

Your usage as integer division seems proper way as it conveys the meaning clearly. Also 22~/7 can be said as Integer division of 22 by 7

dev-aentgs
  • 1,218
  • 2
  • 9
  • 17
2

Truncating division operator

~/ is officially called the truncating division operator in the documentation for numbers, which I discovered by browsing the num source code:

  /**
   * Truncating division operator.
   *
   * If either operand is a [double] then the result of the truncating division
   * `a ~/ b` is equivalent to `(a / b).truncate().toInt()`.
   *
   * If both operands are [int]s then `a ~/ b` performs the truncating
   * integer division.
   */
  int operator ~/(num other);

Notes

  • Going off of @dev-aentgs's answer, I would read 22 ~/ 7 as "truncated division of 22 by 7" or maybe "22 divided by 7 then truncated."
  • I take @Irn's point that the "tilde slash operator" could be put to a different use, but since truncating or integer division is the only listed use in the Dart documentation (correct me if I'm wrong), I'm going to call it the truncating division operator until I have a reason not to.
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393