https://kotlinlang.org logo
p

Paul Woitaschek

05/27/2021, 8:36 AM
Can somebody explain, why
(0F/0F).coerceIn(0F,1F)
NaN
?
z

Zan Skamljic

05/27/2021, 8:37 AM
NaN cannot be mapped to range, as it's "*N*ot a *N*umber"
regular math and comparisons don't work on it, so it can't really become a valid number, either 0 or 1 that you have
p

Paul Woitaschek

05/27/2021, 8:38 AM
As it's not coerced in that case shouldn't it throw instead?
s

spand

05/27/2021, 8:38 AM
It seems pretty in line with the usual behavior that all operations involving
NaN
is
NaN
Not sure anything involving
NaN
is ever intuitive so I guess you could argue either way
p

Paul Woitaschek

05/27/2021, 8:40 AM
NaN was never helpful and is a bug in every single case I faced it 😕
z

Zan Skamljic

05/27/2021, 8:41 AM
as for throwing, that's java behavior, where it throws when you divide something by zero
s

spand

05/27/2021, 8:44 AM
@Zan Skamljic No. Why dont you try it out.
z

Zan Skamljic

05/27/2021, 8:44 AM
yes, actually
s

spand

05/27/2021, 8:44 AM
That is not the case we are discussing
g

gildor

05/27/2021, 8:44 AM
Because it’s int, float will return NaN
z

Zan Skamljic

05/27/2021, 8:45 AM
my bad, yeah, float seems to return NaN
g

gildor

05/27/2021, 8:45 AM
The reason of this behaviour is IEEE 754 and it’s widely adopted by programming languages and hardware
2
4 Views