Solved

Displaying Long Double

Posted on 1997-05-01
3
222 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 100 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 174
Line meaning 9 89
gdb doesn't stop on breakpoint 2 108
Resolve Dependency Issues 4 84
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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

679 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