Floating point in the type system

2015-09-12 Thread Robert via Digitalmars-d

Hi all,

I came across this example, and wondered what your thoughts on it 
are:



```
void main(string[] args)
{
struct Foo(float f) {
alias VAL = f;
float getF() {
return f;
}
}

Foo!(float.nan) f;
Foo!(float.nan) f2;

// This will fail at compile time
static assert(f.VAL == f2.VAL);

// This will fail at run time
assert(f.getF() == f2.getF());

// But this is ok
f = f2;
}
```

It seems a little unusual to me.

Robert


Re: Floating point in the type system

2015-09-12 Thread Robert via Digitalmars-d

On Saturday, 12 September 2015 at 15:49:23 UTC, Atila Neves wrote:

On Saturday, 12 September 2015 at 15:13:27 UTC, Robert wrote:

Hi all,

I came across this example, and wondered what your thoughts on 
it are:



```
void main(string[] args)
{
struct Foo(float f) {
alias VAL = f;
float getF() {
return f;
}
}

Foo!(float.nan) f;
Foo!(float.nan) f2;

// This will fail at compile time
static assert(f.VAL == f2.VAL);

// This will fail at run time
assert(f.getF() == f2.getF());

// But this is ok
f = f2;
}
```

It seems a little unusual to me.

Robert


What do think is unusual?

Atila


It's unusual, because `float.nan != float.nan`, so one might 
expect that `typeof(Foo!(float.nan) != Foo!(float.nan))`, whereas 
this is clearly not the case, even with both the static assert 
and runtime assert failing. I'm just curious to understand the 
reasoning behind this, whether it's intentional, and whether it 
matters at all.


Re: Floating point in the type system

2015-09-12 Thread Robert via Digitalmars-d

On Saturday, 12 September 2015 at 15:13:27 UTC, Robert wrote:

Hi all,

I came across this example, and wondered what your thoughts on 
it are:





It seems a little unusual to me.

Robert


For what it's worth, I was investigating this initially as a 
discussion about adding type-level values in Rust, and how to 
handle unusual cases like this. In the process we've managed to 
break the Idris type system: 
https://github.com/idris-lang/Idris-dev/issues/2609. There's been 
quite a lot of interesting discussion about it on the IRC 
channels for all three languages :) I'd be interested to know how 
other languages handle this, if anyone knows.