Q&A: java calculator.. how to solve division without using “/”?
Question by Mutagen: java calculator.. how to solve division without using “/”?
Best answer:
Answer by JoelKatz
Probably the easiest way is to use Newton’s method.
Basically, use the following mechanism:
1) Pick a number that’s definitely too big to be the answer and a number that’s definitely too small to be the answer. Call these the ‘upper limit’ and ‘lower limit’. (You can use the largest and smallest numbers your calculator is designed to handle.)
2) Calculate the number halfway between the upper and lower limits. (You can use a binary shift to divide by two.)
3) If the lower and upper limits are close enough to each other, stop. The number we calculated in step 2 is the answer.
4) Do a trial multiplication to determine if the number we calculated is too small or too large to be the correct answer.
5) If the number is too small, replace the lower limit with the trial number. If the number is too large, replace the upper limit with the trial number.
6) Go to step 2.
What do you think? Answer below!
Tags: calculator, division, Java, solve, Using, WithoutRelated Posts
Tagged with: calculator • division • Java • solve • Using • Without
Filed under: NET Pay Calculator Answers
Like this post? Subscribe to my RSS feed and get loads more!
try to use mod or %
Why would you do that?
If it’s strength reduction that you’re looking for then just use the shift for divisions of powers of two, but usually the compiler *should* do this for you.
I’ve attached a link for doing this for every value.
Newton’s method :p… looks crazy man.
Shift right to divide…Remember Java integers are all signed meaning the twos compliment is taken.
Easiest way to do this is flip all the bits and add one.
5 d
0101 b flip em 1010 add 1…