On 5/29/2022 2:01 AM, Ben wrote:
> Manfred <non...@add.invalid> writes:
>
>> On 5/28/2022 9:43 PM, Scott Lurndal wrote:
>>> Bonita Montero <
Bonita....@gmail.com> writes:
>>>> In a superior language:
>>>>
>>>> #include <cstdint>
>>>> #include <cstring>
>>>> #include <limits>
>>>> #include <concepts>
>>>> #include <type_traits>
>>>>
>>>> template<typename FpType>
>>>> requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
>>>> && std::numeric_limits<FpType>::is_iec559
>>>> std::conditional_t<std::same_as<FpType, float>, std::int32_t,
>>>> std::int64_t> giveSign( FpType value )
>>>> {
>>>> using namespace std;
>>>> conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
>>>> memcpy( &iValue, &value, sizeof value );
>>>> return iValue;
>>>> }
>>>>
>>>> template
>>>> std::int32_t giveSign( float );
>>>>
>>>>
>>>> template
>>>> std::int64_t giveSign( double );
>>> In an even more superior language.
>>
>> Agreed with the "even superior" part, at least in this case.
>
> Although C++ also has the signbit functions.
Yes, but that comes from <math.h> or <cmath>, which is substantially
equivalent - in other words, this falls into the inclusion of C into C++.
On the other hand, the proposed alternative, C++ template based, is
considerably worse, IMO.
>
>>> #include <math.h>
>>> int main()
>>> {
>>> float_t number = -1.235;
>>> // sign = signbit(number);
>>
>> int sign = signbit(number);
>
> Yes, missing type but also
>
> int sign = signbitf(number);
I can't find a signbitf function or macro in the C or C++ standard.
>
> if likely to be what's wanted, even though float_t is not a standard
> type.
As Keith said, float_t is declared in <math.h> as part of the standard.
I was surprised too.
Anyway, the point stands that simpler solutions are often
> preferable.
Agreed, that is the gist of the "even superior" part.
>
>>> return sign;
>>> }
>