News for hackers. Stuff that matters.
Re:1/0 = 0 by huehehue on Friday August 10, 2018 @06:40PM (#17737210)
Practically, 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).

Re:1/0 = 0 by adrianratnapala on Friday August 10, 2018 @07:11PM (#17737349)
The 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.
Re:1/0 = 0 by Too on Saturday August 11, 2018 @01:14AM (#17738467)
If 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?

Re:1/0 = 0 by octalmage on Friday August 10, 2018 @06:57PM (#17737296)
I’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.
Re:1/0 = 0 by user5994461 on Friday August 10, 2018 @07:45PM (#17737481)

    X / (divisor || 1).
It's invalid code in C, C++ and Java.
Re:1/0 = 0 by l_t on Friday August 10, 2018 @07:54PM (#17737514)
Boolean operators returning values is common, especially in dynamically typed languages. The code is valid in JavaScript, for example, and in Python (except it uses "or" instead of "||").
Re:1/0 = 0 by user5994461 on Friday August 10, 2018 @08:24PM (#17737607)
In python, it should probably be corrected with // or 1.0 to make it either an integer division or a float division.
Re:1/0 = 0 by l_t on Friday August 10, 2018 @08:45PM (#17737667)
Ah, that's true. Good point.
Re:1/0 = 0 by bqe on Friday August 10, 2018 @09:43PM (#17737841)
This is valid C and C++.
Re:1/0 = 0 by cyphar on Friday August 10, 2018 @09:48PM (#17737855)
It 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
Re:1/0 = 0 by comex on Saturday August 11, 2018 @06:12AM (#17739111)
Some trivia: GNU extensions to C/C++ include a "?:" operator that does what you'd want in this case, e.g.

    x / (divisor ?: 1)
Re:1/0 = 0 by user5994461 on Saturday August 11, 2018 @06:09AM (#17739103)
It 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.

Re:1/0 = 0 by comex on Saturday August 11, 2018 @06:16AM (#17739117)
That's not true. C++ does define an implicit conversion from bool to int:

https://en.cppreference.com/w/cpp/language/implicit_conversi... (under "Integral promotion")

And C has a boolean type as of C99.

Re:1/0 = 0 by int0x80 on Friday August 10, 2018 @11:05PM (#17738049)
What do you expect for that C code?
Re:1/0 = 0 by cyphar on Saturday August 11, 2018 @11:41AM (#17740103)
In the context of this discussion, the idea was that it would act the same way as Python. But it obviously doesn't.