jinnicky has asked for the wisdom of the Perl Monks concerning the following question:
I'm running Perl 5.20.1 on openSUSE 13.2. The application involves scaling dimensions for a model railroad. So 12 by 14 inches should scale to 270 by 315 inches with a scale factor of 22.5 (G scale)
The calc subroutine shows 264 by 308, which is a scale of 22.0. The printed trace shows the correct x,y and scale values with the calculated values. When you change one of the inputs and hit enter, the fractional part is sometimes ignored. It may be correct for one of the calculations and not the other.
It's a puzzlement!
#!/usr/bin/perl -w use strict; use Tk; my $x = '12.0'; my $y = '14.0'; my $scale = '22.5'; my $xmax = $x * $scale; my $ymax = $y * $scale; my $shouldBe = sprintf("Originally X=%4.1f, Y=%4.1f, scale=%4.1f X-max +=%8.4f, Y-Max=%8.4f",$x,$y,$scale,$xmax,$ymax); my $results; my $mw = MainWindow->new; my $InFrame = $mw->Frame()->pack(-side=>'top'); $InFrame->Label(-text=>"X= ")->pack(-side=>'left'); $InFrame->Entry(-textvariable=>\$x)->pack(-side=>'left'); $InFrame->Label(-text=>" Y= ")->pack(-side=>'left'); $InFrame->Entry(-textvariable=>\$y)->pack(-side=>'left'); $InFrame->Label(-text=>"Scale= ")->pack(-side=>'left'); $InFrame->Entry(-textvariable=>\$scale)->pack(-side=>'left'); my $OutFrame = $mw->Frame()->pack(-side=>'top'); $OutFrame->Label(-textvariable=>\$results)->pack(-side=>'top'); $OutFrame->Label(-textvariable=>\$shouldBe)->pack(-side=>'top'); $mw->bind("<Key-KP_Enter>",\&calc); $mw->bind("<Key-Return>",\&calc); calc(); MainLoop; sub calc { $xmax = $x * $scale; $ymax = $y * $scale; $results = sprintf("Calculated X-max=%8.4f, Y-Max=%8.4f",$xmax,$ymax +); print "$x, $y, $scale, $xmax, $ymax\n"; }
Can anyone explain this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl is ignoring fractional part sometimes
by Anonymous Monk on Jul 17, 2016 at 23:54 UTC | |
by jinnicky (Sexton) on Jul 18, 2016 at 03:01 UTC | |
|
Re: Perl is ignoring fractional part sometimes
by haukex (Archbishop) on Jul 18, 2016 at 09:58 UTC | |
by jinnicky (Sexton) on Jul 18, 2016 at 17:11 UTC | |
by jinnicky (Sexton) on Jul 19, 2016 at 17:14 UTC | |
|
Re: Perl is ignoring fractional part sometimes
by Anonymous Monk on Jul 17, 2016 at 22:52 UTC | |
by jinnicky (Sexton) on Jul 17, 2016 at 23:38 UTC |