[Cuis-dev] Negative sqrt

Juan Vuletich juan at jvuletich.org
Sun Jan 3 08:12:03 PST 2021

On 12/31/2020 12:51 PM, Gerald Klix via Cuis-dev wrote:
> ...
>> Having worked with numerical algorithms using floating point, mainly 
>> in image and signal processing, but also in numerical linear algebra 
>> and discrete simulations, I never saw Signaling NaNs have any 
>> relevance. I don't think they are used much in practice. And they can 
>> be implemented in Smalltalk.
> I can well imagine that the standard's requirements are of little use 
> in practice. If you can live without signalling NaNs, I will have not 
> objections. 

I think that complying with the standard requirements is important. But 
I don't think supporting Signaling NaNs is a requirement. I believe they 
are optional, and if supported, their behavior must follow the standard. 
Anyway, the important part is that no program should ever give wrong 
results without an error.

As a side note, optimizing C compilers are pretty bad at this, because 
reordering floating point operations, even if correct by Math rules, 
usually gives different results.

> Before starting to read the standard, I quickly checked Python's 
> behavior in this case:
> bear at speedy ~/D/Specs> ~/py381/bin/python3
> Python 3.8.1 (tags/v3.8.1-dirty:1b293b6006, Feb  3 2020, 18:37:36)
> [GCC 7.4.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from math import sqrt
> >>> sqrt(-4.0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: math domain error
> I thought to myself: Here be dragons!
> When taking a closer look I discovered this behavior:
> >>> sqrt(-4.0+0j)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: can't convert complex to float
> >>> from cmath import sqrt
> >>> sqrt(-4.0+0j)
> 2j
> I don't know what to think about this.

Well, this behavior is pythonista style for what we did until this 
thread: Answer error if no Complex loaded, answer Complex if available. 
WRT the need to do 'from cmath import sqrt' and not the much better 
'import ComplexNumbers', what can I say?

>> Still, you may bring the issue to VM-Dev. If floating point 
>> primitives are made to fail when encountering a Signaling NaN, I'll 
>> be happy to raise an appropriate Smalltalk exception.
> I am not competent enough to do this.

If you care for Signaling NaNs, telling in VM-Dev is no different than 
telling here at all!

>> Thanks,
> Best Regards,
> Gerald


Juan Vuletich

More information about the Cuis-dev mailing list