5
$\begingroup$

I want to have an operator for true division (like / in python) giving a float as the result and floor division (like // in python) giving an integer regardless of the input data types.
For example, 5 / 2 = 2.5 and 5.0 // 2.0 = 2.

I would prefer not to use // as I'm using that for line comments (with /* ... */ for block comments). What other options are there to have both true/'real' division and floor division operators?

$\endgroup$
2
  • 3
    $\begingroup$ Frame challenge: do you really need both? C-family languages have gotten away with x / y and floor(x / y) for doubles, or (double)x / y and x / y for ints, just fine. $\endgroup$
    – Bbrk24
    Jul 4, 2023 at 16:52
  • 6
    $\begingroup$ @Bbrk24 I think the reason not to use / for integer division (which floors or truncates) is that this leads to confusion when people write 2 / 3 and are surprised that the result is zero. There are many questions on Stack Overflow about exactly that, across multiple languages. Many people consider it a footgun. $\endgroup$
    – kaya3
    Jul 4, 2023 at 16:57

6 Answers 6

4
$\begingroup$

You can expand the operator to be more than a single character. For example you could do /int~ and /float~ to disambiguate which division operation to use. It's pretty verbose but if you can find a shorter term to signify which operation you can use that.

This can then be expanded to other operations to for example ensure that overflow behavior is specified for integer addition +sat32~.

$\endgroup$
8
$\begingroup$

Some options that don't involve namespaces:

  • Python: a // b
  • Visual Basic: a \ b
  • Pascal: a div b

Normal division is a / b in all the cases.

$\endgroup$
4
  • 2
    $\begingroup$ MATLAB uses backslash to indicate the order for matrix division, because matrix multiplication isn't commutative. $\endgroup$
    – Bbrk24
    Jul 5, 2023 at 3:00
  • $\begingroup$ I haven't used Visual Basic, so my opinion is possibly not very helpful, but having both / and `` in a language to denote different types of division sounds very confusing to me. Does it actually work for Visual Basic ? $\endgroup$ Jul 5, 2023 at 16:14
  • $\begingroup$ It's not just Visual Basic: I think Microsoft's GW-BASIC and QBASIC had the same distinction. And using backslash as an operator works just fine — if you're not already using it for line continuation or some similar special purpose. $\endgroup$
    – dan04
    Jul 5, 2023 at 19:41
  • $\begingroup$ While FORTRAN only used / for division, it had more operators than typographical symbols, and thus used notations like X .LT. Y for x < y. A bit ugly in upper case, especially if the blanks around the operator were omitted [as they often were] but I think using operators in lowercase, delimited with blank and dot on the left, and dot and blank on the right, could work well. $\endgroup$
    – supercat
    Nov 10, 2023 at 18:25
8
$\begingroup$

Julia's approach to this is to use ÷ for floor division (with div as an ascii alias)

$\endgroup$
4
$\begingroup$

The Verse language has an interesting approach to this problem, where division of integers returns a value of type rational, further use of which demands explicit conversion via Ceil or Floor functions. This approach could also be expanded to allow conversion to double and operations within the rational type.

$\endgroup$
4
$\begingroup$

In case you don't mind a small frame challenge: does truncating/flooring division really deserve to be an operator? It could be exposed as a function.

That:

  • Probably prevents the typical "I divided 2 by 3 and got zero, this language is broken" pitfall.
  • You can make it abundantly clear whether the division floors or truncates. But you can also not do that, for example Haskells div vs quot is completely opaque..
  • Maybe makes it more reasonable for the division to participate in "checked exceptions", if you have such a feature.
  • Maybe makes it more reasonable for the division to return an either or maybe that hold the quotient or an error/nothing, if you have such a feature.
  • Maybe makes it more reasonable for the division to return a tuple of quotient and remainder.

Those last three are based on the general idea that operators should be simple and any "advanced language features" should be reserved for functions, but of course you may not hold that view.

As with bitwise operators, there is also the commonality argument: how often do you really use truncating/flooring division? Frankly I use it far less often than bitwise operators.

$\endgroup$
1
  • $\begingroup$ One point in favor of floored division: number theory (in particular modular arithmetic) will often want to use modulus and floored division when doing computation over integer rings. $\endgroup$ Aug 16, 2023 at 16:02
3
$\begingroup$

Scoped configuration

The interesting thing of having various divisions is that the code tends to use only one per compilation unit or function.

Instead of creating a lot of cruft uncommon operators, you could configure your division usage. For example:

// All divisions are compiled to int floor by default
use lang.math.div.intfloor;

def ReplicateResult
{
    def MachineXDiv()
    {
        use lang.math.div.inttruncate; // Because reasons
        var x = a / b;
    }
}

There is a lot of rounding modes, and creating syntax for every one of then is infeasible.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .