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~.
Hello, Can someone please explain the me the difference between those two arithmetic signs because am new to programming python:)
syntax - Ways to have operators for both normal and floor division - Programming Language Design and Implementation Stack Exchange
Java - How to do floor division? - Stack Overflow
Is there any difference between using floor division and int()
C integer division and floor - Stack Overflow
What is the difference between division and floor division?
Regular division can give you a fraction as an answer, whereas floor division will always yield an integer answer. For example, 35 divided by 4 gives 8.75, and floor division operation gives 8.
What is the floor division of 9/2?
The floor division operation of 9 divided by 2 is 4.
The standard division of 9/2 is 4.5, which is then rounded down to 4. The floor division operation always yields integers smaller than or equal to the standard division results.
Why is it called floor division?
The result of floor division is the floor, or the integer part, of what you get from normal division. It is often used in programming to get integer results for algorithms.
Videos
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~.
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.
You can do
double val = 5 / 2;
int answer = Math.floor(val);
OR
int answer = Math.floorDiv(5, 2);
If you were to call System.out.println(answer); the output would be
2
You can easily use Math.floorDiv() method. For example:
int a = 15, b = 2;
System.out.println(Math.floorDiv(a, b));
// Expected output: 7
Like this:
int(5/2) 5//2
Is there ever a case where these are different.
a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int. floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double.
floor returns a double while a / b where both a and b are integers yields an integer value.
With the correct cast the value is the same.
If typeof operator existed in C (it does not) we would have:
(typeof (a /b)) floor(a / b) == a / b
EDIT: Now if the question is: is there any difference between:
(double) (a / b)
and
floor(a / (double) b)
the answer is yes. The results differ with respect to negative values.