Jump to content
Gord P

Debugger gives wrong value for long double

Recommended Posts

Using C++Builder 11.3, 32 bits, VCL.  Problem is same for classic or clang compiler.

 

Some simple code shows the issue:

void __fastcall TForm1::FormShow(TObject *Sender)
{
    long double ld1, ld2, ld3;
    _TCHAR bufStrg[128];

    ld1 = 1.0;
    ld2 = 7.0;
    ld3 = ld1 / ld2;

    swprintf(bufStrg, L"Long Double = %.20LG", ld3);  // better than FormatFloat for displaying long doubles
    Label1->Caption = String(bufStrg);
}

The true value of 1/7 is 0.142857142857142857..., where 142857 repeats itself forever.

 

If you set a break somewhere after ld3 is calculated, and look at either the Inspect tool or the Watch window, it shows the value of ld3 as 0.142857142857142864.

image.png.11b3e8433985d725006a5a660b309768.png

 

But if you let the program run, it displays the value on the form as

image.png.6d938d2f2023a4feee44206aeb08c613.png

Which is as good as you will get for a long double.

 

After putting the variable through a few calculations and inspecting the results, I concluded that the value displayed on the form is indeed what the variable is holding and not what the debugger says it is holding.

 

I will put this into QP unless shows me something that I am doing wrong here.

 

I noticed this issue a fair number of years ago but didn't pursue it because I didn't need to use long doubles.  Now I want to use them and was surprised this issue still persists in a much later version.  I would have thought there would be lots of people using long doubles that would not be happy about this.  There was report on QP about this for the 64 bit compiler but it is closed and says it was fixed in v 10.1.

 

image.png

Share this post


Link to post
Posted (edited)

I fooled around a little bit with TExtended80Rec and found that the value held in the double (ld3) equates to: 

0.14285714285714285712736854039306422........

I have bolded the part before it goes off the rails.  So sprintf() presents it well.  I don't have Delphi to see if their corresponding function is part of the problem (I think they write Rad Studio in Delphi but I could be wrong about that).

 

Anyway after having said and done all that I think the bigger issue for using long doubles is the fact that in Win64 they are not supported.  To be more accurate, they are just the same as doubles (8 bytes).  Kind of pointless. At some point I will be migrating over to 64 bits so I would lose the precision then.  

Edited by Gord P

Share this post


Link to post
On 1/2/2024 at 8:24 PM, Gord P said:

The true value of 1/7 is 0.142857142857142857..., where 142857 repeats itself forever.

Not in IEEE 754 floating point data types that you are using.

 

Anyway I'm not actually sure what your actual problem is. Do you feel that there is an issue with the debugger? 

 

As for 80 bit floats I don't see any future for them. That idea died and everyone uses 64 bit double now. Is double insufficient for your needs? 

Share this post


Link to post
22 hours ago, David Heffernan said:

Not in IEEE 754 floating point data types that you are using.

The number I quoted is the actual (true) value for 1/7.  But yes, in a binary system you don't get that.  Which is why I used TExtended80Rec to see what the value in the long double variable equated to.

 

22 hours ago, David Heffernan said:

Anyway I'm not actually sure what your actual problem is. Do you feel that there is an issue with the debugger?

The debugger does not display the value that the variable actually contains to the precision it should be able to (and could if it used something akin to sprintf()) - as per my first post.

 

22 hours ago, David Heffernan said:

As for 80 bit floats I don't see any future for them. That idea died and everyone uses 64 bit double now.

Yes, that is unfortunate and is what I was referring to in my second post.

 

22 hours ago, David Heffernan said:

Is double insufficient for your needs? 

That is a good question, and one I was trying to sort out.  It is not a matter so much of whether it is insufficient, but whether things could be improved with additional precision.  I have been using doubles forever and it has been fine. But there are cases where there is numerical instability and I wonder if more precision would help.  But, there are always ways to get around it and because of the previous point, those will have to suffice.

Share this post


Link to post

Those libraries will be appallingly slow. In terms of the calculation side of it, it doesn't sound like you have a problem. Use double. 

 

As far as the IDE goes its not surprising that a program written using the Delphi rtl doesn't convert floats to decimal text correctly because the rtl doesn't. Or vice versa. Never has done and in spite of Emba being told more than a decade ago they haven't shown any inclination to address it. 

 

In my code I use dtoa and dragon4 to do these things so I can get correct values. 

Share this post


Link to post
15 hours ago, David Heffernan said:

In my code I use dtoa and dragon4 to do these things so I can get correct values. 

What is "dragon4"?

 

 

Share this post


Link to post
1 hour ago, Mark- said:

What is "dragon4"?

I suppose is about the algorithm (google for "Dragon4 algorithms").

Share this post


Link to post
1 hour ago, Cristian Peța said:

I suppose is about the algorithm (google for "Dragon4 algorithms").

Thanks

Share this post


Link to post
19 hours ago, David Heffernan said:

Those libraries will be appallingly slow.

I suspected that.  That was nice about using the 80 bit doubles.  From what I understood, there wasn't a huge performance hit.  I was about test all that out, but now I think I will stick with the doubles for now.

Share this post


Link to post
5 hours ago, Gord P said:

I suspected that.  That was nice about using the 80 bit doubles.  From what I understood, there wasn't a huge performance hit.  I was about test all that out, but now I think I will stick with the doubles for now.

Massive performance hit compared to double on 64 bit

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×