473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

What's the deal with "long double"

Okay, I'm running the following code in GCC 4.1.2:

<stdio.h>
int main(void)
{
long double test = 4.67e-4;
printf("float: %f e-notation: %e\n", test, test);

return 0;
}
I'm expecting something like the following results:
float: 0.000467 e-notation: 4.67000e-05

but instead, I get the following results:

float: -69441527659720390616132329106879905271265162747383 26277051364061548998292338911457643245167311365849 94939249352577342742286164740704820047241658318432 03634399732116838814293139990778757446217299595246 28730068989817917060091919713159447848299377718491 87328.000000 e-notation: 4.802997e+149

What exactly is the issue here?

I'm using GCC 4.1.2 on an Intel p3, if that matters.
Jan 1 '08 #1
6 2752
In article <pa****************************@nospam.here>,
bananaguyc <ba********@nospam.herewrote:
long double test = 4.67e-4;
printf("float: %f e-notation: %e\n", test, test);
%e and %f are not correct format descriptors for long double.
Try %Le and %Lf (capital L only, not lower-case L).

You are passing in a variable that takes more storage than is
expected according to the format string, so only a part of the
variable storage is (mis-)formatted, leaving the rest of the variable
there to be (mis-)formatted by the next format descriptor.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jan 1 '08 #2
On Tue, 01 Jan 2008 00:59:33 +0000, Walter Roberson wrote:
%e and %f are not correct format descriptors for long double.
Try %Le and %Lf (capital L only, not lower-case L).

You are passing in a variable that takes more storage than is
expected according to the format string, so only a part of the
variable storage is (mis-)formatted, leaving the rest of the variable
there to be (mis-)formatted by the next format descriptor.
I see, very informative, thank you.

Jan 1 '08 #3
bananaguyc wrote:
On Tue, 01 Jan 2008 00:59:33 +0000, Walter Roberson wrote:
>%e and %f are not correct format descriptors for long double.
Try %Le and %Lf (capital L only, not lower-case L).

You are passing in a variable that takes more storage than is
expected according to the format string, so only a part of the
variable storage is (mis-)formatted, leaving the rest of the
variable there to be (mis-)formatted by the next format
descriptor.

I see, very informative, thank you.
The following links will be helpful. I recommend the bz2
compressed version of the C standard, n869_txt.bz2.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Jan 1 '08 #4
bananaguyc wrote:
Okay, I'm running the following code in GCC 4.1.2:
/* mha: You really should with the flags for gcc to check the arguments
of printf, which it will do for you. */

#include <stdio.h /* mha: fixed this line */

int main(void)
{
long double test = 4.67e-4;
#if 0
/* mha: gcc invoked with the proper flags will tell you that your
line below has two incorrect specifiers in it. */
printf("float: %f e-notation: %e\n", test, test);
#endif
/* mha: relacement for the line above */
printf("float: %Lf e-notation: %Le\n", test, test);
return 0;
}
I'm expecting something like the following results:
Actually, you have no business expecting any particular results
when you use an incorrect specifier with printf. Here is what the
above code produces on one implementation. Except for your error in
the exponent for the e-notation, it looks very close to your
expected
float: 0.000467 e-notation: 4.67000e-05
[output]
float: 0.000467 e-notation: 4.670000e-04
Jan 1 '08 #5
CBFalconer wrote, On 01/01/08 01:54:

<snip>
<http://www.eskimo.com/~scs/C-faq/top.html (C-faq)
Chuck, you really should update this to http://c-faq.com/
--
Flash Gordon
Jan 1 '08 #6
Flash Gordon wrote:
CBFalconer wrote, On 01/01/08 01:54:

<snip>
> <http://www.eskimo.com/~scs/C-faq/top.html (C-faq)

Chuck, you really should update this to http://c-faq.com/
Done.

--
Merry ChrX-Mozilla-Status: 0009 Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Jan 1 '08 #7

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

Similar topics

5
by: Piotr B. | last post by:
Hello, I use MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP. I tried to output a "long double" variable using stdio printf(). I've tried various %formats (%llf, %Lf etc.), but none of them...
22
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...
0
by: Dean N. Williams | last post by:
Dear Python and Mac Community, I have just successfully built gcc version 4.1.0 for my Mac OS X 10.4.6. gcc -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with:...
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
7
by: Jo Deni | last post by:
Folks, I'm a newbie here (and with C++). While executing a C++ program I'm getting the following message error: integer constant is too large for "long" type and here is the offending line...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.