6 ms·
I'm reading the "Pony" tweet quoted in TFA and your comment and I'm left very puzzled: is that really that common to want x / 0 == 0 ? In practice where does th
by simias 8y ago
I'm reading the "Pony" tweet quoted in TFA and your comment and I'm left very puzzled: is that really that common to want x / 0 == 0 ? In practice where does that crop up?
You say that you frequently have to write your little shim but honestly I don't remember writing code like that in recent memory.
You talk about progress bars, I suppose it makes sense if you somehow try to copy 0 elements for instance, and you end up dividing by zero when computing the progress, like:
pos_percent = (copied_elems * 100) / total_elems
And both copied_elems and total_elems are zero. But in this case wouldn't you want the computation to return 100% instead of zero?
It's also a bit odd because it introduces a discontinuity: as the divisor goes towards zero the result of the operation gets greater until it reaches exactly zero and it yields 0. Wouldn't it make more sense to return INT_MAX (for integers) or +inf for floats? If you're writing a game for instance it might work better.
I guess it just goes to show that it probably makes a lot of sense to leave it undefined and let people write their own wrapper if necessary to do what works best for them in their given scenario.
- huehehue 8y agoPractically, it's quite common to not immediately know the divisor. In cases where the divisor is initially unknown but takes an imperceptible amount of time to compute it's better to render 0%. Otherwise you might get a flash of a full progress bar (for example) while the divisor is determined. Of course, it's context dependent. As others mention, your code might be full of stuff like X / (divisor || 1).
- octalmage 8y agoI’ve run into this a bit with progress related stuff. I bet if you looked at progress bar libraries they’d have similar logic built in.
- adrianratnapala 8y agoThe nicest UX would be to say something like "waiting..." while the denominator is zero. Presenting a zero to the user instead is probably an acceptable short-cut, but at the end of the day, it's a UX decision. Which is why it should be handled in explicit if-else code, that lives as close to the UI as possible, rather than being buried in the semantics of basic numerical operators.
- user5994461 8y agoX / (divisor || 1). It's invalid code in C, C++ and Java.
- bqe 8y agoThis is valid C and C++.
- cyphar 8y agoIt is, but it will always give you a divisor of 1 (because "true" is 1 -- I also thought it would work until I tested it): % cat >division.c <<EOF #include <stdio.h> int main(void) { printf("1/0 = %d\n", 1 / (0 || 1)); printf("1/2 = %d\n", 1 / (2 || 1)); printf("1.0/0 = %f\n", 1.0 / (0 || 1)); printf("1.0/2 = %f\n", 1.0 / (2 || 1)); } % gcc -Wall -o divison divison.c % ./divison 1/0 = 1 1/2 = 1 1.0/0 = 1.000000 1.0/2 = 1.000000 In Python this is not the case: % python3 >>> 1 / (0 or 1) 1 >>> 1. / (0 or 1) 1.0 >>> 1 / (0 or 1) 1 >>> 1. / (2 or 1) 0.5 % python3 >>> 1 / (0 or 1) 1.0 >>> 1 / (2 or 1) 0.5
- int0x80 8y agoWhat do you expect for that C code?
- cyphar 8y agoIn the context of this discussion, the idea was that it would act the same way as Python. But it obviously doesn't.
- user5994461 8y agoIt shouldn't be valid in C++. booleans cannot participate in arithmetic operations. You will get a warning from the compiler if you are lucky. C doesn't have booleans and treat them as integers 0 or 1, it can do the math and will always return 1.
- Too 8y agoIf you dont know the number of elements, shouldnt that be tracked in another variable, instead of reusing the total elements variable and assuming 0 elements means unknown? Also, if loading total elements takes a significant amount of time, shouldnt this loading also be reflected in the progess bar?
- nostrademons 8y agoReal example: I'm collecting some quality signals from a corpus, most of which are some form of ratio, average, weighted average, or scaled average of counting various quantities within the documents. If the elements being counted are missing, I want the term involving that quality signal to disappear from the final ranking calculation. Defining x / 0 = 0 gets this behavior for free, while leaving zero as an exception means it has to be caught for every single signal calculation, which is a pain when there are potentially dozens of different signals, all of which are counting different things (and have different divisors). I've actually defined a helper function to do this automatically, which also lets me change the default "null object" easily if I choose a different representation or want to apply some baseline value.
- goerz 8y agoDefining a function would seem like exactly the right thing to do! There's no problem.
- nostrademons 8y agoModule-scoped operator overloading would be really, really nice in this situation, though.
- vmchale 8y agoNo, it probably wouldn't, unless you were using integers. Pony still uses floats and returns NaN or Infinity or something.
- ummonk 8y ago>It's also a bit odd because it introduces a discontinuity: as the divisor goes towards zero the result of the operation gets greater until it reaches exactly zero and it yields 0. Wouldn't it make more sense to return INT_MAX (for integers) or +inf for floats? Unless you're using unsigned ints, the discontuity will exist no matter what you do, because of negative divisors.