Sign in to follow this  
gohkgohk

floating point number error

Recommended Posts

gohkgohk    100
#include "stdafx.h"
#pragma   warning   (disable:   4786)   
#include <vector>
#include <map>

struct coordinate {
  int x, y, z; 
  coordinate(int x, int y, int z): x(x), y(y), z(z) {}
};

struct fcoordinate {
  float fx, fy, fz; 
  fcoordinate(float fx, float fy, float fz): fx(fx), fy(fy), fz(fz) {}
};

struct COMPARE  
{  
          bool   operator()(coordinate   k1,coordinate   k2)  const    
          {  if (k1.x   !=   k2.x)
                return   k1.x   <   k2.x;
		    else
				if (k1.y   !=   k2.y)
                return   k1.y   <   k2.y;
			else
                return   k1.z   <   k2.z;
          }  
};  


int main(int argc, char* argv[])
{

typedef std::vector<fcoordinate> triangle_list;
typedef std::map<coordinate, triangle_list,COMPARE> space_mapping;
space_mapping base_array;
triangle_list* t = &base_array[ coordinate(0, 3, 2) ];
t->push_back(fcoordinate(100.1, 250, 300));

triangle_list all_triangles;
for (space_mapping::iterator it = base_array.begin(); it != base_array.end(); ++it) {
  triangle_list& current_triangles = it->second;
  std::copy(current_triangles.begin(), current_triangles.end(), std::back_inserter(all_triangles));
}

printf("%f\n",all_triangles[0].fx);

return 0;
}




100.1 become 100.099998.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by gohkgohk
Sorry...that webpage is too long...Orz
Which part should i look at?....


As the title indicates, if you intend to have any serious business with floating-point numbers, you should read and understand that entire page.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Don't worry, you don't need to read the whole thing.
Just read the title. Then you can decide if you need to read the actual article or not. As it says, it's something a computer scientist should know. If you want to program, you need to know it *all*. Otherwise, you can skip it. Up to you. :)

Share this post


Link to post
Share on other sites
Antheus    2409
Floating point numbers are imprecise.

Any operation, no matter with which numbers using at least some of the members as floating point types will result in inaccurate result.

It's impossible to compare floating point types for equality using ==. While it can be done, it yields incorrect results.

If you need to compare for similarity, use some epsilon value: fabs(f1-f2) < EPSILON, where EPSILON is some value that is relatively small compared to f1 and f2.

Results of floating point computation may differ between languages, compilers and processors.

Share this post


Link to post
Share on other sites
DevFred    840
Quote:
Original post by Antheus
Floating point numbers are imprecise.

Any operation [...] will result in inaccurate result.

It's impossible to compare floating point types for equality using ==. While it can be done, it yields incorrect results.

While it's safe to assume this points, they are not entirely correct. They important thing to understand is this:

floating point VALUES are represented in BINARY, while floating point LITERALS are displayed in DECIMAL in most programming languages. The problem is: most of the finite decimal fractions that we are used to (like 0.1 or 0.2) need infinite binary precision. But since floats only have 24 bits of mantissa, you get a rounded result IF THE BINARY FRACTION IS INFINITE.

Some time ago I wrote a program where you can enter a literal and it then outputs the binary representation and the actual decimal value it represents. You can download it here (6KB, Java 5 required).



Do you see the repeating pattern in the mantissa? The exact value is 1100110011001100... ad infinitum, but since it has to be cut after 24 bits, 0.1 can only be approximated by 0.100000001490116119384765625.

On the other hand, values like 0.5, 0.25, 0.625 or 1000.75 can be represented with a finite numbers of bits <24, so there are no approximations or imprecisions.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by DevFred
Quote:
Original post by Antheus
Floating point numbers are imprecise.

Any operation [...] will result in inaccurate result.

It's impossible to compare floating point types for equality using ==. While it can be done, it yields incorrect results.

While it's safe to assume this points, they are not entirely correct. They important thing to understand is this:


Yes, but since OP was somewhat reluctant to read the whole thing, I gave the worst-case cliff notes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this