|
Signed (integer) division. Where your faith in the sanity of arithmetic first starts to crumble.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Not at all; it's only when integer division is being simulated with floating-point division that things go wrong. But it's floating-point division's fault.
|
|
|
|
|
|
PIEBALD boots up his MicroVAX and enters a darkened chamber...
VAX BASIC V3.9-000
Ready
PRINT MOD(-1,20)
-1
Ready
For added points, Turbo BASIC (sorry, the color didn't come over):
┌──────────────────────────────── Turbo Basic ─────────────────────────────────┐
│ File Edit Run Compile Options Setup Window Debug │
└──────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────── Edit ────────────────────────────────┐┌ Trace ┐
│ E:NONAME.BAS Line 1 Col 19 Insert Indent Tab ││ │
│10 PRINT -1 MOD 20 ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
└─────────────────────────────────────────────────────────────────────┘│ │
┌────────────── Message ───────────────┐╔════════════ Run ════════════╗│ │
│ Compiling: NONAME │║-1 ║│ │
│ Time: 00:00 │║ ║│ │
│ Line: 1 Stmt: 3 Free: 370k │║ ║│ │
│ │║ ║│ │
│ │║ ║│ │
└──────────────────────────────────────┘╚═════════════════════════════╝└───────┘
Alt-F5-Zoom Alt-F6-Next
|
|
|
|
|
PIEBALDconsult wrote: (sorry, the color didn't come over)
You could have posted a link to the screenshot, or code the color in HTML yourself
Jokes apart
Turbo Basic is not unknown for me. But VAX? I never saw it
Edit:
Quote: VAX BASIC User Manual. Order Number: AA-HY158-TE. February 1990. This manual describes how to develop VAX BASIC programs, describes the features of Ok... I think I already know why...
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Unlike the newer DEC/COMPAQ/HP BASIC that I have on my AlphaServers and Itanium, VAX BASIC still has immediate mode.
|
|
|
|
|
Lot's of work went into that
I am not the one who knocks. I never knock.
In fact, I hate knocking.
|
|
|
|
|
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
|
|
|
|
|
Fantastic!!
I'm actually literally LOLing.
|
|
|
|
|
Bewdy, had hoped I wouldn't be the only one prepared to admit it.
|
|
|
|
|
|
|
It is hard to say 'wrong' or 'right'...
The Euclidean way is much more perfect (consistent) and fits better math as science, however Donald Knuth promoted an other way (called floored division), which found to be better for computer science and used by most languages today...
Modulo operation - Wikipedia[^] - there is a nice table on the left side about programming languages...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: It is hard to say 'wrong' or 'right'
Yes, I agree... except of course the one that returns -1 is right.
Just kidding. I really do agree.
Kornfeld Eliyahu Peter wrote: Donald Knuth promoted an other way (called floored division), which found to be better for computer science and used by most languages today.
Very interesting and I agree that this version is better for computer science.
|
|
|
|
|
raddevus wrote: Python and Ruby are garbage languages and I'm sure this will be their end.
I would also include some others like Javascript, but at least it knows its math and so it lives on (for now).
-1%20 = 19 is so counter intuitive, I don't know how they reached that conclusion, however, we can see here that in Python the result of a modulo(%) operation has to be of the same sign and strictly smaller than the second operand.
Edit:
And so python says
print 1%-20; = -19.
print -1%-20; = -1. (finally)
Someone needs to go back to the math class they hated.
I am not the one who knocks. I never knock.
In fact, I hate knocking.
modified 30-Aug-17 4:50am.
|
|
|
|
|
GKP1992 wrote: -1%20 = 19 is so counter intuitive,
I agree. It is interesting that a small number of languages use this different way which is maybe more loyal to math.
|
|
|
|
|
Well done
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
|
|
|
|
|
|
Same discussion here, see answer 2:How to implement a circular counter?[^]
For me the result of c# (-1) is also more intuitive if I look e.g. x % 2 around Zero...
1 % 2 = 0 * 2 rem: 1
0 % 2 = 0 * 2 rem: 0
-1% 2 = 0 * 2 rem: -1
... which for me does Show a Kind of symetrie. I agree, a very unscientific Argumentation
Bruno
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Someone pointed out this in the link you posted:
Quote: Please note that C# and C++'s % operator is actually NOT a modulo, it's remainder.
Could it be that this is true? In that case making assumptions can have unexpected consequences. I'm going to look what the MSDN has to say about that...
There you go:
Quote: % Operator (C# Reference)
The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.
The user can't update the up: we update it for them (Choice in the CP poll)
|
|
|
|
|
To make it even worse, it's not even the same in all languages, and to make that even worse there are languages that didn't even make a choice which way to do it (you have already guessed which languages that were).
There's a nice list at the right side of the relevant wikipedia page, which is interestingly the Modulo operation page while the point is that in most languages it's really the remainder operation.
This is all bullshit and programming sucks.
|
|
|
|
|
|
|
|
I don't think this is really wrong.
This is probably due to confusion between modulo and remainder.
There seems to be the same for positive numbers.
But for negative numbers, there are different.
Remainder work as expected.
But modulo work as following: returns the difference of the first number, and the biggest integer (possibly negative) multiple of the second number that is less than the first number.
So in our example: -1 - -20 = 19
That's why lot of programming languages have 2 "modulo" operators, one for modulo operations and one for remainder operations.
|
|
|
|