473,396 Members | 2,061 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,396 software developers and data experts.

Typecast long double->double seems to go wrong

Hi there,
actually, I have posted the same question in g.g.help.
As there were no answers, I am still not sure whether this
is a bug or only something open to the compiler that is
seemingly inconsistent or whether my understanding of C
is not complete enough.
I would appreciate answers or pointers to answers very much.
Cheers,
Michael

---------original post to g.g.help-----------------------

for a C course I gave the students the task to compute
FLT_EPSILON, DBL_EPSILON and LDBL_EPSILON.
We are using gcc 3.2 and gcc 3.4 on Linux machines.

The following

epsilon_float = 1.0F;
while (1.0 + eps_float != 1.0)
eps_float /= 2.0;

eps_float *= 2.0;

will give the students not FLT_EPSILON, but LDBL_EPSILON
in float precision, as the expressions in the condition for
while are evaluated in long double precision.
Now we apply the necessary type casts

epsilon_float = 1.0F;
while ((float)(1.0 + eps_float) != (float)1.0)
eps_float /= 2.0;

eps_float *= 2.0;

and everything works fine.
Right. Up to now, everything is fine and completely to my
understanding. But if I try the same with doubles, I get
LDBL_EPSILON even with the cast. (Example provided below)

Is this a problem in gcc or am I missing some finer points
of C? I tried RTFM and STFW but probably looked in the wrong
places.
It would be great if someone could tell me what is going
wrong or point me in the right direction.
Cheers,
Michael
gcc -Wall -std=c99 -pedantic castepsilon.c
--------------------------castepsilon.c-------------------
#include <stdio.h>
#include <float.h>
int main (void)
{
float eps_float = 1.0F;
double eps_double_test = 1.0, eps_double = 1.0;
long double eps_long_double = 1.0L;

// find FlT_EPSILON
while ((float)(1.0 + eps_float) != (float)1.0)
eps_float /= 2.0;

eps_float *= 2.0;

// find DBL_EPSILON
while ((double)(1.0 + eps_double_test) != (double)1.0)
eps_double_test /= 2.0;

eps_double_test *= 2.0;

// alternative way
for ( double test=1.0 + eps_double; test != (double)1.0;
test=1.0 + eps_double)
eps_double /= 2.0;

eps_double *= 2.0;

// find LDBL_EPSILON
while ((long double)(1.0 + eps_long_double) != (long double)1.0)
eps_long_double /= 2.0;

eps_long_double *= 2.0;

// Output
printf("Epsilon is --");
printf(" exact val\n");
printf(" - for float : %8.7g -- %8.7g\n",
eps_float, FLT_EPSILON);
printf(" - for double (1) : %17.16g -- %17.16g\n",
eps_double_test, DBL_EPSILON);
printf(" - for double (2) : %17.16g -- %17.16g\n",
eps_double, DBL_EPSILON);
printf(" - for long double: %20.19Lg -- %20.19Lg\n\n",
eps_long_double, LDBL_EPSILON);

return(0);
}
-------------------------------------------------------------------
mairml@cip20:~/C/test> ./a.out
Epsilon is -- exact value --
- for float : 1.192093e-07 -- 1.192093e-07
- for double (1) : 1.084202172485504e-19 -- 2.220446049250313e-16
- for double (2) : 2.220446049250313e-16 -- 2.220446049250313e-16
- for long double: 1.084202172485504434e-19 -- 1.084202172485504434e-19

Nov 14 '05 #1
4 4154

"Michael Mair" <ma********************@ians.uni-stuttgart.de> wrote in
message news:c8**********@news.uni-stuttgart.de...
for a C course I gave the students the task to compute
FLT_EPSILON, DBL_EPSILON and LDBL_EPSILON.
We are using gcc 3.2 and gcc 3.4 on Linux machines.

The following

epsilon_float = 1.0F;
while (1.0 + eps_float != 1.0)
eps_float /= 2.0;

eps_float *= 2.0;

will give the students not FLT_EPSILON, but LDBL_EPSILON
in float precision, as the expressions in the condition for
while are evaluated in long double precision.
Now we apply the necessary type casts

epsilon_float = 1.0F;
while ((float)(1.0 + eps_float) != (float)1.0)
eps_float /= 2.0;

eps_float *= 2.0;

and everything works fine.
Right. Up to now, everything is fine and completely to my
understanding. But if I try the same with doubles, I get
LDBL_EPSILON even with the cast. (Example provided below)

Is this a problem in gcc or am I missing some finer points
of C? I tried RTFM and STFW but probably looked in the wrong
places.
It would be great if someone could tell me what is going
wrong or point me in the right direction.
Cheers,
Michael
gcc -Wall -std=c99 -pedantic castepsilon.c
--------------------------castepsilon.c-------------------
#include <stdio.h>
#include <float.h>
int main (void)
{
float eps_float = 1.0F;
double eps_double_test = 1.0, eps_double = 1.0;
long double eps_long_double = 1.0L;

// find FlT_EPSILON
while ((float)(1.0 + eps_float) != (float)1.0)
eps_float /= 2.0;

eps_float *= 2.0;

// find DBL_EPSILON
while ((double)(1.0 + eps_double_test) != (double)1.0)
eps_double_test /= 2.0;

eps_double_test *= 2.0;

// alternative way
for ( double test=1.0 + eps_double; test != (double)1.0;
test=1.0 + eps_double)
eps_double /= 2.0;

eps_double *= 2.0;

// find LDBL_EPSILON
while ((long double)(1.0 + eps_long_double) != (long double)1.0)
eps_long_double /= 2.0;

eps_long_double *= 2.0;

// Output
printf("Epsilon is --");
printf(" exact val\n");
printf(" - for float : %8.7g -- %8.7g\n",
eps_float, FLT_EPSILON);
printf(" - for double (1) : %17.16g -- %17.16g\n",
eps_double_test, DBL_EPSILON);
printf(" - for double (2) : %17.16g -- %17.16g\n",
eps_double, DBL_EPSILON);
printf(" - for long double: %20.19Lg -- %20.19Lg\n\n",
eps_long_double, LDBL_EPSILON);

return(0);
}
-------------------------------------------------------------------
mairml@cip20:~/C/test> ./a.out
Epsilon is -- exact value --
- for float : 1.192093e-07 -- 1.192093e-07
- for double (1) : 1.084202172485504e-19 -- 2.220446049250313e-16
- for double (2) : 2.220446049250313e-16 -- 2.220446049250313e-16
- for long double: 1.084202172485504434e-19 -- 1.084202172485504434e-19


You might check how the enquire.c program performs these tests, in order to
be less dependent on the interpretations of individual compilers, and
distinguish between precision of evaluation and precision of declared type.
I find that gcc-3.3.4 on Windows gives the results you quote for your
sample. If I add -march=pentium4 -mfpmath=sse, with or without -O, all
double casts do narrow the precision, but if I add -O without sse
or -ffloat-store, neither is narrowed. I think the only advice you will
find in the gcc references is to use either the sse or -ffloat-store
options.
I don't know of any CPU still in production which does not support fixed
53-bit precision, like SSE2, so the fact that gcc might be lacking in
support for such a feature on certain older CPUs is nearly moot. There may
have been optimization reasons for gcc behaving as it did on certain CPUs;
many programmers meant "at least double" when they cast to double, and
specific language features to support that never became widely accepted.
I grant that I might normally avoid the sse option on Pentium-M, but even on
that CPU it is the more efficient method to accomplish what you ask.
The gcc options which produce the behavior to which you object are quite
unlikely to be used on the x86-64 OS, and are contrary to the Windows-64
ABI, so I don't see anyone changing gcc at this late date.
Nov 14 '05 #2
Michael Mair <ma********************@ians.uni-stuttgart.de> writes:
for a C course I gave the students the task to compute
FLT_EPSILON, DBL_EPSILON and LDBL_EPSILON.

[...]
Now we apply the necessary type casts

epsilon_float = 1.0F;
while ((float)(1.0 + eps_float) != (float)1.0)
eps_float /= 2.0;

eps_float *= 2.0;

and everything works fine.
Right. Up to now, everything is fine and completely to my
understanding. But if I try the same with doubles, I get
LDBL_EPSILON even with the cast. (Example provided below)


See <cu*************@zero-based.org>.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3

"Tim Prince" <tp*****@computer.org> wrote in message
news:Vl*******************@newssvr25.news.prodigy. com...
I think the only advice you will
find in the gcc references is to use either the sse or -ffloat-store
options.

For older CPU's, you could set the x87 precision mode to 53 bits. Last I
looked, that was the procedure described for SPEC benchmarks on Athlon, on
the SuSE site.
Nov 14 '05 #4
Hi Tim,
thank you for your extensive response!
So, if I may sum it up as I understood it: The inconsistency is a
"problem" which most of the time is a feature as the compiler uses
the available precision for computations and comparisons on variables
kept in the registers. As double is the "natural" floating point
data type, it is in some sense also "natural" to use the "natural"
floating point precision of the FPU or whatever, neglecting the
expected values from the IEEE standard.

The gcc option -ffloat-store (which I managed to not find in the man
page even though it's there) switches off this "unwanted" behaviour,
discarding the excess precision.

The whole thing does not apply to floats as they already are heavily
restricted when compared agains the abilities of modern
cpus/fpus/whatever, thus the cast works as intended.

So, the only "inconsistency" is the fact that the symbolic constant
DBL_EPSILON refers to the IEEE double instead of the actual double
data type when performing arithmetic operations and comparisons.
However, as soon as we get out of the register or have an explicit
assignment of the value to a double variable (or put it on the stack
as in the message Martin refered to), we automatically lose the excess
precision as we go back to 64 bits, so DBL_EPSILON is consistent in
this case and respect.

I hope that was not to convoluted...
Thank you for helping me understand :-)
Best regards,
Michael

Nov 14 '05 #5

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

Similar topics

5
by: ferran | last post by:
Hi, I'm trying to convert a string to a long double using streams but for some reasing seems to give the wrong values, the idea is to do a precise textual conversion without roundings or...
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...
3
by: RoSsIaCrIiLoIA | last post by:
I have rewrote the malloc() function of K&R2 chapter 8.7 typedef long Align; ^^^^ Here, should I write 'long', 'double' or 'long double'? I know that in my pc+compiler sizeof(long)=4,...
1
by: nsj | last post by:
How to typecast double into ascii or string? Please help me.
69
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...
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: ...
67
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...
52
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I...
15
by: broli | last post by:
Is this a part of C 99 or just an addon in some windows compilers like pellesC ?
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.