in reply to Coping with decimals

my $calc = ($X-$Y)/$Z; my $phase = $calc - int $calc;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Coping with decimals
by Ninthwave (Chaplain) on Jun 02, 2005 at 11:12 UTC

    Be careful with this technique as storage of decimals in a computer is never accurate and a conversion to int could throw you off.

    See this node for a discussion on the same lines

    Dividing and format

    Specifically the links in my reply here:

    Ninthwave's reply in Dividing and format


    "No matter where you go, there you are." BB
Re^2: Coping with decimals
by bofh_of_oz (Hermit) on Jun 02, 2005 at 14:16 UTC
    Be also careful if you have negative numbers...

    e.g. $X = 1, $Y = 7, $Z = 5

    Then:

    ($X-$Y) = -6, so $calc = ($X-$Y)/$Z = -1.2

    but $phase = $calc - int $calc = -1.2 -1 = -2.2, which is not what you wanted...

    --------------------------------
    An idea is not responsible for the people who believe in it...

      Did you try it?

      $ perl -e'my ($X,$Y,$Z)=(1,7,5);my $c=($X-$Y)/$Z;print $c-int$c, $/' -0.2 $
      That may not be what's wanted, either.

      After Compline,
      Zaxo

Re^2: Coping with decimals
by Ruanae (Initiate) on Jun 02, 2005 at 09:40 UTC
    Many Thanks :-)