Re: 1.0 divided by 10 = 0.10000000000000001?
Re: 1.0 divided by 10 = 0.10000000000000001?
- Subject: Re: 1.0 divided by 10 = 0.10000000000000001?
- From: Phil Faber <email@hidden>
- Date: Tue, 28 Mar 2006 22:34:22 +0100
Of course the other answer is to write your own code to produce as
much precision as you like for the division.
The logic uses subtraction and goes as follows; here I'm using the
example, of 23/7.
Count the number of times you can SUBTRACT the 7 from the 23
(23-7-7-7=2 ... so A: 3r2. Print the 3 then a '.'.)
If you don't have a remainder of 0, multiply what's left by 10 (2 *
10 = 20)
Count the number of times you can SUBTRACT the 7 from the new number
(20-7-7=6 ... so A: 2r6. Print the 2.)
If you don't have a remainder of 0, multiply what's left by 10 (6 *
10 = 60)
Count the number of times you can SUBTRACT the 7 from the new number
(60-7-7-7-7-7-7-7-7=4 ... so A: 8r4. Print the 8.)
If you don't have a remainder of 0, multiply what's left by 10 (4 *
10 = 40)
Count the number of times you can SUBTRACT the 7 from the new number
(40-7-7-7-7-7=5 ... so A: 5r5. Print the 5.)
If you don't have a remainder of 0, multiply what's left by 10 (5 *
10 = 50)
...and so on. The longer you run it the more accurate the answer
will be! Of course in this example, the answer is:
3.285714 285714 285714 285714 285714 285714 285714 ..etc
..so the EXACT answer cannot be reached!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden