Solved

Displaying Long Double

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now