News for hackers. Stuff that matters.
Re:Python, unlike C, has the mod operator always return a po... by Lt_Riza_Hawkeye on Wednesday December 29, 2021 @04:21PM (#29730187)
My understanding from college was that C doesn't have a modulo operator, if you look at the spec (according to my professor (according to my memory)) it's called the "remainder operator". Modulo and remainder are not the same operation, so both python and C are correct
Re:Python, unlike C, has the mod operator always return a po... by masklinn on Wednesday December 29, 2021 @04:36PM (#29730343)
FWIW there's an entire paper on the subject of these operations: https://core.ac.uk/download/pdf/187613369.pdf

(well a report in TOPLAS really, but that's pretty close)

There are technically 5 different versions of this operation, though only 2 (possibly 3) are in common use. Common Lisp seems to implement all of them except, somewhat oddly, the euclidean version (the "actual modulo"), its mod function is instead a flooring division.

edit: it looks like Python also follows flooring, not euclidean, meaning the quotient is rounded downwards, and per "a = nq + r" the remainder has the sign of the divisor:

    >>> 5 % -2
    -1
    >>> divmod(5, -2)
    (-3, -1)
The C version is the "truncating" division, meaning the quotient is truncated (rounded towards 0).
Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Wednesday December 29, 2021 @05:35PM (#29731001)
Somewhere out there in the universe, there's an nightmare that starts with "Well, zero is kind of an unusual number, so for safety sake we've defined our division to always round away from zero. This resulted in an interesting mod operation..."
Re:Python, unlike C, has the mod operator always return a po... by Supermancho on Wednesday December 29, 2021 @06:20PM (#29731430)
Ponylang.io - There is a similar operation (toward zero)

https://stdlib.ponylang.io/builtin-Integer/#div_unsafe

and, in Ponylang, i/0 usually results in 0.

Re:Python, unlike C, has the mod operator always return a po... by staticassertion on Wednesday December 29, 2021 @06:33PM (#29731530)
I think this is a bit different. Pony's div is total, and in order to be total div by 0 is 0, which is not a particularly absurd idea.

That said, I do feel that Pony should have added a 'panic' concept and then forced actors to handle panics, but that's me.

Re:Python, unlike C, has the mod operator always return a po... by kibwen on Wednesday December 29, 2021 @11:44PM (#29733698)
> which is not a particularly absurd idea

It's not completely untenable, but it's still kinda janky. I've never heard of any mathematical argument that justifies division by zero yielding 0; contrast 0^0, which at least has an argument for yielding 1: http://mathscitech.org/articles/zero-to-zero-power

Re:Python, unlike C, has the mod operator always return a po... by noswi on Thursday December 30, 2021 @06:31AM (#29735866)
From programming perspective, there's nothing sufficiently preposterous in the idea of dividing by zero that would require bringing the whole world to a low-level halt just for attempting that by default. You tried to calculate the average value of what turned out to be an empty list? Go suffer the most severe exception, like the ones reserved for those reading from nonexistant memory.
Re:Python, unlike C, has the mod operator always return a po... by Asraelite on Thursday December 30, 2021 @07:50AM (#29736287)
Another common case is adjusting some value according to a spatial vector's magnitude, like normalizing a vector or scaling gravitational attraction according to the distance between two points.

In the majority of these cases the most reasonable answer is 0.

Re:Python, unlike C, has the mod operator always return a po... by duskwuff on Thursday December 30, 2021 @04:31AM (#29735134)
> I've never heard of any mathematical argument that justifies division by zero yielding 0

0/x = 0 for all x except zero; defining 0/0=0 removes that exception.

Re:Python, unlike C, has the mod operator always return a po... by AlbinNP on Thursday December 30, 2021 @05:27AM (#29735489)
x/x = 1 for all x except 0; defining 0/0 = 1 removes that exception.
Re:Python, unlike C, has the mod operator always return a po... by hwayne on Thursday December 30, 2021 @11:42AM (#29738206)
Split the difference, 0/0 = 0.5
Re:Python, unlike C, has the mod operator always return a po... by ben_w on Monday January 03, 2022 @04:38AM (#29778956)
It gets worse: differentiation would be 0/0 if you went straight to the limit rather than just approaching it, so 0/0 can be any differentiable function, not just a value.

(Caveat: I’m probably being slightly imprecise with my language here)

Re:Python, unlike C, has the mod operator always return a po... by staticassertion on Thursday December 30, 2021 @10:21PM (#29744511)
I think it's worth all getting on the same page on the fact that math is fake and stupid. If it's more useful for 0/0 to == 0, that's justification enough imo.
Re:Python, unlike C, has the mod operator always return a po... by remexre on Thursday December 30, 2021 @02:07AM (#29734429)
Re:Python, unlike C, has the mod operator always return a po... by dataangel on Thursday December 30, 2021 @11:24AM (#29738019)
Maybe the best argument is that the average of zero items should be zero?
Re:Python, unlike C, has the mod operator always return a po... by housecarpenter on Thursday December 30, 2021 @03:52PM (#29741294)
But then why should the average of zero items be zero? It still seems like just an arbitrary choice to me.
Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Wednesday December 29, 2021 @08:28PM (#29732437)
Toward zero is a fairly intuitive way to do it, I think. Intuitively, if a you were, say, discretizing a length (L) with a number of steps (n) of some size (d) and went for d=L/n, you might want to always end up with d*n <= L even if L is negative, right? Although, having an integer for d may not make sense, or you might want to take |L| beforehand anyway...
Re:Python, unlike C, has the mod operator always return a po... by StefanKarpinski on Wednesday December 29, 2021 @05:26PM (#29730895)
Good paper—it was the primary source we looked at when implementing these operations in Julia. Specifically, Julia implements all versions and overloads `div` and `rem` to take a rounding mode argument. It also gives special names to a few of them and they come in pairs: `div` & `rem`, `fld` & `mod` (`fld` stands for “floored divide” as opposed to `div` which rounds towards zero). The `%` operator does remainder like C, which was a tough call, but we figured being compatible with C was worthwhile for people porting code. It’s also less useful in Julia due to default 1-based indexing of arrays; for that there is a `mod1` function that returns a value between 1 and n.
Re:Python, unlike C, has the mod operator always return a po... by thaumasiotes on Wednesday December 29, 2021 @07:07PM (#29731802)
> The `%` operator does remainder like C, which was a tough call, but we figured being compatible with C was worthwhile for people porting code. It’s also less useful in Julia due to default 1-based indexing of arrays; for that there is a `mod1` function that returns a value between 1 and n.

After thinking about this, what I really want is a way to access an array with an unadjusted number and tell the array to do whatever modulizing is necessary. e.g. instead of

    a[small]
    a[mod1(big)]
I'd like to have something more like

    a{small}
    a[big]
Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Thursday December 30, 2021 @03:26AM (#29734834)
Hmm. This means we'd have to do mod every time we accessed an array which seems not great, right? But probably it could be optimized away for the common/easy cases.
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Thursday December 30, 2021 @05:47AM (#29735598)
In WUFFS if you've got a 40 item array of things, an unsigned 8-bit integer n and you want to access things[n] the compiler will conclude n's type must be base.u8[0..40]

Now, having refined the type this way, when you try elsewhere in the code to read some unknown byte into n, WUFFS will reject that because who knows if it exceeds 40? To make the code compile, you need to actually write code that ensures n is always less than 40.

Of course you aren't going to write big general purpose software under this sort of constraint, it's like having a two year old following you everywhere insisting on a detailed explanation of why you are doing every single thing and asking the most irritating "But why?" questions after each explanation, exhausting. But if you're processing untrusted data it's exactly what you needed to stay safe.

Re:Python, unlike C, has the mod operator always return a po... by dmurray on Thursday December 30, 2021 @04:45AM (#29735218)
Julia already has bounds checking at runtime on array access, so you could do the mod operation only if the bounds check fails.

But I don't get how this would practically work. Would each array have a preferred n that is used for the mod operation? For most situations where that makes sense, using a 2d array seems more logical.

Re:Python, unlike C, has the mod operator always return a po... by half-kh-hacker on Thursday December 30, 2021 @05:16AM (#29735405)
I assume n would be size(a)
Re:Python, unlike C, has the mod operator always return a po... by dmurray on Thursday December 30, 2021 @05:28AM (#29735492)
Oh yeah - I got mixed up on this and thought we wanted to access sub arrays within a.
Re:Python, unlike C, has the mod operator always return a po... by half-kh-hacker on Thursday December 30, 2021 @05:15AM (#29735398)
The parent comment has brace-syntax as well as the square brackets.

I assume the braces are there for "I want to skip doing the wrapping"

Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Thursday December 30, 2021 @09:44AM (#29737064)
I was thinking that, but they didn't mention it, so I assumed it was just a typo. Could be, though.
Re:Python, unlike C, has the mod operator always return a po... by saurik on Wednesday December 29, 2021 @05:00PM (#29730631)
I kind of get what you are saying syntactically, but the semantics are weird to me as the C version is only defined for integers in the first place so saying that the operator is truncating something is an awkward mental model.
Re:Python, unlike C, has the mod operator always return a po... by masklinn on Wednesday December 29, 2021 @05:09PM (#29730716)
> I kind of get what you are saying syntactically, but the semantics are weird to me as the C version is only defined for integers

Why? Mathematically, dividing two integers yields a real. If you want the result to be an integer, you need to define some rounding operation on the real result to produce an integer.

That is the source of the divergence between C and Python here, C rounds by truncation (towards 0), Python rounds by flooring (towards -inf).

Re:Python, unlike C, has the mod operator always return a po... by desio on Wednesday December 29, 2021 @05:29PM (#29730928)
> Mathematically, dividing two integers yields a real

not true. if you define 'divide' as inverse multiplication over the reals then that is closer to being true (zero is an integer, dividing an integer by zero doesn't yield a real). in that case every real can be expressed as n = a/b where a and b are integers and b != 0 then yes n is a real.

But 'divide' could just as well be defined along the lines of divisibility in number theory (the study of integers) where every integer can be expressed as n = q*b + r, where n,q,b,r are all integers and r >= 0 > b. then you could say that n divide b = q, with r as remainder, and n mod b = r. These are my preferred definition of mod and integer division.

Re:Python, unlike C, has the mod operator always return a po... by desio on Wednesday December 29, 2021 @05:30PM (#29730946)
sorry, rational, not real.
Re:Python, unlike C, has the mod operator always return a po... by jounker on Thursday December 30, 2021 @02:13AM (#29734467)
Depends on your definition of division. It you choose a definition which is closed (e.g. you’re working in number theory) then division of integers can only produce other integers.
Re:Python, unlike C, has the mod operator always return a po... by dragonwriter on Wednesday December 29, 2021 @05:22PM (#29730854)
> Mathematically, dividing two integers yields a real

Rational, more specifically.

Re:Python, unlike C, has the mod operator always return a po... by saurik on Wednesday December 29, 2021 @05:12PM (#29730758)
Oh, I'm dumb. Sorry. (I got the terms for quotient, dividend, and divisor mixed up.)
Re:Python, unlike C, has the mod operator always return a po... by masklinn on Wednesday December 29, 2021 @05:18PM (#29730827)
> Oh, I'm dumb.

We both know that's not true.

> Sorry. (I got the terms for quotient, dividend, and divisor mixed up.)

No big, brainfarts happen.

Re:Python, unlike C, has the mod operator always return a po... by DonHopkins on Wednesday December 29, 2021 @08:25PM (#29732422)
Just don't divide by zero. That's how you get black holes!
Re:Python, unlike C, has the mod operator always return a po... by LudwigNagasena on Wednesday December 29, 2021 @06:09PM (#29731327)
Mathematically, Euclidean division of two integers yields an integer. You don't need to define reals or rounding. Children learn how to do it before they learn about rational numbers.
Re:Python, unlike C, has the mod operator always return a po... by dotancohen on Thursday December 30, 2021 @12:58AM (#29734094)
I invite you to ask your child how he would share three cookies with you. I've been asking variations of that question to all three of my children since they could talk.

Of interesting mathematical consequence, each of my children converged on the same solution at different ages. Them: three cookies. Daddy: zero cookies.

Re:Python, unlike C, has the mod operator always return a po... by glandium on Thursday December 30, 2021 @01:59AM (#29734391)
If it's not truncating, how do you describe its behavior when e.g. an odd number is divided by 2?
Re:Python, unlike C, has the mod operator always return a po... by saurik on Thursday December 30, 2021 @08:43AM (#29736617)
(Read the rest of the thread and look for my username.)
Re:Python, unlike C, has the mod operator always return a po... by aix1 on Wednesday December 29, 2021 @04:49PM (#29730508)
Re:Python, unlike C, has the mod operator always return a po... by cormacrelf on Wednesday December 29, 2021 @09:37PM (#29732890)
This one is very concise, great as a reference, you can remind yourself of which negatives go where in an instant.
Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @04:41PM (#29730414)
K&R "The C Programming Language" (1978), p.37:

> The binary arithmetic operators are +, -, *, /, and the modulus operator %.

https://archive.org/details/TheCProgrammingLanguageFirstEdit...

At least, that was the C reference I used in college.

Re:Python, unlike C, has the mod operator always return a po... by klodolph on Wednesday December 29, 2021 @04:53PM (#29730560)
You'll only find that terminology in K&R. The C specification only uses "modulo" to refer to the behavior of unsigned numbers, and uses "remainder" to refer to the behavior of %. This is true of at least several versions of the C standard dating back to 1999, I don't have the C90 spec handy.
Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @05:23PM (#29730865)
Indeed!

The draft version "X3.???-1988" at https://web.archive.org/web/20161223125339/http://flash-gord... (linked-to from Wikipedia) has:

> % modulus operator: 3.3.5

which seems off to a good start for K&R, but then also has:

> remainder operator, %, 3.3.5

and 3.3.5 says "the result of the % operator is the remainder" -- not a whiff of a modulus present there.

Thanks!

Re:Python, unlike C, has the mod operator always return a po... by somerando7 on Wednesday December 29, 2021 @05:11PM (#29730743)
People generally watch tutorials, read programming books, blogs, stackoverflow to learn programming, not the standard of a programming language.

You will find "modulus operator" to refer to % in C in all of the above (other than the standard).

Re:Python, unlike C, has the mod operator always return a po... by klodolph on Wednesday December 29, 2021 @05:35PM (#29731005)
That is what most people do... which is why those of us who write tutorials, write programming books, write blog posts, and write Stack Overflow answers have a responsibility to go to the source.

The more something gets repeated from person to person, the higher chance that somewhere in that chain, somebody screwed up. That's why people like professors, people who run YouTube programming channels, people who answer C questions on Stack Overflow, etc. should generally strive to cut down the number of links in the chain.

Re:Python, unlike C, has the mod operator always return a po... by xboxnolifes on Wednesday December 29, 2021 @05:36PM (#29731013)
The fact that tutorials teach incorrect knowledge does not make the knowledge correct.
Re:Python, unlike C, has the mod operator always return a po... by charcircuit on Wednesday December 29, 2021 @05:23PM (#29730864)
I think there is the same problem with the conditional operator "?" which a ton of resources name as the ternary operator.

edit: tertiary -> ternary

Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @05:26PM (#29730898)
I know it as "the ternary operator" (used that way in K&R at https://archive.org/details/TheCProgrammingLanguageFirstEdit...), and until about 2 minutes ago had never heard it as "the conditional operator" - even though that's how the spec describes it!
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Wednesday December 29, 2021 @07:03PM (#29731779)
It gets named "the ternary operator" in many languages like C because they only have one ternary operator, it's always that one. In some (I think?) languages there are several ternary operators, and so for them it'd be like if we called C's XOR operator ^ "the binary operator".
Re:Python, unlike C, has the mod operator always return a po... by jholman on Thursday December 30, 2021 @06:39AM (#29735909)
Yeah. It's "the ternary operator" the way John Carmack is "the author of the tweet we're discussing". It's a correct description, but it's not his name.
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Thursday December 30, 2021 @08:00AM (#29736351)
Names are just whatever you call something. For inanimate objects the objects can't care what you call them, so at the very most you're privileging one person's opinion over another if you choose to call it the same thing. For ideas even more so. Ordinarily the most important purpose of the name is to ensure you're talking about the same thing.

Calling it "Ayers Rock" privileges some guy from the 19th century, calling it "Uluru" privileges a bunch of people who lived near it for longer than that, the rock itself has no opinion (and also doesn't believe anything about ritual magic, property law, gender based taboos, or the tourist industry)

Now, John Carmack is a little different because John is a person and it is rude to use names people don't like. But it isn't impossible, it's just rude. Calling the previous President of the United States of America "Tangerine Palpatine" works just fine - we both know who I meant. In fact it's a little weird that our culture has parents assign to their children full legal names, only kinda-sorta making it possible for them to choose for themselves later, the Culture's habit of allowing children to pick one of their names as part of growing up seems better.

Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Wednesday December 29, 2021 @04:47PM (#29730477)
This is also exactly what the ANSI ("Second edition") K&R book I have in front of me says.

It goes on to say: "x % y produces the remainder when x is divided by y" and that "the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow".

Re:Python, unlike C, has the mod operator always return a po... by fanf2 on Wednesday December 29, 2021 @08:08PM (#29732276)
The language in the reference manual at the end of the book (p. 188) is much more specific. It says:

The binary operator / indicates division. When positive integers are divided, truncation is toward 0, but the form of truncation is machine-dependent if either operand is negative. On all machines covered by this manual, the remainder has the same sign as the dividend. It is always true that (a/b)*b + a%b is equal to a (if b is not 0). The binary % operator yields the remainder from division of the first operand by the second.

Re:Python, unlike C, has the mod operator always return a po... by jounker on Thursday December 30, 2021 @02:20AM (#29734497)
Interesting. If the equality must hold, then this means that a/b has different values on different architectures depending on whether a%b is positive or negative.
Re:Python, unlike C, has the mod operator always return a po... by kazinator on Wednesday December 29, 2021 @05:29PM (#29730934)
C only pinned this down in C99. In C90, the sign of the remainder was implementation-defined in those cases in question.
Re:Python, unlike C, has the mod operator always return a po... by LAC-Tech on Wednesday December 29, 2021 @06:12PM (#29731357)
So only 22 years ago :)

Are C programmers still using C89 and C90?

Re:Python, unlike C, has the mod operator always return a po... by opencl on Wednesday December 29, 2021 @06:40PM (#29731583)
There are a lot of C compilers out there, and many of them have not implemented all of C99. Microsoft started to add C99 features in MSVC2013 and still doesn't support the whole spec.

The C compilers supplied by microcontroller vendors in particular are often, to put it charitably, not great at conforming with the latest standards.

Re:Python, unlike C, has the mod operator always return a po... by jlarocco on Wednesday December 29, 2021 @08:53PM (#29732596)
IIRC, Microsoft has publicly stated that they only plan to implement the parts of C99 required by the C++ standards.

EDIT: Fact checking myself, I found [1], so apparently they've decided to start adding C11 and C17. C99 support isn't entirely clear, though.

[1] https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...

Re:Python, unlike C, has the mod operator always return a po... by jcranmer on Thursday December 30, 2021 @12:03AM (#29733811)
C99 added some (mandatory) features that had some issues with implementation, most notably variable-length arrays; C11 made those features optional instead. MSVC is unlikely to ever support VLAs (as noted in the blog post you linked).

A parallel can be drawn to `export template` in C++98, which is a feature that was only ever implemented by a single compiler (whose engineers, when asked for recommendations to others attempting to implement, offered "don't"). As a result, it was dropped from later versions of the standard, and so almost every compiler doesn't actually support C++98 100%, but no one cares because the things that it doesn't implement doesn't matter. VLAs aren't quite as universally maligned as `export template`, but given that it's dropped to optional in later versions, it's not totally unreasonable to claim implementation of all of C99 that matters while not supporting VLAs.

Re:Python, unlike C, has the mod operator always return a po... by pjmlp on Thursday December 30, 2021 @02:32AM (#29734550)
VLAs are considered a huge mistake in security coding, to the point that Google sponsored the work to clean the Linux kernel of them.
Re:Python, unlike C, has the mod operator always return a po... by kazinator on Thursday December 30, 2021 @10:56AM (#29737738)
How recent is the -Wwla warning in GCC? Because it seems that would easily find them for you, and -Werror=vla prevent them from creeping in.

Answering my own question: by doing a binary search on the online GCC documentation, I discovered that it's not documented up to 4.2.4. The next version up for which online documentation is hosted is 4.3.6, and that has -Wvla.

Re:Python, unlike C, has the mod operator always return a po... by pjmlp on Thursday December 30, 2021 @12:21PM (#29738668)
Assuming all source code is available, the code is never migrated to other compilers, and every company in the world would actually bother to use such warnings when using GCC.

Naturally removing them from ISO C was the saner option.

Re:Python, unlike C, has the mod operator always return a po... by kazinator on Thursday December 30, 2021 @01:54PM (#29739972)
But they are forever in C99, which GCC supports.

VLA's crept in to the kernel, which is what this thread is about.

With -Wvla, you can learn about where all the VLA's are being declared in the code base.

Also, don't forget that VLA's are a GCC extension which existed before C99. They are available in the -stdc=gnu89 mode, and -ansi (without -pedantic).

They are available in -ansi mode (diagnosed if you use -pedantic).

They were not "removed" from ISO C; they are optional. If you implement VLA's, there is an ISO-C-conforming way to do that.

  $ cat vla.c
  int fun(int x, int a[x]) { return 0; }
  int main(void) { return 0; }
  $ gcc -ansi vla.c
  $ gcc -std=gnu89 vla.c
  $ gcc -ansi -pedantic vla.c
  vla.c:1:1: warning: ISO C90 forbids variable length array ‘a’ [-Wvla]
  int fun(int x, int a[x]) { return 0; }
  ^~~
  $ gcc -std=gnu89 -pedantic vla.c
  vla.c:1:1: warning: ISO C90 forbids variable length array ‘a’ [-Wvla]
  int fun(int x, int a[x]) { return 0; }
   ^~~
This warning is now linked to [-Wvla] rather than just [-pedantic], so you can have -pedantic on, yet allow VLA's without a diagnostic.

  $ gcc -std=gnu89 -pedantic -Wno-vla vla.c
  $ gcc -ansi -pedantic -Wno-vla vla.c
Likewise you need -Wvla to police your code against VLA's creeping in, even if you're compiling in -ansi/-std=c90 mode. Traditionally, -pedantic will do it, but it's too strong; it rejects other things you might want. For instance, in the gnu89 dialect, you can initialize local structs and arrays with non-load-time computable expressions, which is complained about with -pedantic.

Dynamic allocation from the stack is something that is quite essential in some situations, and a more efficient approach compared to other alternatives in some other situations.

alloca never goes away, and VLA's are better than alloca in many ways.

Re:Python, unlike C, has the mod operator always return a po... by pjmlp on Thursday December 30, 2021 @03:23PM (#29741007)
Apparently Google's security team thought otherwise.

You can discuss your point of view with the folks that cleaned the kernel.

https://lore.kernel.org/all/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK...

Notice that they are fully aware of that warning.

Re:Python, unlike C, has the mod operator always return a po... by kazinator on Thursday December 30, 2021 @04:15PM (#29741519)
Right. As I dug up, the warning was introduced sometime in 4.3, which could be as far back as 2011. It looks like it was helpful in hunting down the VLA's.

I don't know what point of view you're talking about; what the are discussing in the mailing list are silly uses of VLA that can be replaced by small arrays of fixed length sized to the worst case. These perform better, too.

This is not always practical in every situation.

C itself dynamically allocates from the stack. When a function is called, the increment in stack usage depends on which function. (If recursion is going on, it may not be easy to know the worst-case stack size, even if every function has a static frame size.)

Anyway, similarly, suppose we have used C to write a virtual machine. Suppose the virtual machine allocates function locals and temporaries on the native stack. When a VM function call is processed, the VM (a C program) has to allocate a frame for that. a VLA or alloca can provide that easily, with minimal waste.

Re:Python, unlike C, has the mod operator always return a po... by pjmlp on Thursday December 30, 2021 @04:51PM (#29741898)
As Linus puts it,

> Anyway, some of these are definitely easy to just fix, and using VLA's is actively bad not just for security worries, but simply because VLA's are a really horribly bad idea in general in the kernel.

Re:Python, unlike C, has the mod operator always return a po... by kazinator on Friday December 31, 2021 @12:13PM (#29750036)
Deep recursion is a bad idea in the kernel too; that doesn't mean it's a bad idea.

Code running in the kernel has a limited stack space. I think it's tiny, something like just a few pages.

Interrupts (which can nest) run in that stack space, not just the mainline code in syscall context. I think that might have changed though (there are interrupt specific stacks), which might also depend on the specific arch port of Linux. Still, those stacks are small and all the caveats apply to interrupt context. Don't be searching a linked list with linear recursion or using crazy VLA's.

If I had to put virtual machine execution into the kernel, though, I would definitely use VLA's or alloca to get the space required for each function to be as tight as possible to its actual requirements, the same way that the C compiler emits code for each function to allocate its frame size to exactly the needed size. Right tool for the right job.

Re:Python, unlike C, has the mod operator always return a po... by rascul on Thursday December 30, 2021 @08:05AM (#29736381)
HN discussion about it from 2018 (100 comments):

https://news.ycombinator.com/item?id=18323938

Re:Python, unlike C, has the mod operator always return a po... by __s on Wednesday December 29, 2021 @07:01PM (#29731756)
Re:Python, unlike C, has the mod operator always return a po... by LAC-Tech on Wednesday December 29, 2021 @08:47PM (#29732566)
There are C99 (and beyond)_ compliant compilers for windows, aren't there?

Why insist on MSVC when it's not one of them?

(Genuine question. I remember python was c89 for similar reasons).

Re:Python, unlike C, has the mod operator always return a po... by NavinF on Thursday December 30, 2021 @03:04AM (#29734712)
It’s only recently (as in the last decade) that a drop-in replacement for MSVC’s cl.exe came along that’s ABI compatible and accepts the same CLI arguments: https://clang.llvm.org/docs/MSVCCompatibility.html

Incidentally, that’s around the same time MSVC started getting updates and became bearable.

Re:Python, unlike C, has the mod operator always return a po... by LAC-Tech on Thursday December 30, 2021 @10:27PM (#29744549)
I still don't get it.

Why is a drop in replacement for MSVC needed?

There were c99 compilers for windows before clang IIRC correctly. So why weren't they used, MSVC be damned?

Full disclaimer, I've only ever used C in linux systems.

Re:Python, unlike C, has the mod operator always return a po... by NavinF on Friday December 31, 2021 @07:21AM (#29747384)
Oh there were lots of other compilers. Hardly anyone used them because they didn't work with any existing tooling and IIRC most of them didn't even let you to link the system's UI libraries due to ABI issues and unsupported __stuff in headers. Sure you could work without tooling like a caveman and you could print text to stdout using only the libc you brought with you, but most programs need to do a bit more than that.
Re:Python, unlike C, has the mod operator always return a po... by Too on Thursday December 30, 2021 @12:39AM (#29734000)
MSVC for many years ignored C completely, with the argument that C++ was the successor. Now it seems they turned on their heel with C11 and C17 support added last year, https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...
Re:Python, unlike C, has the mod operator always return a po... by pjmlp on Thursday December 30, 2021 @02:34AM (#29734560)
Somehow I get to wonder that WSL and Azure Sphere aren't completely alien to that decision.
Re:Python, unlike C, has the mod operator always return a po... by _3u10 on Wednesday December 29, 2021 @10:16PM (#29733126)
Yes, Linux uses gnu89
Re:Python, unlike C, has the mod operator always return a po... by kazinator on Thursday December 30, 2021 @02:01PM (#29740058)
.. and that allows plenty of silently accepted extensions, including variable-length arrays, which were in GNU C long before C99.

It seems that only GCC 4.3 added the -Wvla option to unbundle the diagnosis of the presence of VLA's from -pedantic.

Now you can write in gnu89, while banishing VLA's from your code base, if you're so inclined.

Re:Python, unlike C, has the mod operator always return a po... by thaumasiotes on Wednesday December 29, 2021 @06:51PM (#29731664)
> Modulo and remainder are not the same operation, so both python and C are correct

That just makes C look worse. Remainders are always nonnegative, as specified in the Division Algorithm [note that the Division Algorithm is the name of a theorem, not an algorithm].

https://en.wikipedia.org/wiki/Euclidean_division

Also, I'm not aware of a definition of "modulo" other than "remainder". Where are you getting the idea that they're different?

For example, https://en.wikipedia.org/wiki/Modulo_operation explicitly defines "modulo" as "remainder".

Re:Python, unlike C, has the mod operator always return a po... by ollien on Wednesday December 29, 2021 @05:22PM (#29730859)
Yep. I remember that for my first C assignment in college we had to write a calendar, and leap year calculations needed modulos. I remember distinctly that I tore my hair out trying to figure this out, especially because I was prototyping my math in Python. I eventually resolved that I needed to constantly ensure my sum was > 0 (by adding my modulo base).
Re:Python, unlike C, has the mod operator always return a po... by Normal_gaussian on Wednesday December 29, 2021 @05:37PM (#29731024)
That will only work for cases where a >= -b. For the more general case:

> a mod b = ((a % b) + b) % b

The first remainder bounds it to -b < x < b. Then adding b goes to 0 < x < 2b. Then the second remainder gets th desired 0 <= x < b.

This will not work for b > max_int / 2 (solution in that case is much mkre language dependent)

Re:Python, unlike C, has the mod operator always return a po... by yaur on Thursday December 30, 2021 @05:29AM (#29735505)
What's the case where the C % operator behaves in an unexpected way? I've been wiring software for about 20 years and can count zero hands the number of times someone I've worked with has had to worry about how the modulo operator behaves with negative numbers.
Re:Python, unlike C, has the mod operator always return a po... by housecarpenter on Thursday December 30, 2021 @06:09AM (#29735722)
An example would be if you want to find the month corresponding to a timestamp. The obvious thing to do is to scale the timestamp so that it gives you the number of months after the epoch, take that number % 12, and map 0 to January, 1 to February, etc. (assuming the epoch is in January). But if you try to do that with a timestamp from before the epoch, you'll end up with 0 for January, -11 for February, -10 for March, etc.
Re:Python, unlike C, has the mod operator always return a po... by yaur on Friday December 31, 2021 @02:00AM (#29745702)
No. If your software has to deal with dates from before 1970 "seconds since the epoch" is not a reasonable choice for a time unit.