I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
so it is posible to make working with all values, if az i take 0.000000001 then it is not working, so i need to add more 0.00000000000000001 in compare, it is other way?
so it is posible to make working with all values, if az i take 0.000000001 then it is not working, so i need to add more 0.00000000000000001 in compare, it is other way?
Computers work in binary, not decimal. This means that the decimal numbers you're trying to use must be converted to binary. Do you know how to convert a decimal floating point value to a binary floating point value? If you do, then you will see why decimal fractions cannot have exact binary representations unless they are a sum of inverse powers of 2.
If you want exact numbers, then don't use float or double. Use a class that represents arbitrary precision numbers, and there are many of them out there.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 2nd, 2011 at 09:39 AM.
Yes, but dont understand everything. I just need to do a program in few days. This is my first program and I am still learning everything.
Well, this code will not work:
Code:
for (float a = ap; a <= ag; a += az)
Never use floats or doubles as loop counters. Never.
The reason is that you cannot guarantee how many times that loop will execute. It could execute 1 more time than expected, and the reason was explained by everyone so far -- floating point computations are not exact.
Always use integers as loop counters. This guarantees that the loop will execute the exact number of times you expect. For example, if you know you should loop from 0.01 to 0.1 then you write this:
Code:
for (int i = 1; i <= 100; ++i)
{
double myValue = i / 100.0;
// then you use myValue in the loop, not i
//...
}
Now the loop is guaranteed to loop 100 times. The bottom line is however you do it, the goal is to normalize all "counted" loops to use integers as loop counters.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 2nd, 2011 at 09:52 AM.
Bookmarks