[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] what's the best way to have MacPerl do properlong division?



How about

#!perl -w

sub divalg {
my ($divisor, $dividend) = @_;
my ($quotient, $remainder);
#	dividend = quotient * divisor + remainder
	$remainder = $dividend % $divisor;
	$quotient = ($dividend-$remainder)/$divisor;
	return [$quotient, $remainder];
}

$answer = divalg(24, 273);
print "quotient = $answer->[0] remainder = $answer->[1]\n";

# result : quotient = 11 remainder = 9

You can avoid division (/) by using repeated addition, but why bother?

Regards,

Vic


At 9:05 AM -0600 12/6/00, <CoDeReBeL@hotmail.com> wrote:
>Aside from floating point computations on a computer being approximate, it's
>possible to write a Perl program that will take two numbers and put them
>through the division algorithm you learned in grade school without using the
>"/" operator. I'm not going to sit here and do it myself, but it would give
>you results that lack the particular flaws that sometimes occur from just
>using x / y.
>
>on 11/7/00 9:32 AM, Paul Schinder at schinder@pobox.com wrote:
>
>>  At 9:42 AM -0500 11/7/00, Scott R. Godin wrote:
>>>  in other words, I would like to have more accurate calculations involving
>>>  division, where the percentages don't add up to more than 100%, and where I
>>>  can be reasonably assured of accuracy to around 5-6 decimal places. ?
>>
>>  You should already have that kind of accuracy on a Power PC chip.
>>  What kind of problems are you having?  (You're aware that floating
>>  point computations on a computer are only an approximation?)
>>
>>>
>>>  (glad you're back :)
>>>
>>  --
>>  --
>>  Paul Schinder
>  > schinder@pobox.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org