<p>I want to have an operator for true division (like <code>/</code> in python) giving a float as the result and floor division (like <code>//</code> in python) giving an integer regardless of the input data types.<br />
For example,
<code>5 / 2</code> = 2.5 and <code>5.0 // 2.0</code> = 2.</p>
<p>I would prefer not to use <code>//</code>  as I'm using that for line comments (with <code>/* ... */</code> for block comments). What other options are there to have both true/'real' division and floor division operators?</p>

<syntax><operators><division>
Ways to have operators for both normal and floor division
CC BY-SA 4.0
2023-07-04T16:52:56.893Z
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.
Bbrk24
CC BY-SA 4.0
2023-07-04T16:57:42.310Z
@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.
kaya3
<p>The Verse language has an <a href="https://dev.epicgames.com/documentation/en-us/uefn/int-in-verse#math" rel="nofollow noreferrer">interesting approach</a> to this problem, where division of integers returns a value of type <code>rational</code>, further use of which demands explicit conversion via <code>Ceil</code> or <code>Floor</code> functions. This approach could also be expanded to allow conversion to <code>double</code> and operations within the <code>rational</code> type.</p>

CC BY-SA 4.0
2023-07-05T07:17:46.320Z
2023-07-05T07:17:46.320Z
abel1502
<p>Some options that don't involve namespaces:</p>
<ul>
<li>Python: <code>a // b</code></li>
<li>Visual Basic: <code>a \ b</code></li>
<li>Pascal: <code>a div b</code></li>
</ul>
<p>Normal division is <code>a / b</code> in all the cases.</p>

CC BY-SA 4.0
2023-07-05T02:31:16.427Z
2023-07-05T02:31:16.427Z
user23013
CC BY-SA 4.0
2023-07-05T03:00:21.837Z
MATLAB uses backslash to indicate the order for matrix division, because matrix multiplication isn't commutative.
Bbrk24
CC BY-SA 4.0
2023-07-05T16:14:46.743Z
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 ?
Tom Tsagkatos
CC BY-SA 4.0
2023-07-05T19:41:39.867Z
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.
dan04
CC BY-SA 4.0
2023-11-10T18:25:39.640Z
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.
supercat
<p>Julia's approach to this is to use ÷ for floor division (with div as an ascii alias)</p>

CC BY-SA 4.0
2023-07-05T04:19:15.203Z
2023-07-05T04:19:15.203Z
Oscar Smith
<h2>Scoped configuration</h2>
<p>The interesting thing of having various divisions is that the code tends to use only one per compilation unit or function.</p>
<p>Instead of creating a lot of <strike>cruft</strike> uncommon operators, you could <em>configure</em> your division usage. For example:</p>
<pre><code>// 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;
    }
}
</code></pre>
<p>There is a <a href="https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer" rel="nofollow noreferrer">lot of rounding modes</a>, and creating syntax for every one of then is infeasible.</p>

CC BY-SA 4.0
2023-07-05T16:09:23.923Z
2023-08-16T15:14:38.030Z
2023-08-16T15:14:38.030Z
André L F S Bacci
<p>You can expand the operator to be more than a single character. For example you could do <code>/int~</code> and <code>/float~</code> 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.</p>
<p>This can then be expanded to other operations to for example ensure that overflow behavior is specified for integer addition <code>+sat32~</code>.</p>

CC BY-SA 4.0
2023-07-04T18:26:06.517Z
2023-07-04T18:26:06.517Z
ratchet freak
# Ways to have operators for both normal and floor division
Posted by: username_0 on 2023-07-04
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?
**Tags:** syntax operators division
> **Comments:**
> **username_1**: 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.  
> **username_2**: @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.  
---
## 5 answers
### Answer by username_10 on 2023-07-04. Score: 4 | Accepted answer
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~`.
---
### Answer by username_4 on 2023-07-05. Score: 8
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.
> **Comments:**
> **username_1** (2023-07-05): MATLAB uses backslash to indicate the order for matrix division, because matrix multiplication isn't commutative.  
> **username_5** (2023-07-05): 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 ?  
> **username_6** (2023-07-05): 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.  
> **username_7** (2023-11-10): 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.  
---
### Answer by username_8 on 2023-07-05. Score: 8
Julia's approach to this is to use ÷ for floor division (with div as an ascii alias)
---
### Answer by username_3 on 2023-07-05. Score: 4
The Verse language has an [interesting approach](https://dev.epicgames.com/documentation/en-us/uefn/int-in-verse#math) 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.
---
### Answer by username_9 on 2023-07-05. Score: 3
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.
---
