WASasquatch / was-node-suite-comfyui

An extensive node suite for ComfyUI with over 210 new nodes
MIT License
1.22k stars 177 forks source link

Random Number of type bool generates a float between 0.0 and 1.0 instead of True/False or 1/0 #239

Open TimmermanV opened 1 year ago

TimmermanV commented 1 year ago

A Random Number node configured to generate a bool will output a floating point number between 0.0 and 1.0 instead. The int output is always 0 in this case.

WASasquatch commented 1 year ago

That's not really a Boolean, though.

On Thu, Oct 19, 2023, 2:52 AM Tim Verweij @.***> wrote:

A Random Number node configured to generate a bool will output a floating point number between 0.0 and 1.0 instead. The int output is always 0 in this case.

— Reply to this email directly, view it on GitHub https://github.com/WASasquatch/was-node-suite-comfyui/issues/239, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIZEZLIHXMJX53UQR6SRZ3YADZ5BAVCNFSM6AAAAAA6G45CPWVHI2DSMVQWIX3LMV43ASLTON2WKOZRHE2TCNZWGM2DGOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

TimmermanV commented 1 year ago

I don't understand. Could you elaborate? Is the 'bool' type not meant as a boolean value? Then what is its purpose?

WASasquatch commented 1 year ago

A Boolean can only have two values. True or False. 0 or 1. Not a float. A float will be rounded to true of false.

On Thu, Oct 19, 2023, 9:45 AM Tim Verweij @.***> wrote:

I don't understand. Could you elaborate? Is the 'bool' type not meant as a boolean value? Then what is its purpose?

— Reply to this email directly, view it on GitHub https://github.com/WASasquatch/was-node-suite-comfyui/issues/239#issuecomment-1771360793, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIZEZP6SSIKPKHYLCF6XSDYAFKLBAVCNFSM6AAAAAA6G45CPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZRGM3DANZZGM . You are receiving this because you commented.Message ID: @.***>

TimmermanV commented 1 year ago

I'm not a Python programmer, but as far as I know any non-zero float is evaluated to true. E.g. this prints true:

if 0.1:
    print('true')
else:
    print('false')

Now in the Random number code there is this bit:

        # Return random number
        if number_type:
            if number_type == 'integer':
                number = random.randint(minimum, maximum)
            elif number_type == 'float':
                number = random.uniform(minimum, maximum)
            elif number_type == 'bool':
                number = random.random()
            else:
                return

        # Return number
        return (number, float(number), int(number))

So if the number_type is set to bool, number will be between 0.0 and 1.0. As a result, the first two outputs ("NUMBER" and "FLOAT") almost always evaluate to True and the last one ("INT") always evaluates to 0 and thus False. This is the issue I'm raising here.

WASasquatch commented 1 year ago

Any non-zero number is true in Python, thus any comparison of less then 0.5 or over 0.5 wouldn't work. So we round. Simple as that. Boolean is always either true or false. Nothing else.

On Thu, Oct 19, 2023, 11:10 PM Tim Verweij @.***> wrote:

I'm not a Python programmer, but as far as I know any non-zero float is evaluated to true. E.g. this prints true:

if 0.1: print('true') else: print('false')

Now in the Random number code there is this bit:

    # Return random number
    if number_type:
        if number_type == 'integer':
            number = random.randint(minimum, maximum)
        elif number_type == 'float':
            number = random.uniform(minimum, maximum)
        elif number_type == 'bool':
            number = random.random()
        else:
            return

    # Return number
    return (number, float(number), int(number))

So if the number_type is set to bool, number will be between 0.0 and 1.0. As a result, the first two outputs ("NUMBER" and "FLOAT") almost always evaluate to True and the last one ("INT") always evaluates to 0 and thus False. This is the issue I'm raising here.

— Reply to this email directly, view it on GitHub https://github.com/WASasquatch/was-node-suite-comfyui/issues/239#issuecomment-1772140419, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIZEZLUIUB65ZTUDUCKYCTYAIIUVAVCNFSM6AAAAAA6G45CPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZSGE2DANBRHE . You are receiving this because you commented.Message ID: @.***>

WASasquatch commented 1 year ago

Also python just had issues with floating approximation and precision.

On Fri, Oct 20, 2023, 8:33 AM Jordan Thompson @.***> wrote:

Any non-zero number is true in Python, thus any comparison of less then 0.5 or over 0.5 wouldn't work. So we round. Simple as that. Boolean is always either true or false. Nothing else.

On Thu, Oct 19, 2023, 11:10 PM Tim Verweij @.***> wrote:

I'm not a Python programmer, but as far as I know any non-zero float is evaluated to true. E.g. this prints true:

if 0.1: print('true') else: print('false')

Now in the Random number code there is this bit:

    # Return random number
    if number_type:
        if number_type == 'integer':
            number = random.randint(minimum, maximum)
        elif number_type == 'float':
            number = random.uniform(minimum, maximum)
        elif number_type == 'bool':
            number = random.random()
        else:
            return

    # Return number
    return (number, float(number), int(number))

So if the number_type is set to bool, number will be between 0.0 and 1.0. As a result, the first two outputs ("NUMBER" and "FLOAT") almost always evaluate to True and the last one ("INT") always evaluates to 0 and thus False. This is the issue I'm raising here.

— Reply to this email directly, view it on GitHub https://github.com/WASasquatch/was-node-suite-comfyui/issues/239#issuecomment-1772140419, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIZEZLUIUB65ZTUDUCKYCTYAIIUVAVCNFSM6AAAAAA6G45CPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZSGE2DANBRHE . You are receiving this because you commented.Message ID: @.***>

TimmermanV commented 1 year ago

So we round. Simple as that.

Where is this rounding done? Outside of this node? Then this node does not by itself generate a bool value.

I have the feeling we're not understanding each other. I was under the impression that I could get a random boolean value by setting the "number_type" of "Random Number" to "bool", but this is not the case.

WASasquatch commented 1 year ago

Again... A Boolean is True or False. Nothing else. So you get 0 or 1. Rounding is handled in receiving nodes in case they get a float, which is non-standard.

Just cause Python evals a float, doesn't make it a Boolean...

The reason it is way it is now is cause of all those scoring nodes so you can trigger an event based off the float value rounded to 0 or 1

On Fri, Oct 20, 2023, 8:44 AM Tim Verweij @.***> wrote:

So we round. Simple as that.

Where is this rounding done? Outside of this node? Then this node does not by itself generate a bool value.

I have the feeling we're not understanding each other. I was under the impression that I could get a random boolean value by setting the "number_type" of "Random Number" to "bool", but this is not the case.

— Reply to this email directly, view it on GitHub https://github.com/WASasquatch/was-node-suite-comfyui/issues/239#issuecomment-1772979305, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIZEZJYRMARVUQ2QZO36QTYAKL4DAVCNFSM6AAAAAA6G45CPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZSHE3TSMZQGU . You are receiving this because you commented.Message ID: @.***>

TimmermanV commented 1 year ago

So you get 0 or 1.

Where do I get this 0 or 1 you are referring to? The INT output is always 0.

WASasquatch commented 1 year ago

Ah, yeah, that's cause the output should be rounded, and was never changed with the other rounding operations as int() on a float will always be rounded down (doesn't care about 0.5 or 0.6 being rounded up. I patched that.