473,839 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use long double floating?

The guideline says to use %f in printf() function using the keyword
float and double.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can you
please provide the information which it support long double, please?
Thanks...

Bryan Parkoff
Jan 1 '06 #1
10 18783
Bryan Parkoff wrote:
The guideline says to use %f in printf() function using the keyword
float and double.
Use %f in printf family of functions to output a value of type 'float'
or 'double'. The reason the same specifier works for both is that any
value of type 'float' is promoted to 'double' anyway.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can
you please provide the information which it support long double,
please? Thanks...

The Standard specifies that you should use 'L' width prefix (like

%.2Lf

) to output a 'long double' value.

V
Jan 1 '06 #2

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:Qr******** ************@co mcast.com...
Bryan Parkoff wrote:
The guideline says to use %f in printf() function using the keyword
float and double.


Use %f in printf family of functions to output a value of type 'float'
or 'double'. The reason the same specifier works for both is that any
value of type 'float' is promoted to 'double' anyway.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can
you please provide the information which it support long double,
please? Thanks...

The Standard specifies that you should use 'L' width prefix (like

%.2Lf

) to output a 'long double' value.

V

Victor,

Thank you for providing me the answer, but this decimal has 6 digits
after ".". It needs to be 10 digits to 20 digits for best accuracy decimal
numbers. How is it possible that %Lf help? %Lf still provides 6 digits.
How do I overcome the limit?

Bryan Parkoff
Jan 1 '06 #3

"Bryan Parkoff" <no**@nospam.ne t> wrote in message
news:Xh******** ***********@new ssvr21.news.pro digy.com...

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:Qr******** ************@co mcast.com...
Bryan Parkoff wrote:
The guideline says to use %f in printf() function using the keyword
float and double.


Use %f in printf family of functions to output a value of type 'float'
or 'double'. The reason the same specifier works for both is that any
value of type 'float' is promoted to 'double' anyway.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can
you please provide the information which it support long double,
please? Thanks...

The Standard specifies that you should use 'L' width prefix (like

%.2Lf

) to output a 'long double' value.

V

Victor,

Thank you for providing me the answer, but this decimal has 6 digits
after ".". It needs to be 10 digits to 20 digits for best accuracy
decimal numbers. How is it possible that %Lf help? %Lf still provides 6
digits. How do I overcome the limit?


printf("%.20Lf\ n", b);

Where's your textbook?

-Mike
Jan 1 '06 #4
On Sun, 01 Jan 2006 00:38:49 GMT, "Bryan Parkoff" <no**@nospam.ne t>
wrote in comp.lang.c++:
The guideline says to use %f in printf() function using the keyword
float and double.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can you
please provide the information which it support long double, please?
Thanks...

Bryan Parkoff


Victor and Mike have answered the questions you actually asked, but
note that if you are using Microsoft's compiler, long double does not
give you any gain over double.

Microsoft's 16-bit C and C++ compilers used to use the full 80-bit
extended precision format of the Intel FPU for long double, but when
they built their first 32-bit compiler for Windows NT, the decided to
use the 64-bit real format for long double as well as double.

The reason? "Under Windows NT, in order to be compatible with other
non-Intel floating point implementations ...". Remember when Microsoft
was going to conquer every 32-bit processor in the world?

They decided to take away the programmer's choice to decide whether it
was more important for the application to have the maximum accuracy
the hardware could provide, or to have compatibility with all the MIPS
and ARM desktops machines running Windows NT.

A triumph of marketing over sound engineering, if ever there was one.

Read their own words on the subject:

http://support.microsoft.com/default...b;en-us;129209

By the way, their are other compilers for Windows that do not do this,
including Borland and gcc (but a real version of gcc, not Mingw).

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 1 '06 #5

Jack Klein wrote:
On Sun, 01 Jan 2006 00:38:49 GMT, "Bryan Parkoff" <no**@nospam.ne t>
wrote in comp.lang.c++:
The guideline says to use %f in printf() function using the keyword
float and double.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can you
please provide the information which it support long double, please?
Thanks...

Bryan Parkoff
Victor and Mike have answered the questions you actually asked, but
note that if you are using Microsoft's compiler, long double does not
give you any gain over double.

Microsoft's 16-bit C and C++ compilers used to use the full 80-bit
extended precision format of the Intel FPU for long double, but when
they built their first 32-bit compiler for Windows NT, the decided to
use the 64-bit real format for long double as well as double.

The reason? "Under Windows NT, in order to be compatible with other
non-Intel floating point implementations ...". Remember when Microsoft
was going to conquer every 32-bit processor in the world?

They decided to take away the programmer's choice to decide whether it
was more important for the application to have the maximum accuracy
the hardware could provide, or to have compatibility with all the MIPS
and ARM desktops machines running Windows NT.


Hardly. Microsoft decided to offer the customer a choice of hardware
from competing vendors in order to run Windows NT. Microsoft
even-handedly made sure that their compiler adhered to industry
standard floating point format (IEEE) rather than show blatant
favoritism toward a dominant supplier by catering to its proprietary
chip.

Microsoft didn't "take away" anything, since the customer is not
obligated to use their compiler, and Intel's floating point chip is
still capable of 80-bit precision. Nor does either the C or C++
standard require any specific level of precision.

Incidentally, Windows NT never ran on an ARM processor. It did run and
continues to run on the PowerPC (to power the XBox 360).
A triumph of marketing over sound engineering, if ever there was one.
Consistent, verifiable floating point results across platforms is
better engineering than apparently more precise results reproducible on
a single platform - especially when the accuracy of those results is
open to doubt (as the Pentium division bug superbly demonstrated).
Read their own words on the subject:

http://support.microsoft.com/default...b;en-us;129209

By the way, their are other compilers for Windows that do not do this,
including Borland and gcc (but a real version of gcc, not Mingw).


Of course, hardcore Intel fans can always use Intel's C/C++ compiler -
which apparently costs the company nothing to write. Intel does give it
away for free. But anyone hoping for a free PowerPC compiler from Intel
is probably out of luck.

"Sound engineering" does not necessarily equate to doing right by the
customer. In this case, Microsoft did do the right thing.

Greg

Jan 1 '06 #6
On 1 Jan 2006 01:23:18 -0800, "Greg" <gr****@pacbell .net> wrote in
comp.lang.c++:

Jack Klein wrote:
On Sun, 01 Jan 2006 00:38:49 GMT, "Bryan Parkoff" <no**@nospam.ne t>
wrote in comp.lang.c++:
The guideline says to use %f in printf() function using the keyword
float and double.

For example

float a = 1.2345;
double b = 5.166666667;

printf("%.2f\n %f\n", a, b);

Now, I want to use long double rather than double to get accuracy
decimal number which it is larger than normal number such as billion
numbers. I assume that it would be %dd, but it is not correct. Can you
please provide the information which it support long double, please?
Thanks...

Bryan Parkoff
Victor and Mike have answered the questions you actually asked, but
note that if you are using Microsoft's compiler, long double does not
give you any gain over double.

Microsoft's 16-bit C and C++ compilers used to use the full 80-bit
extended precision format of the Intel FPU for long double, but when
they built their first 32-bit compiler for Windows NT, the decided to
use the 64-bit real format for long double as well as double.

The reason? "Under Windows NT, in order to be compatible with other
non-Intel floating point implementations ...". Remember when Microsoft
was going to conquer every 32-bit processor in the world?

They decided to take away the programmer's choice to decide whether it
was more important for the application to have the maximum accuracy
the hardware could provide, or to have compatibility with all the MIPS
and ARM desktops machines running Windows NT.
Of course this is off-topic here, but it happens to be a pet peeve of
mine. And since a substantial fraction of comp.lang.c++ uses
Microsoft's Visual C++ compiler, they may be affected by this issue.
Hardly. Microsoft decided to offer the customer a choice of hardware
from competing vendors in order to run Windows NT. Microsoft
even-handedly made sure that their compiler adhered to industry
standard floating point format (IEEE) rather than show blatant
favoritism toward a dominant supplier by catering to its proprietary
chip.
That's quite nice, but the IEEE defines an "IEEE 754 Double-Extended"
format that is just as much part of the standard as the 32-bit
"Single" and 64-bit "Double" floating point formats. Now it just so
happens that the IEEE 754 standard was based largely on Intel's work
in developing the x86 math coprocessor and later FPU, but that's
immaterial. The 80-bit "Double-Extended" format is fully a part of
the IEEE 754 standard, even though its implementation is optional.

As for compatibility, everyone expects the C/C++ float and double
types to use the IEEE 754 formats, and Microsoft had to do nothing at
all to keep their 32-bit compilers compatible with user's
expectations, as their 16-bit compilers had been.
Microsoft didn't "take away" anything, since the customer is not
That is not true at all. I had C programs that delivered accurate
results in Microsoft C compilers from MS-DOS version 5.x through
Visual C++ 1.52, with or without floating point hardware. When
recompiled with Visual C++ 4.0, the first 32-bit version I used, they
delivered different and inaccurate results.

From my point of view, they certainly "took away" accuracy and
precision from the long double data type, features that had existed in
their 16-bit predecessors, and that were still right their in the
processor hardware.
obligated to use their compiler, and Intel's floating point chip is
They have at most times in the past made every effort to ensure that
the maximum possible percentage of x86 Windows programmers used their
compiler. Or so it appears to me, you may disagree. Can you provide
an URL to a link on their web site mentioning other compilers as a
possibility to those who want the 80 bit format back?
still capable of 80-bit precision. Nor does either the C or C++
standard require any specific level of precision.
I am well aware of the fact that neither language requires long double
to have any more precision and range than double. I am also well
aware of the fact that the IEEE "Double" format exceeds the minimum
requirements of C and C++ by a considerable margin.
Incidentally, Windows NT never ran on an ARM processor. It did run and
continues to run on the PowerPC (to power the XBox 360).
Is there any other non x86 architecture running a version of Windows
other then CE these days, or anyone other than Microsoft running it on
a PowerPC?
A triumph of marketing over sound engineering, if ever there was one.


Consistent, verifiable floating point results across platforms is
better engineering than apparently more precise results reproducible on
a single platform - especially when the accuracy of those results is
open to doubt (as the Pentium division bug superbly demonstrated).


This is utter nonsense. They deliberately deprived the programmer of
the ability to make the choice between maximum accuracy permitted by
the hardware and maximum comparability. Some who use the long double
type know exactly why they use it and what they expect from it.
Generally, those who don't know what they are doing generally just use
float or double.
Read their own words on the subject:

http://support.microsoft.com/default...b;en-us;129209

By the way, their are other compilers for Windows that do not do this,
including Borland and gcc (but a real version of gcc, not Mingw).


Of course, hardcore Intel fans can always use Intel's C/C++ compiler -
which apparently costs the company nothing to write. Intel does give it
away for free. But anyone hoping for a free PowerPC compiler from Intel
is probably out of luck.


How does this square with Microsoft's cramming support for MMX, SIMD,
SIMD2, etc., into Visual C++, when these features aren't available on
"all" those other architectures running NT and its descendents these
days?
"Sound engineering" does not necessarily equate to doing right by the
customer. In this case, Microsoft did do the right thing.
"Sound engineering" means leaving the engineering decisions up to the
engineer dealing with the requirements of the particular situation, or
application in this case.

"Sound engineering" does not mean denying the engineer/programmer the
opportunity to make a decision by deciding for him/her that accuracy
and precision were not as important as Microsoft's idea of
portability. The result is that Microsoft Visual C++ is literally
unsuitable for certain types of serious scientific and engineering
applications.

They could quite easily have documented that long double was not
portable across Windows implementations , whereas float and double
were.
Greg


There are numerous discussions available online about the harmful
effects of denying the use of the full precision available. For one
such, see http://www.cs.berkeley.edu/~wkahan/i...us/IEEE754.PDF

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 2 '06 #7

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:9m******** *************** *********@4ax.c om...
There are numerous discussions available online about the harmful
effects of denying the use of the full precision available. For one
such, see http://www.cs.berkeley.edu/~wkahan/i...us/IEEE754.PDF


I've never understood why 80 bit long doubles are not universally supported
by compilers. Digital Mars C, C++ and D programming language compilers
certainly support it.

Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Jan 2 '06 #8
On Sun, 1 Jan 2006 18:49:29 -0800, "Walter Bright"
<wa****@nospa mm-digitalmars.com > wrote in comp.lang.c++:

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:9m******** *************** *********@4ax.c om...
There are numerous discussions available online about the harmful
effects of denying the use of the full precision available. For one
such, see http://www.cs.berkeley.edu/~wkahan/i...us/IEEE754.PDF


I've never understood why 80 bit long doubles are not universally supported
by compilers. Digital Mars C, C++ and D programming language compilers
certainly support it.


I have never understood it, either.

As I said before, the reason for Microsoft's decision is stated on
their web page
http://support.microsoft.com/default...b;en-us;129209 where
they disclose a hack using assembly language to read back 80-bit long
double values from binary files written by programs using their
earlier 16-bit compilers.

This also applies to compilers like Mingw, which is rather like a true
gcc port in that it provides no libraries of its own, but uses the
Microsoft C library installed on the OS. Naturally it must limit its
long doubles to 64 bits as well, or it would break on calls to the
library.

Fortunately most other compilers do not shackle the programmer the
same way, and I commend your decision to do the right thing with the
Digital Mars tools.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 2 '06 #9
Walter Bright wrote:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:9m******** *************** *********@4ax.c om...
There are numerous discussions available online about the harmful
effects of denying the use of the full precision available. For one
such, see http://www.cs.berkeley.edu/~wkahan/i...us/IEEE754.PDF


I've never understood why 80 bit long doubles are not universally supported
by compilers. Digital Mars C, C++ and D programming language compilers
certainly support it.


Because 80-bit long doubles are not universally supported in hardware.
If the floating point registers of the target microprocessor are
64-bit, it doesn't make a lot of sense to support 80-bit long doubles
in software. Clearly, it would be much more efficient and much more
precise to combine two 64-bit floating point registers to create one
128-bit floating point value- if precision in excess of 64 bits is
desired. gcc 4.0 on the PowerPC follows exactly that approach.

Greg

Jan 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1744
by: dan | last post by:
Can someone tell me the difference between these three data types? Thanks Daniel
32
22648
by: f | last post by:
I have this double sum, a, b, c; sum = a + b + c; printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c); I found that the debug version and release version of the same code give me different result. I am using VC++ 6.0. In debug version, the print out is:
12
2989
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
22
3729
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What wintel compilers support a 16-byte "long double" (with 33 decimal digits of accuracy) including proper printf() support. I found some compilers that did support "long double", but theirs was an 8-byte or 10-byte or 12-byte type with accuracy the...
15
3937
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
69
5616
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
67
9958
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
0
2563
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23 significand bits (with an implicit leading 1, for 24 total), and 8 exponent bits double precision: 64 bits including 1 sign bit, 52 significand bits
8
14184
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val = 3.14159; u_long nw_val = *((u_long *) &val); than the nw_val equal to 1078530000, I made such conversion: float d_val = *((float*)&nw_val);
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10910
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10297
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7833
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7021
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.