Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #81702 > unrolled thread

How to get mantissa of long double?

Started bywij <wyniijj@gmail.com>
First post2021-10-01 08:37 -0700
Last post2021-10-03 18:46 -0700
Articles 20 on this page of 103 — 17 participants

Back to article view | Back to comp.lang.c++


Contents

  How to get mantissa of long double? wij <wyniijj@gmail.com> - 2021-10-01 08:37 -0700
    Re: How to get mantissa of long double? RadicalRabbit@theburrow.co.uk - 2021-10-01 16:09 +0000
      Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 14:05 -0400
        Re: How to get mantissa of long double? wij <wyniijj@gmail.com> - 2021-10-01 17:23 -0700
          Re: How to get mantissa of long double? Manfred <noname@add.invalid> - 2021-10-02 18:58 +0200
            Re: How to get mantissa of long double? Manfred <noname@add.invalid> - 2021-10-02 19:21 +0200
    Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-01 19:18 +0200
      Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-01 19:20 +0200
        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-01 19:25 +0200
          Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-01 19:53 +0200
            Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-03 08:09 +0200
              Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-03 11:15 +0100
                Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-03 12:19 +0200
                  Re: How to get mantissa of long double? red floyd <no.spam@its.invalid> - 2021-10-03 18:03 -0700
                  Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-04 09:52 +0200
                    Re: How to get mantissa of long double? Juha Nieminen <nospam@thanks.invalid> - 2021-10-04 09:59 +0000
                    Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-04 13:50 +0200
                      Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-04 20:04 +0200
                        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-05 07:12 +0200
                          Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-05 09:47 +0200
                            Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-05 16:46 +0200
                              Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-05 19:04 +0200
                                Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-05 19:13 +0200
                            Re: How to get mantissa of long double? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-05 09:04 -0700
                              Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-05 19:14 +0200
                                Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-05 19:21 +0100
                                  Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 07:58 +0200
                                    Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-06 08:11 +0200
                                      Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 10:25 +0200
                                      Re: How to get mantissa of long double? scott@slp53.sl.home (Scott Lurndal) - 2021-10-06 14:25 +0000
                                        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 16:53 +0200
                                    Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-06 12:12 +0100
                                      Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 14:48 +0200
                                        Re: How to get mantissa of long double? red floyd <no.spam.here@its.invalid> - 2021-10-06 09:44 -0700
                                          Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 18:52 +0200
                                            Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-06 19:02 +0200
                                          Re: How to get mantissa of long double? Juha Nieminen <nospam@thanks.invalid> - 2021-10-07 05:35 +0000
                                            Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-07 08:20 +0200
                                              Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-07 08:38 +0200
                                                Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-07 08:42 +0200
                                                  Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-07 10:23 +0200
                                                    Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-08 20:23 +0200
                                                      Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-09 10:23 +0200
                                                        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-09 10:50 +0200
                                                          Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-09 09:02 +0000
                                                          Re: How to get mantissa of long double? scott@slp53.sl.home (Scott Lurndal) - 2021-10-09 14:57 +0000
                                                        Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-09 12:16 +0100
                                                          Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-09 15:10 +0200
                                                          Re: How to get mantissa of long double? red floyd <no.spam.here@its.invalid> - 2021-10-09 10:26 -0700
                                                        Re: How to get mantissa of long double? Vir Campestris <vir.campestris@invalid.invalid> - 2021-10-12 21:23 +0100
                                                          Re: How to get mantissa of long double? RadicalRabbit@theburrow.co.uk - 2021-10-13 10:30 +0000
                                            Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-07 08:38 +0200
                                              Re: How to get mantissa of long double? Juha Nieminen <nospam@thanks.invalid> - 2021-10-08 04:37 +0000
                                                Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-08 07:54 +0200
                                              Re: How to get mantissa of long double? red floyd <no.spam.here@its.invalid> - 2021-10-07 22:44 -0700
                                          Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-07 08:17 +0200
                                            Re: How to get mantissa of long double? Juha Nieminen <nospam@thanks.invalid> - 2021-10-08 04:39 +0000
                                              Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-08 09:25 +0200
                                                Re: How to get mantissa of long double? scott@slp53.sl.home (Scott Lurndal) - 2021-10-08 14:34 +0000
                                      Re: How to get mantissa of long double? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-10-06 13:57 -0700
                                Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-05 19:34 +0000
                              Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-05 19:33 +0000
                          Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-05 10:19 +0000
                    Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-04 14:06 +0100
                      Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-04 16:12 +0200
                        Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 16:15 +0000
                      Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 16:15 +0000
                        Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-05 09:48 +0200
                      Re: How to get mantissa of long double? Juha Nieminen <nospam@thanks.invalid> - 2021-10-05 05:19 +0000
                        Re: How to get mantissa of long double? David Brown <david.brown@hesbynett.no> - 2021-10-05 10:00 +0200
                        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-05 16:47 +0200
                    Re: How to get mantissa of long double? scott@slp53.sl.home (Scott Lurndal) - 2021-10-04 14:37 +0000
                      Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-04 16:53 +0200
                      Re: How to get mantissa of long double? scott@slp53.sl.home (Scott Lurndal) - 2021-10-04 17:08 +0000
                        Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-04 20:22 +0100
      Re: How to get mantissa of long double? Bart <bc@freeuk.com> - 2021-10-01 19:09 +0100
        Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-01 20:16 +0200
    Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-01 18:53 +0000
      Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 20:19 -0400
        Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-02 02:16 +0000
          Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 01:28 -0400
          Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 01:56 -0400
            Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-02 10:59 +0000
              Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 14:57 -0400
              Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 15:37 -0400
                Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-03 00:47 +0000
                  Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-03 23:42 -0400
                    Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 04:35 +0000
    Re: How to get mantissa of long double? Bonita Montero <Bonita.Montero@gmail.com> - 2021-10-02 12:09 +0200
    Re: How to get mantissa of long double? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-10-02 21:00 +0200
      Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 15:12 -0400
        Re: How to get mantissa of long double? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-10-02 21:23 +0200
          Re: How to get mantissa of long double? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-10-02 21:27 +0200
            Re: How to get mantissa of long double? Manfred <noname@add.invalid> - 2021-10-02 23:19 +0200
          Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-03 23:43 -0400
        Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-03 00:45 +0000
          Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-03 23:31 -0400
            Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 04:34 +0000
              Re: How to get mantissa of long double? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-04 11:30 -0400
                Re: How to get mantissa of long double? Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 16:19 +0000
      Re: How to get mantissa of long double? wij <wyniijj@gmail.com> - 2021-10-03 00:35 -0700
    Re: How to get mantissa of long double? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-03 15:02 +0100
      Re: How to get mantissa of long double? wij <wyniijj@gmail.com> - 2021-10-03 18:46 -0700

Page 1 of 6  [1] 2 3 4 5 6  Next page →


#81702 — How to get mantissa of long double?

Fromwij <wyniijj@gmail.com>
Date2021-10-01 08:37 -0700
SubjectHow to get mantissa of long double?
Message-ID<c19306a0-c1ea-419e-9604-d0e124c358e0n@googlegroups.com>
// numeric_limits<long double>::digits=64.
//
typedef long double FType;
FType x=numeric_limits<FType>::max();
int iexp;
int64_t mint;
x=::frexpl(x,&iexp);
x=::ldexpl(x,numeric_limits<FType>::digits);
mint= static_cast<int64_t>(x);

Result (mint) is a negative number, something not right!!!

[toc] | [next] | [standalone]


#81703

FromRadicalRabbit@theburrow.co.uk
Date2021-10-01 16:09 +0000
Message-ID<sj7br6$qk2$1@gioia.aioe.org>
In reply to#81702
On Fri, 1 Oct 2021 08:37:21 -0700 (PDT)
wij <wyniijj@gmail.com> wrote:
>// numeric_limits<long double>::digits=64.
>//
>typedef long double FType;
>FType x=numeric_limits<FType>::max();
>int iexp;
>int64_t mint;
>x=::frexpl(x,&iexp);
>x=::ldexpl(x,numeric_limits<FType>::digits);
>mint= static_cast<int64_t>(x);
>
>Result (mint) is a negative number, something not right!!!

It might help if you set iexp to some value.

[toc] | [prev] | [next] | [standalone]


#81709

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2021-10-01 14:05 -0400
Message-ID<sj7ilh$6bk$1@dont-email.me>
In reply to#81703
On 10/1/21 12:09 PM, RadicalRabbit@theburrow.co.uk wrote:
> On Fri, 1 Oct 2021 08:37:21 -0700 (PDT)
> wij <wyniijj@gmail.com> wrote:
>> // numeric_limits<long double>::digits=64.
>> //
>> typedef long double FType;
>> FType x=numeric_limits<FType>::max();
>> int iexp;
>> int64_t mint;
>> x=::frexpl(x,&iexp);
>> x=::ldexpl(x,numeric_limits<FType>::digits);
>> mint= static_cast<int64_t>(x);
>>
>> Result (mint) is a negative number, something not right!!!

I can't duplicate this problem: I get mint:9223372036854775807.

> It might help if you set iexp to some value.

frexpl() is from the part of the C++ standard library that is copied
from the C standard library.

Section 7.12.6.7p1 of the C standard says:
> long double frexpl(long double value, int *p);

The following paragraph says:
> The frexp functions break a floating-point number into a normalized fraction and an integer exponent. They store the integer in the int object pointed to by p .

So iexp should be set. When I ran the code, it got set to a value of
16384. That's not the problem.

[toc] | [prev] | [next] | [standalone]


#81727

Fromwij <wyniijj@gmail.com>
Date2021-10-01 17:23 -0700
Message-ID<838b5e50-a8f9-4bac-a034-7bd1e649ac27n@googlegroups.com>
In reply to#81709
On Saturday, 2 October 2021 at 02:05:56 UTC+8, james...@alumni.caltech.edu wrote:
> On 10/1/21 12:09 PM, Radica...@theburrow.co.uk wrote: 
> > On Fri, 1 Oct 2021 08:37:21 -0700 (PDT) 
> > wij <wyn...@gmail.com> wrote: 
> >> // numeric_limits<long double>::digits=64. 
> >> // 
> >> typedef long double FType; 
> >> FType x=numeric_limits<FType>::max(); 
> >> int iexp; 
> >> int64_t mint; 
> >> x=::frexpl(x,&iexp); 
> >> x=::ldexpl(x,numeric_limits<FType>::digits); 
> >> mint= static_cast<int64_t>(x); 
> >> 
> >> Result (mint) is a negative number, something not right!!!
> I can't duplicate this problem: I get mint:9223372036854775807.
> > It might help if you set iexp to some value.
> frexpl() is from the part of the C++ standard library that is copied 
> from the C standard library. 
> 
> Section 7.12.6.7p1 of the C standard says: 
> > long double frexpl(long double value, int *p); 
> 
> The following paragraph says: 
> > The frexp functions break a floating-point number into a normalized fraction and an integer exponent. They store the integer in the int object pointed to by p . 
> 
> So iexp should be set. When I ran the code, it got set to a value of 
> 16384. That's not the problem.

// ----- file t.cpp -----
#include <math.h>
#include <limits>
#include <iostream>

using namespace std;
#define ENDL endl

template<typename T>
int64_t get_mant(T x) {
 int iexp;
 x=frexp(x,&iexp);
 x=ldexp(x,numeric_limits<T>::digits);
 return static_cast<int64_t>(x);
};

int main()
{
 cout << dec << get_mant(numeric_limits<float>::max()) << ", "
      << hex << get_mant(numeric_limits<float>::max()) << ENDL;
 cout << dec << get_mant(numeric_limits<double>::max()) << ", "
      << hex << get_mant(numeric_limits<double>::max()) << ENDL;
 cout << dec << get_mant(numeric_limits<long double>::max()) << ", "
      << hex << get_mant(numeric_limits<long double>::max()) << ENDL;
 return 0;
};
// end file t.cpp -----

$ g++ t.cpp
]$ ./a.out 
16777215, ffffff
9007199254740991, 1fffffffffffff
-9223372036854775808, 8000000000000000

[toc] | [prev] | [next] | [standalone]


#81788

FromManfred <noname@add.invalid>
Date2021-10-02 18:58 +0200
Message-ID<sja34j$10k8$1@gioia.aioe.org>
In reply to#81727
On 10/2/2021 2:23 AM, wij wrote:
> On Saturday, 2 October 2021 at 02:05:56 UTC+8, james...@alumni.caltech.edu wrote:
>> On 10/1/21 12:09 PM, Radica...@theburrow.co.uk wrote:
>>> On Fri, 1 Oct 2021 08:37:21 -0700 (PDT)
>>> wij <wyn...@gmail.com> wrote:
>>>> // numeric_limits<long double>::digits=64.
>>>> //
>>>> typedef long double FType;
>>>> FType x=numeric_limits<FType>::max();
>>>> int iexp;
>>>> int64_t mint;
>>>> x=::frexpl(x,&iexp);
>>>> x=::ldexpl(x,numeric_limits<FType>::digits);
>>>> mint= static_cast<int64_t>(x);
>>>>
>>>> Result (mint) is a negative number, something not right!!!
>> I can't duplicate this problem: I get mint:9223372036854775807.
<snip>
> 
> // ----- file t.cpp -----
> #include <math.h>
> #include <limits>
> #include <iostream>
> 
> using namespace std;
> #define ENDL endl
> 
> template<typename T>
> int64_t get_mant(T x) {
>   int iexp;
>   x=frexp(x,&iexp);
>   x=ldexp(x,numeric_limits<T>::digits);
>   return static_cast<int64_t>(x);
> };
> 
> int main()
> {
>   cout << dec << get_mant(numeric_limits<float>::max()) << ", "
>        << hex << get_mant(numeric_limits<float>::max()) << ENDL;
>   cout << dec << get_mant(numeric_limits<double>::max()) << ", "
>        << hex << get_mant(numeric_limits<double>::max()) << ENDL;
>   cout << dec << get_mant(numeric_limits<long double>::max()) << ", "
>        << hex << get_mant(numeric_limits<long double>::max()) << ENDL;
>   return 0;
> };
> // end file t.cpp -----
> 
> $ g++ t.cpp
> ]$ ./a.out
> 16777215, ffffff
> 9007199254740991, 1fffffffffffff
> -9223372036854775808, 8000000000000000
> 

There are two problems:

1)
The templated function uses 'frexp' and 'ldexp', which take both double 
arguments (not *long* double), hence UB occurs at those calls for the 
'long double' type whenever this type is actually larger than 'double'.

2)
On my Linux box numeric_limits<*long* double>::digits is 64 
(numeric_limits<double>::digits is 53), so the static_cast<int64_t>(x) 
yields UB again.

=========
#include <cmath>
#include <limits>
#include <iostream>

using namespace std;
#define ENDL endl

uint64_t get_mantf(float x)
{
   int iexp;
   x=frexp(x,&iexp);
   x=ldexp(x,numeric_limits<float>::digits);
   return static_cast<uint64_t>(x);
};

uint64_t get_mant(double x)
{
   int iexp;
   x=frexp(x,&iexp);
   x=ldexp(x,numeric_limits<double>::digits);
   return static_cast<uint64_t>(x);
};

uint64_t get_mantl(long double x)
{
   int iexp;
   x=frexp(x,&iexp);
   x=ldexp(x,numeric_limits<long double>::digits);
   return static_cast<uint64_t>(x);
};

int main()
{
   cout << dec << numeric_limits<float>::digits << ", " << 
get_mantf(numeric_limits<float>::max()) << ", "
     << hex << get_mantf(numeric_limits<float>::max()) << ENDL;
   cout << dec << numeric_limits<double>::digits << ", " << 
get_mant(numeric_limits<double>::max()) << ", "
     << hex << get_mant(numeric_limits<double>::max()) << ENDL;
   cout << dec << numeric_limits<long double>::digits << ", " << 
get_mantl(numeric_limits<long double>::max()) << ", "
     << hex << get_mantl(numeric_limits<long double>::max()) << ENDL;
   return 0;
}
===================
$ c++ -std=c++11 -O2 -Wall mant.cc && ./a.out
24, 16777215, ffffff
53, 9007199254740991, 1fffffffffffff
64, 18446744073709551615, ffffffffffffffff

[toc] | [prev] | [next] | [standalone]


#81789

FromManfred <noname@add.invalid>
Date2021-10-02 19:21 +0200
Message-ID<sja4ep$1m4i$1@gioia.aioe.org>
In reply to#81788
On 10/2/2021 6:58 PM, Manfred wrote:
> On 10/2/2021 2:23 AM, wij wrote:
>> On Saturday, 2 October 2021 at 02:05:56 UTC+8, 
>> james...@alumni.caltech.edu wrote:
>>> On 10/1/21 12:09 PM, Radica...@theburrow.co.uk wrote:
>>>> On Fri, 1 Oct 2021 08:37:21 -0700 (PDT)
>>>> wij <wyn...@gmail.com> wrote:
>>>>> // numeric_limits<long double>::digits=64.
>>>>> //
>>>>> typedef long double FType;
>>>>> FType x=numeric_limits<FType>::max();
>>>>> int iexp;
>>>>> int64_t mint;
>>>>> x=::frexpl(x,&iexp);
>>>>> x=::ldexpl(x,numeric_limits<FType>::digits);
>>>>> mint= static_cast<int64_t>(x);
>>>>>
>>>>> Result (mint) is a negative number, something not right!!!
>>> I can't duplicate this problem: I get mint:9223372036854775807.
> <snip>
>>
>> // ----- file t.cpp -----
>> #include <math.h>
>> #include <limits>
>> #include <iostream>
>>
>> using namespace std;
>> #define ENDL endl
>>
>> template<typename T>
>> int64_t get_mant(T x) {
>>   int iexp;
>>   x=frexp(x,&iexp);
>>   x=ldexp(x,numeric_limits<T>::digits);
>>   return static_cast<int64_t>(x);
>> };
>>
>> int main()
>> {
>>   cout << dec << get_mant(numeric_limits<float>::max()) << ", "
>>        << hex << get_mant(numeric_limits<float>::max()) << ENDL;
>>   cout << dec << get_mant(numeric_limits<double>::max()) << ", "
>>        << hex << get_mant(numeric_limits<double>::max()) << ENDL;
>>   cout << dec << get_mant(numeric_limits<long double>::max()) << ", "
>>        << hex << get_mant(numeric_limits<long double>::max()) << ENDL;
>>   return 0;
>> };
>> // end file t.cpp -----
>>
>> $ g++ t.cpp
>> ]$ ./a.out
>> 16777215, ffffff
>> 9007199254740991, 1fffffffffffff
>> -9223372036854775808, 8000000000000000
>>
> 
> There are two problems:
> 
> 1)
> The templated function uses 'frexp' and 'ldexp', which take both double 
> arguments (not *long* double), hence UB occurs at those calls for the 
> 'long double' type whenever this type is actually larger than 'double'.
> 
> 2)
> On my Linux box numeric_limits<*long* double>::digits is 64 
> (numeric_limits<double>::digits is 53), so the static_cast<int64_t>(x) 
> yields UB again.
> 
> =========
> #include <cmath>
> #include <limits>
> #include <iostream>
> 
> using namespace std;
> #define ENDL endl
> 
> uint64_t get_mantf(float x)
> {
>    int iexp;
>    x=frexp(x,&iexp);
>    x=ldexp(x,numeric_limits<float>::digits);
>    return static_cast<uint64_t>(x);
> };
> 
> uint64_t get_mant(double x)
> {
>    int iexp;
>    x=frexp(x,&iexp);
>    x=ldexp(x,numeric_limits<double>::digits);
>    return static_cast<uint64_t>(x);
> };
> 
> uint64_t get_mantl(long double x)
> {
>    int iexp;
>    x=frexp(x,&iexp);
>    x=ldexp(x,numeric_limits<long double>::digits);
>    return static_cast<uint64_t>(x);
> };
> 
> int main()
> {
>    cout << dec << numeric_limits<float>::digits << ", " << 
> get_mantf(numeric_limits<float>::max()) << ", "
>      << hex << get_mantf(numeric_limits<float>::max()) << ENDL;
>    cout << dec << numeric_limits<double>::digits << ", " << 
> get_mant(numeric_limits<double>::max()) << ", "
>      << hex << get_mant(numeric_limits<double>::max()) << ENDL;
>    cout << dec << numeric_limits<long double>::digits << ", " << 
> get_mantl(numeric_limits<long double>::max()) << ", "
>      << hex << get_mantl(numeric_limits<long double>::max()) << ENDL;
>    return 0;
> }
> ===================
> $ c++ -std=c++11 -O2 -Wall mant.cc && ./a.out
> 24, 16777215, ffffff
> 53, 9007199254740991, 1fffffffffffff
> 64, 18446744073709551615, ffffffffffffffff

Sorry, that should have been:

=====
uint64_t get_mantf(float x)
{
   int iexp;
   x=frexpf(x,&iexp);
   x=ldexpf(x,numeric_limits<float>::digits);
   return static_cast<uint64_t>(x);
};

uint64_t get_mant(double x)
{
   int iexp;
   x=frexp(x,&iexp);
   x=ldexp(x,numeric_limits<double>::digits);
   return static_cast<uint64_t>(x);
};

uint64_t get_mantl(long double x)
{
   int iexp;
   x=frexpl(x,&iexp);
   x=ldexpl(x,numeric_limits<long double>::digits);
   return static_cast<uint64_t>(x);
};
=====

However, the three distinct functions with frexp and ldexp in all three 
still work on my box (I'm guessing gcc is still able to compile the 
correct implementation in in this case), but the template doesn't.

[toc] | [prev] | [next] | [standalone]


#81704

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-01 19:18 +0200
Message-ID<sj7ftm$f4u$1@dont-email.me>
In reply to#81702
Am 01.10.2021 um 17:37 schrieb wij:
> // numeric_limits<long double>::digits=64.
> //
> typedef long double FType;
> FType x=numeric_limits<FType>::max();
> int iexp;
> int64_t mint;
> x=::frexpl(x,&iexp);
> x=::ldexpl(x,numeric_limits<FType>::digits);
> mint= static_cast<int64_t>(x);
> 
> Result (mint) is a negative number, something not right!!!

Take this:

#include <iostream>
#include <cstdint>
#include <limits>
#include <utility>
#include <iomanip>

using namespace std;

using mantissa_pair = pair<bool, uint64_t>;

mantissa_pair getMantissa( double value );

int main()
{
	double v = numeric_limits<double>::min();
	do
	{
		mantissa_pair mp = getMantissa( v );
		cout << "value: " << v;
		if( mp.first )
			cout << " mantissa: " << hex << mp.second << endl;
		else
			cout << " invalid mantissa (Inf, S(NaN))" << endl;
	} while( (v *= 2.0) != numeric_limits<double>::infinity() );
}

static_assert(numeric_limits<double>::is_iec559, "must be standard fp");

mantissa_pair getMantissa( double value )
{
	union
	{
		uint64_t binary;
		double value;
	} u;
	u.value = value;
	unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
	if( exponent == 0 )
		return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | 
0x10000000000000u );
	if( exponent == 0x7FF )
		return pair<bool, uint64_t>( false, 0 );
	return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | 
0x10000000000000u );
}

[toc] | [prev] | [next] | [standalone]


#81705

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-01 19:20 +0200
Message-ID<sj7g1o$f4u$2@dont-email.me>
In reply to#81704
Am 01.10.2021 um 19:18 schrieb Bonita Montero:
> Am 01.10.2021 um 17:37 schrieb wij:
>> // numeric_limits<long double>::digits=64.
>> //
>> typedef long double FType;
>> FType x=numeric_limits<FType>::max();
>> int iexp;
>> int64_t mint;
>> x=::frexpl(x,&iexp);
>> x=::ldexpl(x,numeric_limits<FType>::digits);
>> mint= static_cast<int64_t>(x);
>>
>> Result (mint) is a negative number, something not right!!!
> 
> Take this:
> 
> #include <iostream>
> #include <cstdint>
> #include <limits>
> #include <utility>
> #include <iomanip>
> 
> using namespace std;
> 
> using mantissa_pair = pair<bool, uint64_t>;
> 
> mantissa_pair getMantissa( double value );
> 
> int main()
> {
>      double v = numeric_limits<double>::min();
>      do
>      {
>          mantissa_pair mp = getMantissa( v );
>          cout << "value: " << v;
>          if( mp.first )
>              cout << " mantissa: " << hex << mp.second << endl;
>          else
>              cout << " invalid mantissa (Inf, S(NaN))" << endl;
>      } while( (v *= 2.0) != numeric_limits<double>::infinity() );
> }
> 
> static_assert(numeric_limits<double>::is_iec559, "must be standard fp");
> 
> mantissa_pair getMantissa( double value )
> {
>      union
>      {
>          uint64_t binary;
>          double value;
>      } u;
>      u.value = value;
>      unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
>      if( exponent == 0 )
>          return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu 
> | 0x10000000000000u );

Should be:
           return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu );


>      if( exponent == 0x7FF )
>          return pair<bool, uint64_t>( false, 0 );
>      return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | 
> 0x10000000000000u );
> }

[toc] | [prev] | [next] | [standalone]


#81706

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-01 19:25 +0200
Message-ID<sj7g9f$hsk$1@dont-email.me>
In reply to#81705
>> mantissa_pair getMantissa( double value )
>> {
>>      union
>>      {
>>          uint64_t binary;
>>          double value;
>>      } u;
>>      u.value = value;
>>      unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
>>      if( exponent == 0 )
>>          return pair<bool, uint64_t>( true, u.binary & 
>> 0xFFFFFFFFFFFFFu | 0x10000000000000u );
> 
> Should be:
>            return pair<bool, uint64_t>( true, u.binary & 
> 0xFFFFFFFFFFFFFu );
> 
> 
>>      if( exponent == 0x7FF )
>>          return pair<bool, uint64_t>( false, 0 );
>>      return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | 
>> 0x10000000000000u );
>> }

Now with maximum efficiency:

mantissa_pair getMantissa( double value )
{
	union
	{
		uint64_t binary;
		double value;
	} u;
	u.value = value;
	unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
	if( exponent == 0x7FF )
		return pair<bool, uint64_t>( false, 0 );
	uint64_t hiBit = (uint64_t)(exponent != 0) << 52;
	return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | hiBit );
}

[toc] | [prev] | [next] | [standalone]


#81708

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-01 19:53 +0200
Message-ID<sj7hv5$v3u$1@dont-email.me>
In reply to#81706
Am 01.10.2021 um 19:25 schrieb Bonita Montero:

> mantissa_pair getMantissa( double value )
> {
>      union
>      {
>          uint64_t binary;
>          double value;
>      } u;
>      u.value = value;
>      unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
>      if( exponent == 0x7FF )
>          return pair<bool, uint64_t>( false, 0 );
>      uint64_t hiBit = (uint64_t)(exponent != 0) << 52;
>      return pair<bool, uint64_t>( true, u.binary & 0xFFFFFFFFFFFFFu | 
> hiBit );
> }

Oh, I think this would be the best solution for 0x7ff-exponents:

mantissa_pair getMantissa( double value )
{
	union
	{
		uint64_t binary;
		double value;
	} u;
	u.value = value;
	unsigned exponent = (unsigned)(u.binary >> 52) & 0x7FF;
	uint64_t mantissa = u.binary & 0xFFFFFFFFFFFFFu;
	if( exponent == 0x7FF )
		return pair<bool, uint64_t>( false, mantissa );
	uint64_t hiBit = (uint64_t)(exponent != 0) << 52;
	return pair<bool, uint64_t>( true, mantissa | hiBit );
}

Return the mantisssa also with false in .first for Inf and (S)NaN.

[toc] | [prev] | [next] | [standalone]


#81800

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-03 08:09 +0200
Message-ID<sjbhfj$fd7$1@dont-email.me>
In reply to#81708
So, this should be the most elegant code:

#pragma once
#include <limits>
#include <cstdint>
#include <cassert>

struct dbl_parts
{
	static_assert(std::numeric_limits<double>::is_iec559, "must be standard 
fp");
	dbl_parts( double d );
	dbl_parts &operator =( double d );
	dbl_parts() = default;
	operator double();
	bool getSign();
	std::uint16_t getBiasedExponent();
	std::int16_t getExponent();
	std::uint64_t getMantissa();
	void setSign( bool sign );
	void setBiasedExponent( uint16_t exp );
	void setExponent( int16_t exp );
	void setMantissa( uint64_t mantissa );
private:
	static unsigned const
		MANTISSA_BITS = 52;
	using i64 = std::int64_t;
	using ui64 = std::uint64_t;
	static ui64 const
		SIGN_MASK = (ui64)1 << 63,
		EXP_MASK = (ui64)0x7FF << MANTISSA_BITS,
		MANTISSA_MASK = ~-((i64)1 << MANTISSA_BITS),
		MANTISSA_MAX = ((ui64)1 << MANTISSA_BITS) | MANTISSA_MASK;
	using ui16 = std::uint16_t;
	using i16 = std::int16_t;
	static ui16 const
		BEXP_DENORMAL = 0,
		BEXP_BASE = 0x3FF,
		BEXP_MAX = 0x7FF;
	static i16 const
		EXP_MIN = 0 - BEXP_BASE,
		EXP_MAX = BEXP_MAX - BEXP_BASE;
	union
	{
		double value;
		ui64 binary;
	};
};

inline
dbl_parts::dbl_parts( double d ) :
	value( d )
{
}

inline
dbl_parts &dbl_parts::operator =( double d )
{
	value = d;
	return *this;
}

inline
dbl_parts::operator double()
{
	return value;
}

inline
bool dbl_parts::getSign()
{
	return (i64)binary < 0;
}

inline
std::uint16_t dbl_parts::getBiasedExponent()
{
	return (ui16)(binary >> MANTISSA_BITS) & BEXP_MAX;
}

inline
int16_t dbl_parts::getExponent()
{
	return (i16)(getBiasedExponent() - BEXP_BASE);
}

inline
std::uint64_t dbl_parts::getMantissa()
{
	ui16 bExp = getBiasedExponent();
	ui64 hiBit = (ui64)(bExp && bExp != BEXP_MAX) << MANTISSA_BITS;
	return binary & MANTISSA_MASK | hiBit;
}

inline
void dbl_parts::setSign( bool sign )
{
	binary = binary & ~SIGN_MASK | (ui64)sign << 63;
}

inline
void dbl_parts::setBiasedExponent( std::uint16_t exp )
{
	assert(exp <= BEXP_MAX);
	binary = binary & (SIGN_MASK | MANTISSA_MASK) | (ui64)exp << MANTISSA_BITS;
}

inline
void dbl_parts::setExponent( std::int16_t exp )
{
	exp += BEXP_BASE;
	setBiasedExponent( exp );
}

inline
void dbl_parts::setMantissa( std::uint64_t mantissa )
{
#if !defined(NDEBUG)
	ui64 mantissaMax = MANTISSA_MASK | (ui64)(getBiasedExponent() != 
BEXP_DENORMAL && getBiasedExponent() != BEXP_MAX) << MANTISSA_BITS;
	assert(mantissa <= mantissaMax);
#endif
	binary = binary & (SIGN_MASK | EXP_MASK) | mantissa & MANTISSA_MASK;
}

[toc] | [prev] | [next] | [standalone]


#81803

FromBart <bc@freeuk.com>
Date2021-10-03 11:15 +0100
Message-ID<sjbvs8$9d9$1@dont-email.me>
In reply to#81800
On 03/10/2021 07:09, Bonita Montero wrote:
> So, this should be the most elegant code:
> 
> #pragma once
> #include <limits>
> #include <cstdint>
> #include <cassert>
> 
> struct dbl_parts
> {
>      static_assert(std::numeric_limits<double>::is_iec559, "must be 
> standard fp");
>      dbl_parts( double d );
>      dbl_parts &operator =( double d );
>      dbl_parts() = default;
>      operator double();
>      bool getSign();
>      std::uint16_t getBiasedExponent();
>      std::int16_t getExponent();
>      std::uint64_t getMantissa();
>      void setSign( bool sign );
>      void setBiasedExponent( uint16_t exp );
>      void setExponent( int16_t exp );
>      void setMantissa( uint64_t mantissa );
> private:
>      static unsigned const
>          MANTISSA_BITS = 52;
>      using i64 = std::int64_t;
>      using ui64 = std::uint64_t;
>      static ui64 const
>          SIGN_MASK = (ui64)1 << 63,
>          EXP_MASK = (ui64)0x7FF << MANTISSA_BITS,
>          MANTISSA_MASK = ~-((i64)1 << MANTISSA_BITS),
>          MANTISSA_MAX = ((ui64)1 << MANTISSA_BITS) | MANTISSA_MASK;
>      using ui16 = std::uint16_t;
>      using i16 = std::int16_t;
>      static ui16 const
>          BEXP_DENORMAL = 0,
>          BEXP_BASE = 0x3FF,
>          BEXP_MAX = 0x7FF;
>      static i16 const
>          EXP_MIN = 0 - BEXP_BASE,
>          EXP_MAX = BEXP_MAX - BEXP_BASE;
>      union
>      {
>          double value;
>          ui64 binary;
>      };
> };
> 
> inline
> dbl_parts::dbl_parts( double d ) :
>      value( d )
> {
> }
> 
> inline
> dbl_parts &dbl_parts::operator =( double d )
> {
>      value = d;
>      return *this;
> }
> 
> inline
> dbl_parts::operator double()
> {
>      return value;
> }
> 
> inline
> bool dbl_parts::getSign()
> {
>      return (i64)binary < 0;
> }
> 
> inline
> std::uint16_t dbl_parts::getBiasedExponent()
> {
>      return (ui16)(binary >> MANTISSA_BITS) & BEXP_MAX;
> }
> 
> inline
> int16_t dbl_parts::getExponent()
> {
>      return (i16)(getBiasedExponent() - BEXP_BASE);
> }
> 
> inline
> std::uint64_t dbl_parts::getMantissa()
> {
>      ui16 bExp = getBiasedExponent();
>      ui64 hiBit = (ui64)(bExp && bExp != BEXP_MAX) << MANTISSA_BITS;
>      return binary & MANTISSA_MASK | hiBit;
> }
> 
> inline
> void dbl_parts::setSign( bool sign )
> {
>      binary = binary & ~SIGN_MASK | (ui64)sign << 63;
> }
> 
> inline
> void dbl_parts::setBiasedExponent( std::uint16_t exp )
> {
>      assert(exp <= BEXP_MAX);
>      binary = binary & (SIGN_MASK | MANTISSA_MASK) | (ui64)exp << 
> MANTISSA_BITS;
> }
> 
> inline
> void dbl_parts::setExponent( std::int16_t exp )
> {
>      exp += BEXP_BASE;
>      setBiasedExponent( exp );
> }
> 
> inline
> void dbl_parts::setMantissa( std::uint64_t mantissa )
> {
> #if !defined(NDEBUG)
>      ui64 mantissaMax = MANTISSA_MASK | (ui64)(getBiasedExponent() != 
> BEXP_DENORMAL && getBiasedExponent() != BEXP_MAX) << MANTISSA_BITS;
>      assert(mantissa <= mantissaMax);
> #endif
>      binary = binary & (SIGN_MASK | EXP_MASK) | mantissa & MANTISSA_MASK;
> }

Jesus. And I think this still doesn't do an actual long double!

If you know you're running on an x86/x64 device (or even an 8088 with 
8087 co-processor!), then this inline code expands a 64-bit double 'x64' 
to its constituent parts:

      fld qword [x64]
      fstp tword [a80]               ; sometimes, 'tbyte'

And for a long double 'x80' known to use 80-bit format:

      fld tword [x80]
      fstp tword [a80]

The former works because on the x87, all loads expand to an 80-bit 
internal format with no hidden parts.

'a80' needs to be an instance of a type like this (here assumes 
little-endian memory format, which is typical for anything with x87):

    typedef struct {
        uint64_t mantissa;
        uint16_t sign_and_exponent;  // sign is top bit
    } ldformat;

I don't know how to reliably split those last 16 bits into 15- and 1-bit 
fields using C's bitfields. (I used a different test language.)

ASM code shown may need adapting to gcc-style assembly.

[toc] | [prev] | [next] | [standalone]


#81804

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-03 12:19 +0200
Message-ID<sjc03i$a01$1@dont-email.me>
In reply to#81803
Am 03.10.2021 um 12:15 schrieb Bart:

> Jesus. And I think this still doesn't do an actual long double!

long double isn't supported by many compilers for x86-64.
long double should be avoided when possible because loads
and stores are slow with long double.

[toc] | [prev] | [next] | [standalone]


#81814

Fromred floyd <no.spam@its.invalid>
Date2021-10-03 18:03 -0700
Message-ID<sjdjtq$26h$1@redfloyd.dont-email.me>
In reply to#81804
On 10/3/21 3:19 AM, Bonita Montero wrote:
> Am 03.10.2021 um 12:15 schrieb Bart:
> 
>> Jesus. And I think this still doesn't do an actual long double!
> 
> long double isn't supported by many compilers for x86-64.
> long double should be avoided when possible because loads
> and stores are slow with long double.
> 

FFS, Bonita, READ THE FRICKIN' SUBJECT LINE!!!

OP wanted to get the mantissa of long double.

[toc] | [prev] | [next] | [standalone]


#81823

FromDavid Brown <david.brown@hesbynett.no>
Date2021-10-04 09:52 +0200
Message-ID<sjebr5$ut3$1@dont-email.me>
In reply to#81804
On 03/10/2021 12:19, Bonita Montero wrote:
> Am 03.10.2021 um 12:15 schrieb Bart:
> 
>> Jesus. And I think this still doesn't do an actual long double!
> 
> long double isn't supported by many compilers for x86-64.
> long double should be avoided when possible because loads
> and stores are slow with long double.
> 

"long double" is supported by any C++ or C compiler, for any target,
that tries to come close to any current standards compliance.

What you probably mean is that on some targets, "long double" is the
same size as "double".  That's true for most 32-bit (and smaller)
targets.  Most 64-bit targets support larger "long double".

As happens so often, there is /one/ major exception to the common
practices used by (AFAICS) every other OS, every other processor, every
other compiler manufacturer - on Windows, and with MSVC, "long double"
is 64-bit.

"long double" should not be used where "double" will do, because it can
be a great deal slower on many platforms (the load and saves are
irrelevant).  You also have to question whether "long double" does what
you want, on any given target.  On smaller targets (or more limited
compilers, like MSVC), it gives you no benefits in accuracy or range
compared to "double".  On some, such as x86-64 with decent tools, it
generally gives you 80-bit types.  On others - almost any other 64-bit
system - it gives you 128-bit quad double, but it is likely to be
implemented in software rather than hardware.

However, it /is/ a valid type on all (reasonable) C and C++ systems, and
it is a perfectly reasonable question to ask how to handle it.  Some
aspects can be handled in a portable manner, others require
implementation-dependent details.

[toc] | [prev] | [next] | [standalone]


#81825

FromJuha Nieminen <nospam@thanks.invalid>
Date2021-10-04 09:59 +0000
Message-ID<sjej9h$ic9$1@gioia.aioe.org>
In reply to#81823
David Brown <david.brown@hesbynett.no> wrote:
> What you probably mean is that on some targets, "long double" is the
> same size as "double".  That's true for most 32-bit (and smaller)
> targets.  Most 64-bit targets support larger "long double".

That might perhaps be true for non-x86 32-bit targets. However, in x86
architectures long double is most typically 80-bit, and has been so
for pretty much as long as the x86 (and its x87 math coprocessor)
has existed, ie. all the way from the 8086 (which was a 16-bit
processor), especially if you had the 8087 FPU coprocessor.

The reason why long double is 80-bit in x86 architectures is that
that's the native floating point format used by the x87 FPU.
(While you can load 32-bit floats and 64-bit doubles, internally
the FPU uses 80-bit floating point. And you can load and store
full 80-bit floating point values, of course.)

However, what has happened since then is the introduction of SSE
(and later AVX), which is much simpler to use and more efficient
than the FPU, and is a direct replacement of the FPU (well, with
the exception of trigonometric functions, which SSE/AVX for some
reason do not support natively). However, SSE/AVX only supports
64-bit floating point, not 80-bit, which is why 80-bit floating
point has been "soft-deprecated" for like two decades now.

You can still use 80-bit long doubles with compilers like gcc
and clang, even when targeting the latest x86-64 CPUs. The compilers
will generate FPU opcodes to handle them (and, most often than not,
they will be less efficient. Also, if I understand correctly, using
the FPU and SSE at the same time is not very efficient because they
share logic circuitry and interfere with each other. In other words,
the SSE unit needs to wait for the FPU logic to do its thing, which
wastes clock cycles. I might be wrong in this, though.)

80-bit long doubles are also considered justifiably deprecated
because they don't really all that much precision. Sure, they
have an 11 bits larger mantissa, and a slightly bigger range,
but all in all that's not a huge advantage in most calculations.
If you calculations are hitting the precision limits of double,
chances are that they are going to hit the precision limits of
long double as well. There's a relatively small range where
long double beats double in practical use.

[toc] | [prev] | [next] | [standalone]


#81826

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-04 13:50 +0200
Message-ID<sjeppn$jlc$1@dont-email.me>
In reply to#81823
Am 04.10.2021 um 09:52 schrieb David Brown:

> "long double" is supported by any C++ or C compiler, for any target,
> that tries to come close to any current standards compliance.

Most compilers map long double to IEEE-754 double precision and not
extended precision. Even with Intel C++ you must supply a compiler
-switch that you have double as a extended precision.

> What you probably mean is that on some targets, "long double" is the
> same size as "double".  That's true for most 32-bit (and smaller)
> targets.  Most 64-bit targets support larger "long double".

No, most don't.

[toc] | [prev] | [next] | [standalone]


#81843

FromDavid Brown <david.brown@hesbynett.no>
Date2021-10-04 20:04 +0200
Message-ID<sjffn2$7dq$1@dont-email.me>
In reply to#81826
On 04/10/2021 13:50, Bonita Montero wrote:
> Am 04.10.2021 um 09:52 schrieb David Brown:
> 
>> "long double" is supported by any C++ or C compiler, for any target,
>> that tries to come close to any current standards compliance.
> 
> Most compilers map long double to IEEE-754 double precision and not
> extended precision. Even with Intel C++ you must supply a compiler
> -switch that you have double as a extended precision.

No, you don't.

> 
>> What you probably mean is that on some targets, "long double" is the
>> same size as "double".  That's true for most 32-bit (and smaller)
>> targets.  Most 64-bit targets support larger "long double".
> 
> No, most don't.
> 

Yes, they do.

Again, you are confusing "Windows" with "everything".

MS, for reasons known only to them, seem to have decided that "long
double" should be 64-bit in the Windows ABI (noting that there never was
a real ABI for 32-bit Windows).  So Intel's compiler /on windows/ will
use 64-bit "long double" by default.  It uses 80-bit "long double" on
other x86-64 targets (they are actually 16 bytes in size, for alignment
purposes, but 80 bits of data).

MSVC for ARM has 64-bit "long doubles" even on 64-bit ARM, other 64-bit
ARM targets have 128-bit (I don't know off-hand if they are IEEE quad
precision).  For RISC-V, even 32-bit targets have 128-bit "long double".

[toc] | [prev] | [next] | [standalone]


#81850

FromBonita Montero <Bonita.Montero@gmail.com>
Date2021-10-05 07:12 +0200
Message-ID<sjgmsn$4nr$2@dont-email.me>
In reply to#81843
Am 04.10.2021 um 20:04 schrieb David Brown:

> MSVC for ARM has 64-bit "long doubles" even on 64-bit ARM, other 64-bit
> ARM targets have 128-bit (I don't know off-hand if they are IEEE quad
> precision).  For RISC-V, even 32-bit targets have 128-bit "long double".

There isn't any ARM-implementation with 128 bit FP.

[toc] | [prev] | [next] | [standalone]


#81853

FromDavid Brown <david.brown@hesbynett.no>
Date2021-10-05 09:47 +0200
Message-ID<sjgvvg$pnt$1@dont-email.me>
In reply to#81850
On 05/10/2021 07:12, Bonita Montero wrote:
> Am 04.10.2021 um 20:04 schrieb David Brown:
> 
>> MSVC for ARM has 64-bit "long doubles" even on 64-bit ARM, other 64-bit
>> ARM targets have 128-bit (I don't know off-hand if they are IEEE quad
>> precision).  For RISC-V, even 32-bit targets have 128-bit "long double".
> 
> There isn't any ARM-implementation with 128 bit FP.
> 

And you know that because ... what?  Because you are confusing hardware
floating point with floating point in general?

There are very few /hardware/ implementations of quad precision floating
point - I think perhaps Power is the only architecture that actually has
it in practice.  (Some architectures, such as SPARC and RISC-V, have
defined them in the architecture but have no physical devices supporting
them.)

But /software/ implementations of quad precision floating point are not
hard to find.  And a lot of toolchains support them - just as lots of
toolchains have software support for other kinds of floating point or
integer arithmetic that are part of C or C++, but do not have hardware
implementations on a given target.

And yes, we all know that software floating point is usually a lot
slower than hardware.  People don't use 128-bit floating point for speed
- they use it because they need the range or precision, and speed is a
secondary concern.

[toc] | [prev] | [next] | [standalone]


Page 1 of 6  [1] 2 3 4 5 6  Next page →

Back to top | Article view | comp.lang.c++


csiph-web