?
Solved

floating point addition and multiplication not necessarily associative.

Posted on 2008-09-30
12
Medium Priority
?
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

 
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
 

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 1000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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