Jump to page: 1 2
Thread overview
deprecate boolean evaluation of floating point and character types
Apr 30
Basile B.
Apr 30
Daniel N
May 06
Basile B.
2 days ago
Quirin Schroll
2 days ago
Quirin Schroll
2 days ago
Nick Treleaven
2 days ago
Quirin Schroll
2 days ago
Quirin Schroll
April 30

Generally there is a strong correlation between default initialization and boolean evaluation to false. Excepted for floating point and characters types :

void main(string[] args)
{
    void* v;
    assert(!v);
    int s32;
    assert(!s32);
    long s64;
    assert(!s64);

    class C {}
    C c;
    assert(!c);

    struct S { bool opCast(T)(){return true;} }
    S s;
    assert(s);

    // etc ...

    float f64;
    assert(!f64);
}

instead of an assertion failure what we should get is rather a message such as

error, cannot evaluate f64 to a bool

May 01
Characters are quite often compared against zero when doing string handling in C without the binary expression. For this reason I would not want to see this changed.



Now floats on the other hand... there is no clear truthiness value associated with them, unless IEEE-754 defines one (it doesn't appear to for 2008 version).

C23 appears to not define if a floating point type can even convert to bool, although it does mention bool as not being supported in multiple places.

April 30
On Tuesday, 30 April 2024 at 18:09:22 UTC, Richard (Rikki) Andrew Cattermole wrote:
> Characters are quite often compared against zero when doing string handling in C without the binary expression. For this reason I would not want to see this changed.
>
>
>
> Now floats on the other hand... there is no clear truthiness value associated with them, unless IEEE-754 defines one (it doesn't appear to for 2008 version).
>
> C23 appears to not define if a floating point type can even convert to bool, although it does mention bool as not being supported in multiple places.

I think the most useful conversion would be.
NaN -> false
because that corresponds to .init

Futhermore you can't simply use == to compare with NaN you need to call isNan(), which complicates writing generic code.

May 01
On Tuesday, 30 April 2024 at 18:09:22 UTC, Richard (Rikki) Andrew Cattermole wrote:
> Characters are quite often compared against zero when doing string handling in C without the binary expression. For this reason I would not want to see this changed.


D has the philosophy that valid C code should either behave the same in C as in D, or give an error (c.f. integer promotion rules). It is reasonable to make misleading behaviour like this an error.

May 01
On 01/05/2024 3:45 PM, Nicholas Wilson wrote:
> On Tuesday, 30 April 2024 at 18:09:22 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> Characters are quite often compared against zero when doing string handling in C without the binary expression. For this reason I would not want to see this changed.
> 
> 
> D has the philosophy that valid C code should either behave the same in C as in D, or give an error (c.f. integer promotion rules). It is reasonable to make misleading behaviour like this an error.

Both C and D have the same behavior for characters.

```c
#include <stdio.h>

int main()
{
    char c = 0;

    if (c) {
        printf("+\n");
    }

    return 0;
}
```

No output.

```d
import core.stdc.stdio;

void main()
{
    char c = 0;

    if (c) {
        printf("+\n");
    }
}
```

No output.
May 01

On Tuesday, 30 April 2024 at 17:01:42 UTC, Basile B. wrote:

>

Generally there is a strong correlation between default initialization and boolean evaluation to false.

But the meaning of boolean evaluation of a number is to check if it is non-zero. That is well established from C.

>

instead of an assertion failure what we should get is rather a message such as

error, cannot evaluate f64 to a bool

Why break D code that was written with the non-zero test expectation? Why make porting from C harder yet still allow non-zero tests for integers? Wouldn't that be more confusing?

May 06

On Wednesday, 1 May 2024 at 10:01:29 UTC, Nick Treleaven wrote:

>

On Tuesday, 30 April 2024 at 17:01:42 UTC, Basile B. wrote:

>

Generally there is a strong correlation between default initialization and boolean evaluation to false.

But the meaning of boolean evaluation of a number is to check if it is non-zero. That is well established from C.

>

instead of an assertion failure what we should get is rather a message such as

error, cannot evaluate f64 to a bool

Why break D code that was written with the non-zero test expectation? Why make porting from C harder yet still allow non-zero tests for integers? Wouldn't that be more confusing?

This idea is a preliminary work for something bigger: I'd like to propose a DIP for inline variable declarations. For them I have the feeling that char.init and {float|double|real}.init would not work very well. I'm not 100%s sure.

I'd say that it's not worth discuting this anymore for now.

May 10

On Tuesday, 30 April 2024 at 17:01:42 UTC, Basile B. wrote:

>

instead of an assertion failure what we should get is rather a message such as

error, cannot evaluate f64 to a bool

With float you often want to be more specific anyway.

Do you mean isFinite?
Do you mean !isNaN?
Do you mean non-zero?

Can't really see a legit use for: if(float) { }

2 days ago

On Wednesday, 1 May 2024 at 10:01:29 UTC, Nick Treleaven wrote:

>

On Tuesday, 30 April 2024 at 17:01:42 UTC, Basile B. wrote:

>

Generally there is a strong correlation between default initialization and boolean evaluation to false.

But the meaning of boolean evaluation of a number is to check if it is non-zero. That is well established from C.

Not well enough for JavaScript. Just tried it: Boolean(0/0) gives false.

Deprecating floating-point to bool conversions (in conditions or otherwise) would be correct, including cast(bool)(x). What the user wants, usually, is !isNaN(x) && x != 0.0. Writing out x != 0.0 isn’t that hard.

Also, ++x is morally wrong for floating-point types.

2 days ago

On Friday, 10 May 2024 at 19:25:45 UTC, Guillaume Piolat wrote:

>

On Tuesday, 30 April 2024 at 17:01:42 UTC, Basile B. wrote:

>

instead of an assertion failure what we should get is rather a message such as

error, cannot evaluate f64 to a bool

With float you often want to be more specific anyway.

Do you mean isFinite?
Do you mean !isNaN?
Do you mean non-zero?

Can't really see a legit use for: if(float) { }

I wrote that once thinking it meant (!isNaN(x) && x != 0.0), thought I be smart. Introduced a bug. Maybe I overreacted, but I never used implicit conversions to bool in conditions ever. i != 0, x != 0.0, ptr !is null, it’s all so much clearer. Whenever I see if (x), I have to think what it means, unless x is a bool.

« First   ‹ Prev
1 2