Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

floating point addition and multiplication not necessarily associative.

Posted on 2008-09-30
12
Medium Priority
?
680 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
Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the most frequent problems a "newbie" developer may encounter is having to deal with different data formats. One for all: THE DATE We, as humans, need to "see" a date and then interpret it (much of the times this is an automatic operation)…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

609 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