This is precise math???
Hi,
I am sorry if I am being too annoying with my questions, but here's a really puzzling one, especially, since I'd been running the same operation in Java, and it worked PERFECTLY! here's the code: Code:
double i = 0; I've tried, float, coverting double to float, and backwards... Please help, Thanks, Thanks again, Daniel 
Re: This is precise math???
using comparisons is tricky with floating points, which is what a double type is. As an analogy consider decimal numbers. The standard way to to represeent 1/3 is 0.33333333333 (for as long as you want). But multiply this by 3 and you don't have 1. The same problems occur with the binary numbers computers use. Different compilers have different ways of representing floating points, which might be why the program worked in Java.

Re: This is precise math???
Ever_thus gives a great example. You might also consider this: if a 32bit float has more range than a 32bit integer, where's the magic? There isn't any. Range is gained by a loss of precision and accuracy. If I give you only 3 decimal places, but an exponent to go with it, you can express a ton of numbers, but you can't express them all. One deals with this by measuring a result in terms of an error range, rather than by looking for an exact equality.

Re: This is precise math???
Actually,
Whenever I ask the program to print out the multiplication of i*2 One of the answers that comes is 1.6(the correct one!). Thus, I was only able to reckon that something must wrong with the if statement rather then the precision. 1.6 is a definite fraction. Not infinite like 1/3. Also, As I've mentioned earlier, the multiplication 'goeson' just fine. Any other suggestions? Thanks, Daniel 
Re: This is precise math???
1.6 is not a 'definite fraction' in base 2. Even so, that has nothing to do with the problem, per se. If i*2, however correctly or incorrectly it's represented, matches 1.6, however correctly or incorrectly it's represented, then the expression evaluation will be true.
The value of .8, to 5 binary places, is .11001; multiplying that by 2 yields 1.10010. The value of 1.6, to 5 binary places, is 1.10011. Unequal. The value of .8, to 6 binary places, is .110011; multiplying by 2 yields 1.100110. The value of 1.6, to 6 binary places, is 1.10010. Equal. The outcome of your example, therefore, depends entirely upon your particular implementation. You have a couple of problems. You're taking an analogy expressed in base 10 as being an exact example for base 2. It was intended to show you that perfect accuracy is sometimes unattainable, this side of infinity. Your second problem is that you are looking for an exact explanation, such as I have given you above. It is better for you if you read your responses, then go sit on the corral fence and ponder the implications. You'll learn more and understand more. One unfortunate side effect of sitting on the fence and pondering is that you might get sidetracked into wondering why cow crap is like a frisbee and horse crap is like a tennis ball. I still highly recommend it. Despite what you may have heard, "precise math" is not a given. In ANY endeavor. That's why the term, "discarded because it's negligible", is seen in many solutions of complex equations. All you have to do to make your example work is define the bounds of negligibility and implement it. 
All times are GMT +5.5. The time now is 23:42. 