in reply to Scientific Notation Throws Off Results.

Well, I can see where you are using length, but your code fragment offers no indication at all which value Perl is shortening to scientific notation. Are we talking floats here? Integers? Which value are you printing, what are you getting, and what do you want to get?

Abigail

  • Comment on Re: Scientific Notation Throws Off Results.

Replies are listed 'Best First'.
Re: Re: Scientific Notation Throws Off Results.
by Elijah (Hermit) on Jan 14, 2004 at 00:06 UTC
    Sorry, here is the full code so you might have a better idea of what goes where. It is a gui app written in Tk.

    #!/usr/local/bin/perl -w #perl2exe_include Tk; #perl2exe_include Tk::Photo; #perl2exe_include strict; use Tk; use Tk::Photo; use strict; ########################################### ########################################### ## ## ## This script is a graphical interface ## ## that allows a user to easily convert ## ## IP bandwidth into ATM PCR. ## ## PCR = (Peak Cell rate) ## ## ## ########################################### ########################################### our($choice, $cb, $pcr, $bps, $status, $answer, $cell_size, $cell_size +2); my $window = MainWindow->new; $window->title("ip2atm"); my $frame_space = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame_space2 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame2 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame_space3 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame3 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame4 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame_space5 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame5 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame6 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame_space7 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame7 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame_space8 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame8 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame9 = $window->Frame->pack(-side=>'top', -fill=>'x'); my $frame10 = $window->Frame->pack(-side=>'top', -fill=>'x'); $frame_space->Label()->pack(-fill=>'x'); $frame->Label(-text=>"Choose Conversion Type")->pack(-side=>'top', -an +chor=>'n'); $frame->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame->Button(-text=>"Cells to Bits", -command=>\&cb)-> pack(-side=>'left', -anchor=>'w', -fill=>'none'); $frame->Label(-text=>" ")->pack(-side=>'right', -anchor=>'e'); $frame->Button(-text=>"Bits to Cells", -command=>\&bc)-> pack(-side=>'right', -anchor=>'e', -fill=>'none'); $frame_space2->Label()->pack(-fill=>'x'); $frame2->Label(-text=>"Conversion Type Being Used")->pack(-side=>'top' +, -anchor=>'n'); $frame2->Entry(-textvariable=>\$choice)-> pack(-side=>'bottom', -anchor=>'n', -fill=>'none'); $frame_space3->Label()->pack(-fill=>'x'); $frame3->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame3->Label(-text=>"Enter Peak Cell Rate")->pack(-side=>'left', -an +chor=>'w'); $frame3->Label(-text=>" ")->pack(-side=>'right', -anchor=>'e'); $frame3->Label(-text=>"Enter Bits Per Second")->pack(-side=>'right', - +anchor=>'e'); $frame4->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame4->Entry(-textvariable=>\$pcr)-> pack(-side=>'left', -anchor=>'w', -fill=>'none'); $frame4->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame4->Label(-text=>" ")->pack(-side=>'right', -anchor=>'e'); $frame4->Entry(-textvariable=>\$bps)-> pack(-side=>'right', -anchor=>'e', -fill=>'none'); $frame_space5->Label()->pack(-fill=>'x'); $frame5->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame5->Label(-text=>"Enter Cell Size")->pack(-side=>'left', -anchor= +>'w'); $frame5->Label(-text=>" ")->pack(-side=>'right', -anchor=>'e'); $frame5->Label(-text=>"Enter Cell Size")->pack(-side=>'right', -anchor +=>'e'); $frame6->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame6->Entry(-textvariable=>\$cell_size)-> pack(-side=>'left', -anchor=>'w', -fill=>'none'); $frame6->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame6->Label(-text=>" ")->pack(-side=>'right', -anchor=>'e'); $frame6->Entry(-textvariable=>\$cell_size2)-> pack(-side=>'right', -anchor=>'e', -fill=>'none'); $frame_space7->Label()->pack(-fill=>'x'); $frame7->Button(-text=>" Convert ", -command=>\&convert)-> pack(-side=>'top', -anchor=>'n'); $frame_space8->Label()->pack(-fill=>'x'); $frame8->Label(-text=>" Answer ")->pack(-side=>'top', -anchor=>'n'); $frame9->Label(-text=>" ")->pack(-side=>'left', -anchor=>'w'); $frame9->Entry(-textvariable=>\$answer)-> pack(-side=>'top', -anchor=>'n', -fill=>'none'); $frame10->Button(-text=>" Exit ", -command=>sub{exit;})-> pack(-side=>'bottom', -anchor=>'e'); $window->Label(-textvariable=>\$status, -relief=>'ridge')-> pack(-side=>'bottom', -fill=>'x'); if (-e "converter.gif") { $window->Photo('image', -file=>"converter.gif"); $window->Label(-image=>'image')->pack(-side=>'bottom'); } $status = "Written by: Me"; MainLoop; sub cb { $choice = "Cells to Bits"; $bps = "N/A"; $cell_size2 = "N/A"; $pcr = ""; $cell_size = ""; $answer = ""; } sub bc { $choice = "Bits to Cells"; $pcr = "N/A"; $cell_size = "N/A"; $bps = ""; $cell_size2 = ""; $answer = ""; } sub convert { my ($len1, $type); if ($choice eq "Cells to Bits") { if ($pcr && $cell_size) { $answer = $pcr * $cell_size * 8; $len1 = length($answer); print $len1,"\n"; print $answer,"\n"; if (($len1 < 4) || ($len1 > 15)) { $type = "b/s"; }elsif (($len1 >= 4) && ($len1 <= 6)) { $type = "Kb/s"; $answer = substr($answer, 0, 1)." ".$type if $len1 == 4; $answer = substr($answer, 0, 2)." ".$type if $len1 == 5; $answer = substr($answer, 0, 3)." ".$type if $len1 == 6; }elsif (($len1 >= 7) && ($len1 <= 9)) { $type = "Mb/s"; $answer = substr($answer, 0, 1)." ".$type if $len1 == 7; $answer = substr($answer, 0, 2)." ".$type if $len1 == 8; $answer = substr($answer, 0, 3)." ".$type if $len1 == 9; }else{ $type = "Gb/s"; foreach (my $count=1; $count<10; $count++) { chop($answer); } $answer .= " ".$type; } $status = "Conversion Successfull!"; }else{ $status = "Error: required field missing!"; } }elsif ($choice eq "Bits to Cells") { if ($bps && $cell_size2) { $answer = $bps / 8 / $cell_size2; $answer = sprintf("%.0f", $answer) . " cps"; $status = "Conversion Successfull!"; }else{ $status = "Error: required field missing!"; } }else{ $status = "Error: required field missing!"; } }
    I know that after 15 digits (yes integers) that the format changes to scientific notation therefore I have coded that into my script but after I think

    Here is a debugger output I have going to the command prompt to check the value of the 2 variables ($len1 and $answer) and you cans ee where the length jumps back down and screws me.

    5 41976 6 423576 7 4239576 8 42399576 9 423999576 10 4239999576 11 42399999576 12 423999999576 13 4239999999576 14 42399999999576 15 423999999999576 21 4.23999999999958e+015 21 4.23999999999996e+016 9 4.24e+017

    The first number (as you can see by the code) is the value of the length in digits of the answer and the second (longer number) is the actual answer.

      No doubt very interesting, but I'm not going to try to grok a program several pages long.

      Try posting a short program that exhibits the unwanted behaviour.

      Abigail