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?


In reply to Perl is ignoring fractional part sometimes by jinnicky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.