473,695 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5712

"R.Biloti" <bi****@gmail.c om> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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_EPSILO N = %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_EPSILO N = %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_EPSIL ON = %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.c om> 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
1482
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 3, 5.45, 1.23456789012345 i.e. numbers with siginificant count of digits <= 15
31
6630
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
2
9751
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 dataset. (Actually the values are held as integers in the database for compact storage but are scaled to their true Single values during retrieval). But I'm sure that I keep reading that there's no performance penalty to using Double rather...
11
2245
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 Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
7
2702
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 Totto
116
35869
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
8
10407
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 array modules generally support arrays of single-precision floats. I suppose I could turn all my variables into single-element arrays, but that would be way ugly...
11
2662
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
2958
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 bug , i am still c++ beginner
0
9120
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
8996
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...
1
8853
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7667
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5841
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();...
0
4345
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3011
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
2277
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.