Re: division by 7 without using division operator



Richard Heathfield wrote:
CBFalconer said:
"*** T. Winter" wrote:
"Francois Grieu" <fgrieu@xxxxxxxxx> writes:
...
To suceed in these interviews, I fear that you must not be so
smart as to outsmart the person asking the questions. So I
suspect the right answer might have been

number *= 0.142857142857143; // divide by 7

which indeed, in quite a few contexts, is an approriate answer.

Indeed, it gives the correct answer for all integers from 0 to
2147483647.

And probably down to -2147483648.

An exercise for those with way too much time on their hands: what
is the smallest-magnitude integer (i.e. regardless of sign) for
which it fails? If this number is negative, what is the smallest
*positive* integer for which it fails?

Without spending too much time on it, the error will be roughly
0.000000000000000143 * N. Equating this expression (abs) to 1.0
should give you the answer.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.