Thanks for the information. I tried using the examples given in my program but have still come up against a problem.
In my program I am receiving in certain amounts of stock in less that 1kg quantities. The user enters the amount into a QLineEdit and that amount is then subtracted from the outstanding amount. They then enter another amount up until the point where we have received in all outstanding stock.
For example I have 1kg outstanding and I receive in 0.9kg and then 0.1kg. I then hit a problem with 0.9kg + 0.1kg not equaling 1kg.
It looks as if using float or double is a bad idea as it seems to be non-precise. I have included a test piece of code that demonstrates this.
float float1(1);
float1 = float1 - (float)string1.toFloat();
float1 = float1 - (float)string2.toFloat();
QString string1("0.9");
QString string2("0.1");
float float1(1);
float1 = float1 - (float)string1.toFloat();
float1 = float1 - (float)string2.toFloat();
QMessageBox::information( 0, "float1", QString("float1 : %1").arg( float1 ) );
To copy to clipboard, switch view to plain text mode
It gives the result of float1 to be : 2.23517e-08
How do people normally work around this problem?
Thanks,
Royce
Bookmarks