Why doesn't C have unsigned floats?

后端 未结 12 1641
名媛妹妹
名媛妹妹 2020-11-28 01:47

I know, the question seems to be strange. Programmers sometimes think too much. Please read on...

In C I use signed and unsigned integers a

12条回答
  •  春和景丽
    2020-11-28 02:24

    A square-root will definately never return a negative number. There are other places as well where a negative float value has no meaning. Perfect candidate for an unsigned float.

    C99 supports complex numbers, and a type generic form of sqrt, so sqrt( 1.0 * I) will be negative.


    The commentors highlighted a slight gloss above, in that I was referring to the type-generic sqrt macro rather than the function, and it will return a scalar floating point value by truncation of the complex to its real component:

    #include 
    #include 
    
    int main () 
    {
        complex double a = 1.0 + 1.0 * I;
    
        double f = sqrt(a);
    
        return 0;
    }
    

    It also contains a brain-fart, as the real part of the sqrt of any complex number is positive or zero, and sqrt(1.0*I) is sqrt(0.5) + sqrt(0.5)*I not -1.0.

提交回复
热议问题