Solved

Displaying Long Double

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

758 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

22 Experts available now in Live!

Get 1:1 Help Now