Subj : Re: Difference between double To : borland.public.cpp.borlandcpp From : maeder Date : Thu Jul 29 2004 07:12 pm "Matrix" writes: > Hello to everyone....Is there somebody who knows why when I > subtract two double tha are equal the result isnt 0??? Yes. The reason is that computers can only represent a finite set of numbers internally. If you are using a floating point type, all kinds of rounding and truncating happen. > I use this structure: > > typedef struct tagTARIFFE_PACCO > { > double tariffaBase; > double dirittiDoganali; > double supplementi; > double preaffrancatura; > double tariffaTotale(void) > {return tariffaBase+dirittiDoganali+supplementi;}; > double differenzaDovuta(void) > {return tariffaTotale()-preaffrancatura;}; > } TARIFFE_PACCO, *PTARIFFE_PACCO; > > and it happens that when "tariffaTotale" equals "preaffrancatura" > the result isn't 0.... > TIA My knowledge of the Italian language is limited, but these look like monetary values. Dealing correctly with monetary values usually means using a specific class type that correctly performs the roundings as prescribed by the law of the country where the application is run. In the more general case, and if the accuracy of the floating point type is magnitudes higher than what you actually need in your application, you can probably consider two numbers equal if the absolute value of their difference is below some small value. .