in reply to int's behaviour

try:
sub round { map{ int($_ + 0.5) @_ }

Replies are listed 'Best First'.
Re^2: int's behaviour
by last_stand (Initiate) on Nov 25, 2007 at 16:49 UTC
    I am putting the code:: i am using following function to figure out range of a variable.. $range_data= find_range($max_data,$min_data,$step_data); I am passing the max value, min value and resolution. The function would return the range. the function definition is::
    sub find_range { my $max=$_[0]; my $min=$_[1]; my $step=$_[2]; my @step_part; my $range; $range=($max-$min)/($step_real); return($range); }
    now when I do: $range_int= int($range_data); and try to print range_data and range_int i get different values.specifically 65535 becomes 65534 and 255 becomes 254. Also I know range_data is 65535 and not 65534.99 . Can anyone help me.

      Either the code you posted is not the code you are using or you should be using the strict pragma. You are initializing a variable $step but you are dividing by a variable $step_real.

      Also, please post a complete, self-contained, short program that demonstrates the issue. You talk about variables with names like range_data and range_int but never show how and where they are used.

        yeah..u r right. I have not posted the complete code. I an doing that now:
        sub find_range { my $max=$_[0]; my $min=$_[1]; my $step=$_[2]; my @step_part; my $range; $dec_lolmt=0; # for avoiding positive offset in compu method any min data gr +eater than zero is made equal to zero. if ($min>0) { $dec_lolmt=$min; $min=0; print OUT_LOG "WARNING: minimum physical value found to be + positive for $name and is reduced to zero \n"; } my @step_part=split(/\//,$step); if($step_part[1]=~ /^[0-9]/) { $step_real=$step_part[0]/$step_part[1]; } else { $step_real=$step_part[0]; } # check for proper value of range could be returned or not if((!($step_real=~ /^[0-9]/))||($max==$min)||($step_real==0)| +|(!($step_real =~ /^-?\d/))) { print OUT_LOG "WARNING: No proper range could be determine +d for $name : default range returned as 255 \n"; $range = 255; } $range=($max-$min)/($step_real); # check if range is feasible or not(range cannot be less than +1) if($range<1) { print OUT_LOG "WARNING: No proper range could be determine +d for $name : defalult range returned as 255 \n"; $range = 255; } return($range); }
        The split is done to obtain the actual step values in cases where i read (say:100/65535) at step(read resolution). the reading tagged data function is:
        sub read_tagdata { my $tag_start= $_[0]; #read the start of the tag my $tag_end=$_[1]; #read the end of the tag my $temp_buffer_data= $_[2]; #read the tagged line my @buffer_data_split; $tag_error=($temp_buffer_data=~ s/$tag_start/;/); $tag_error1=($temp_buffer_data=~ s/$tag_end/;/); if (($tag_error==0) || ($tag_error1==0)) { if(!($tag_start=~ /parameter/) && !($tag_start=~ /intern/) + && !($tag_start=~ /out/) && !($tag_start=~ /visu/)) { print OUT_LOG "WARNING: No data found at $tag_start + $tag_end for $name_data \n"; $buffer_data_split[1]="UNKNOWN DATA"; } } else { @buffer_data_split= split(/;/,$temp_buffer_data); } return($buffer_data_split[1]) }
        since i am very new to all this i thought that my code may seem stupid to u guys!! thats why i was hiding some part. But now I DONT CARE!!!!