473,396 Members | 1,816 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.

single x double precision on 32bit arch

Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:

#include <stdio.h>

int main (void)
{
double x;
float y ;

x=1;
while ( (1+x) > 1 ) x = x/2;
x = 2*x;
printf("double precision epsilon: %e\n", x);

y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);

return 0;
}

It was compiled with:
gcc -O0 epsilon.c -o epsilon

Take a look at the output on AMD64:

double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07

However on an Petium IV (32bit):

double precision epsilon: 1.084202e-19
single precision epsilon: 1.084202e-19

Other 32bit processors behave as pentium IV,

Am I missing something? Can anyone explain me what is going on?

Thanks.

Regards,
R. Biloti.

May 19 '06 #1
6 5692

"R.Biloti" <bi****@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:
[...]
Am I missing something? Can anyone explain me what is going on?


http://docs.sun.com/source/806-3568/ncg_goldberg.html

-Mike
May 19 '06 #2
R.Biloti wrote:
Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:

#include <stdio.h>

int main (void)
{
double x;
float y ;

x=1;
while ( (1+x) > 1 ) x = x/2;
x = 2*x;
printf("double precision epsilon: %e\n", x);

y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);

return 0;
}

It was compiled with:
gcc -O0 epsilon.c -o epsilon

Take a look at the output on AMD64:

double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07

However on an Petium IV (32bit):

double precision epsilon: 1.084202e-19
single precision epsilon: 1.084202e-19

Other 32bit processors behave as pentium IV,

Am I missing something? Can anyone explain me what is going on?

Thanks.

Regards,
R. Biloti.


I expect the compilers on the Pentia are using internal 80-bit
precision (which gives 64*log(2) ~ 19 significant decimal digits)
vs. the compiler for the AMD that probably uses the IEEE 64-bit
double and IEEE 32-bit single representations. These have, res-
pectively, about 16 and 7 significant decimal digits of pre-
cision.

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
May 19 '06 #3
R.Biloti wrote:
Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:

#include <stdio.h>

int main (void)
{
double x;
float y ;

x=1;
while ( (1+x) > 1 ) x = x/2;
x = 2*x;
printf("double precision epsilon: %e\n", x);

y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);

return 0;
}

It was compiled with:
gcc -O0 epsilon.c -o epsilon

Take a look at the output on AMD64:

double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07

However on an Petium IV (32bit):

double precision epsilon: 1.084202e-19
single precision epsilon: 1.084202e-19

Other 32bit processors behave as pentium IV,

Am I missing something? Can anyone explain me what is going on?


At the risk of seeming to muddy the waters rather than clear them, I
have expanded your code below. If you run your test with it, you may
find that the results of the loops with the direct comparisons like
if (1 + y > 1) { /* ... */ }
give different results from those with a delayed comparison, like
yp = 1 + y;
if (yp > 1) { /* ... */ }
Think about why this might be so.

#include <stdio.h>
#include <float.h>

int main(void)
{
double x, xp;
float y, yp;
long double z, zp;

for (y = 1; 1 + y > 1; y /= 2) ;
printf("float epsilon: %e\n"
"FLT_EPSILON = %e\n", 2 * y, FLT_EPSILON);
for (y = 1, yp = 1 + y; yp > 1; y /= 2, yp = 1 + y) ;
printf("float epsilon (using temp): %e\n\n", 2 * y);
for (x = 1; 1 + x > 1; x /= 2) ;
printf("double epsilon: %e\n"
"DBL_EPSILON = %e\n", 2 * x, DBL_EPSILON);
for (x = 1, xp = 1 + x; xp > 1; x /= 2, xp = 1 + x) ;
printf("double epsilon (using temp): %e\n\n", 2 * x);

for (z = 1; 1 + z > 1; z /= 2) ;
printf("long double epsilon: %Le\n"
"LDBL_EPSILON = %Le\n", 2 * z, LDBL_EPSILON);
for (z = 1, zp = 1 + z; zp > 1; z /= 2, zp = 1 + z) ;
printf("long double epsilon (using temp): %Le\n\n", 2 * z);

return 0;
}
May 19 '06 #4
R.Biloti <bi****@gmail.com> wrote:

float y;
y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);


The problem is that C allows intermediate results to have greater
precision (and/or range) than their type would imply. So, even though
(1+y) has type float, it can have the precision of double, long double,
or even more. The C Standard requires that a cast or assignment discard
any excess presision or range, so you could use either:

while ( (float)(1+y) > 1 ) y = y/2;

or

while ( y2 = 1 + y, y2 > 1 ) y = y/2;

Unfortunately, many otherwise excellent compilers do not conform to that
requirement. Most require at least an assignment, some require the
assignment to be a separate statement, and few go so far as to require
the target of the assignment to be declared volatile.

-Larry Jones

Whatever it is, it's driving me crazy! -- Calvin
May 19 '06 #5
R.Biloti wrote:
Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:

#include <stdio.h>

int main (void)
{
double x;
float y ;

x=1;
while ( (1+x) > 1 ) x = x/2;
x = 2*x;
printf("double precision epsilon: %e\n", x);

y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);

return 0;
}

It was compiled with:
gcc -O0 epsilon.c -o epsilon

Take a look at the output on AMD64:

double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07

However on an Petium IV (32bit):

double precision epsilon: 1.084202e-19
single precision epsilon: 1.084202e-19

Other 32bit processors behave as pentium IV,

Am I missing something? Can anyone explain me what is going on?

Thanks.

Regards,
R. Biloti.


I take it to mean
compiled and run on AMD64 system produces this result:
double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07
and
compiled and run on a P4 system produces this result:
Which AMD processor?
Can you describe in detail each system? (cpu, mobo, chipset, ram)
Was gcc the same version on each system? What version was it?
Thanks
Eric

May 19 '06 #6
Eric wrote:
R.Biloti wrote:
Hi folks

I wrote the naive program to show up the unit roundoff (machine
precision) for single and double precision:

#include <stdio.h>

int main (void)
{
double x;
float y ;

x=1;
while ( (1+x) > 1 ) x = x/2;
x = 2*x;
printf("double precision epsilon: %e\n", x);

y=1;
while ( (1+y) > 1 ) y = y/2;
y = 2*y;
printf("single precision epsilon: %e\n", y);

return 0;
}

It was compiled with:
gcc -O0 epsilon.c -o epsilon

Take a look at the output on AMD64:

double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07

However on an Petium IV (32bit):

double precision epsilon: 1.084202e-19
single precision epsilon: 1.084202e-19

Other 32bit processors behave as pentium IV,

Am I missing something? Can anyone explain me what is going on?

Thanks.

Regards,
R. Biloti.


I take it to mean
compiled and run on AMD64 system produces this result:
double precision epsilon: 2.220446e-16
single precision epsilon: 1.192093e-07
and
compiled and run on a P4 system produces this result:
Which AMD processor?
Can you describe in detail each system? (cpu, mobo, chipset, ram)
Was gcc the same version on each system? What version was it?
Thanks
Eric

Yes, clearly the question leads well outside the bounds of topicality.
But it seems unlikely these different results could be produced by the
same compiler with the same options, unless there is a difference in the
fpu mask settings, such as there is between 32- and 64-bit Windows, or
between most linux and some BSD OS.
May 20 '06 #7

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

Similar topics

0
by: Knackeback | last post by:
I use data type double at 32Bit platform and from float.h I learn: /* Number of decimal digits of precision in a double */ #undef DBL_DIG #define DBL_DIG 15 My questions: Are numbers like...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
2
by: John Dann | last post by:
I'm retrieving some columns from a database with numeric values that fall comfortably within the range of the Single type and I'm tempted to use Single for the relevant column type in the retrieved...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
8
by: Grant Edwards | last post by:
I'm pretty sure the answer is "no", but before I give up on the idea, I thought I'd ask... Is there any way to do single-precision floating point calculations in Python? I know the various...
11
by: Steven Woody | last post by:
long i = nnn; long j; double d; d = i; j = ( long )d; in this case, i == j ? thanks.
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
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: 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: 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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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,...

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.