in reply to Re: int's behaviour
in thread int's behaviour

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.

Replies are listed 'Best First'.
Re^3: int's behaviour
by Corion (Patriarch) on Nov 25, 2007 at 17:02 UTC

    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!!!!

        Sorry, but I can't easily follow your code. It is not a self-contained program, you don't show the input values to your subroutine find_range and I don't see how your second subroutine, read_tagdata relates to the first subroutine at all. I imagine something like the following skeleton that shows us what values you pass in to find_range and also shows how these values are not what you expect:

        #!/usr/bin/perl -w use strict; sub find_range { ... } print find_range('100/65535'); # prints 1, OK print find_range('1/1'); # prints warning and 255, OK print find_range('99/99999'); # prints 1, should print 2