Solved

floating point addition and multiplication not necessarily associative.

Posted on 2008-09-30
12
656 Views
Last Modified: 2012-05-05
(a) Give an example to show that floating point addition is not necessarily associative.
(b) Give an example to show that floating point multiplication is not necessarily associative.
0
Comment
Question by:panaz
  • 7
  • 5
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22610072
(FLT_EPSILON + FLT_EPSILON) + 2
FLT_EPSILON + (FLT_EPSILON + 2)


FLT_MAX * (FLT_MAX * FLT_MIN)
(FLT_MAX * FLT_MAX) * FLT_MIN
0
 

Author Comment

by:panaz
ID: 22610091
FLT_EPSILON : is this a standard global constant in C/ C++?
0
 
LVL 84

Expert Comment

by:ozo
ID: 22610099
(FLT_EPSILON + 2.5) + -2.5  
FLT_EPSILON + (2.5 + -2.5)
0
 
LVL 84

Expert Comment

by:ozo
ID: 22610189
FLT_EPSILON is in
#include <float.h>
0
 

Author Comment

by:panaz
ID: 22610195
thanks
0
 

Author Comment

by:panaz
ID: 22610277
#include<float.h>
#include<iostream>

using namespace std;
int main()
{
float a,b,c,d;

a = (FLT_EPSILON + 2.5) + -2.5 ;
b = FLT_EPSILON + (2.5 + -2.5);

cout<<"a :"<<a<<endl;

cout<<"b :"<<b<<endl;



return 0;
}


csci>a.out
a :1.19209e-07
b :1.19209e-07


0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:panaz
ID: 22610281
a and b have the same values here. looks like it dont work?
0
 
LVL 84

Expert Comment

by:ozo
ID: 22610440
You are probably doing double precision arithmetic
try

a =  (FLT_EPSILON + 2.5);
cout<<"a :"<<a<<endl;
b = a + -2.5;
cout<<"b :"<<b<<endl;
c = 2.5;
d = -2.5;
a = (FLT_EPSILON + c) + d;
cout<<"a :"<<a<<endl;
b = (float)(FLT_EPSILON + 2.5) + -2.5;
cout<<"b :"<<b<<endl;
b =(FLT_EPSILON + 2.5f) + -2.5f;
cout<<"b :"<<b<<endl;
0
 

Author Comment

by:panaz
ID: 22610472
I guess its still the same values:

int main()
{
double a,b,c,d;

a =  (FLT_EPSILON + 2.5);
cout<<"a :"<<a<<endl;
b = a + -2.5;
cout<<"b :"<<b<<endl;
c = 2.5;
d = -2.5;
a = (FLT_EPSILON + c) + d;
cout<<"a :"<<a<<endl;

b = (float)(FLT_EPSILON + 2.5) + -2.5;
cout<<"b :"<<b<<endl;

b =(FLT_EPSILON + 2.5f) + -2.5f;
cout<<"b :"<<b<<endl;

return 0;

}

 [ Wrote 28 lines ]

csci>g++ test.cpp
csci>a.out
a :2.5
b :1.19209e-07
a :1.19209e-07
b :0
b :0
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 22610562
You changed
float a,b,c,d;
to
double a,b,c,d;
For doubles, use DBL_EPSILON instead of FLT_EPSILON
0
 

Author Comment

by:panaz
ID: 22610575
okay
0
 

Author Comment

by:panaz
ID: 22610628
#include<float.h>
#include<iostream>
#include<limits>

using namespace std;

int main()
{
float a,b,c,d;

a =  (FLT_EPSILON + 2.5);
cout<<"a :"<<a<<endl;
b = a + -2.5;
cout<<"b :"<<b<<endl;

c = 2.5;
d = -2.5;
a = (FLT_EPSILON + c) + d;
cout<<"a :"<<a<<endl;

b = (float)(FLT_EPSILON + 2.5) + -2.5;
cout<<"b :"<<b<<endl;

b =(FLT_EPSILON + 2.5f) + -2.5f;
cout<<"b :"<<b<<endl;


return 0;

}


csci>g++ test.cpp
csci>a.out
a :2.5
b :0
a :0
b :0
b :0
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The CRUD Functions CRUD, meaning "Create, Read, Update, Delete (http://en.wikipedia.org/wiki/Create,_read,_update_and_delete)" is a common term to data base developers.  It describes the essential functions of data base table maintenance.  This art…
Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now