Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Displaying Long Double

Posted on 1997-05-01
3
Medium Priority
?
228 Views
Last Modified: 2008-03-10
I've declared a long double variable and tried to display it but could only display in exponential format.

The problem is, C allows me to declare a long double but printf and sprintf only handles up to the size of double (16 digits).

I need to at display the full value on the screen as well as converting it to a string using sprintf. To illustrate:

/* Dummy codes */
long double buf1, buf2, result;
char ans[20];

buf1 = 12345678901234567.00;
buf2 = 12345678901234567.00;
result = buf1+buf2;
printf("result: %17.0f", result);
sprintf(ans, "%17.0f", result);

If printf and sprintf can't work please suggest other alternatives.

Note: The maximum length of the variable "result" can go up to 18 digits.

Please help, this is urgent. Thank you.
0
Comment
Question by:samtan
3 Comments
 
LVL 1

Expert Comment

by:warren_dale
ID: 1250218
(1) Long double is not ANSI C. It is an extension.
(2) On my machine I can only get 17 significant figues for long double. You may be asking for more than is available.
(3) Are you only using whole numbers? If so:
(3.1) consider looking at one of the multiple-precision-integer packages that are around.
(3.2) consider using "long long integer" which gcc (at least) supports.
Both of these enable you to use 64 bit integers, which give up to 20 decimal digits. (If you are on a Digital Alpha they are native).
0
 
LVL 4

Accepted Solution

by:
AVaulin earned 300 total points
ID: 1250219
Try to use "fcvt" function. Prototype is in "stdlib.h".
0
 
LVL 1

Expert Comment

by:prc
ID: 1250220
No, fcvt takes a double argument.

You can use (s)printf in some systems.  It's not part of the ANSI spec for (s)printf though.  In GNU library systems (including Linux), you can use the 'L' prefix (yes, upper case).  In BSD, you can use 'q'.  Actually, you can use 'q' in both, so that's the more portable (but not Portable) way of doing it.

The follow code works fine on Linux:

#include <stdio.h>

int main()
{
  long double a,b,c;
  a=12345678901234567.00;
  b=12345678901234567.00;
  c=a+b;
  printf("result: %17.0qf\n", c);  /* Or Lf */
  return 0;
}

If this is any good to you, reject the above and I'll enter an empty answer for you to accept.

Cheers

Paul
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question