Gunnar Hjalmarsson wrote:
> ANJAN PURKAYASTHA answered:
>> ANJAN PURKAYASTHA asked:
>>>
>>> I would like to divide a floating point number into its whole and fractional
>>> parts.
>>> So,
>>> ($w, $f)= some_subroutine(12.735)
>>> assigns 12 to $w and 0.735 to $f.
>>>
>>> Any easy perlish way of doing this?
>> after some research i was able to answer my own question.
>> solution:
>>
>> use POSIX;
>> $n= 12.735;
>>
>> $w= floor($n); # assigns the value 12 to $n.
>>
>> $f= $n-$w; # this holds the fractional value.
> 
> print $f outputs 0.734999999999999 on my box.
> 
> Owen's solution does not have that problem.
> 

Owen wrote:
> 
> #!/usr/bin/perl -w
> 
> use strict;
> 
> my $nr = "12.75";
> 
> my ( $whole, $part ) = ( int($nr), $nr - int($nr) );
> 
> print "$whole   $part\n ";

Owen's solution doesn't show the problem because he uses a value of 12.75, which
is exactly representable in binary, rather than 12.735, which is a recurring
binary value.

Rob

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to