Why doesn't C have unsigned floats?

后端 未结 12 1640
名媛妹妹
名媛妹妹 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:03

    (As an aside, Perl 6 lets you write

    subset Nonnegative::Float of Float where { $_ >= 0 };
    

    and then you can use Nonnegative::Float just like you would any other type.)

    There's no hardware support for unsigned floating point operations, so C doesn't offer it. C is mostly designed to be "portable assembly", that is, as close to the metal as you can be without being tied down to a specific platform.

    [edit]

    C is like assembly: what you see is exactly what you get. An implicit "I'll check that this float is nonnegative for you" goes against its design philosophy. If you really want it, you can add assert(x >= 0) or similar, but you have to do that explicitly.

提交回复
热议问题