Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Integer Division

Status
Not open for further replies.

StormMedic85

Programmer
Sep 7, 2007
20
US
Is there a way to do integer division in Perl similar to C's "div" operator?

I'm trying to implement the following algorithm to convert Julian Date to Gregorian and it requires integer division otherwise it breaks down.

Thanks!

l = jd + 68569
n = ( 4 * l ) / 146097
l = l - ( 146097 * n + 3 ) / 4
i = ( 4000 * ( l + 1 ) ) / 1461001
l = l - ( 1461 * i ) / 4 + 31
j = ( 80 * l ) / 2447
d = l - ( 2447 * j ) / 80
l = j / 11
m = j + 2 - ( 12 * l )
y = 100 * ( n - 49 ) + i + l
 


I just found the link on the net.. so I promise nothing.. but looks pretty good :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
There's no integer division op in perl.
You can use either of the following:
Code:
$result=int($dividend/$divisor);
$result=($dividend-$dividend%$divisor)/$dividend;
The two give different results with negatives and non integers: one must choose the right one if such conditions occur.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
As an addition to the above, if you say [tt]use integer;[/tt] in the block that needs integer calcs (e.g. at the start of the [tt]sub[/tt] that performs that series of calculations), all the divisions will be integer divs, giving the same result as [tt]int[/tt].

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top