[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

re: [MacPerl] Basic question from a raw recruit




Brian segal <astro@juliet.stfx.ca> writes:
} I am writing a little test program to convert a tax included amount to a 
} net amount. It works just fine...except if the gross amount is an even 
} integer - then it returns an erroneous result. For example, if the tax is 
} 7% and I divide through by 1.07 ($net = $gross/1.07) and $gross = 107, I 
} get a net of 9!! (instead of 100).

In which MacPerl?  Tool or application?  PPC or 68k?  I just tried this
in the 68k application:

$gross = 107;
$net = $gross/1.07;

print "$net\n";

and, as expected, I got 100.  Just to make sure things work as I expect

$gross = 107;
$net = $gross/1.08;

print "$net\n";

gives 99.0740740740741.

Did you syntax check your script?   Maybe you're not doing what you
think you're doing.

} 
} Now, a friend, who is an experienced programmer, but who doesn't know 
} Perl, says it is because Perl is treating the number like an integer 
} instead of a floating point number (probably because it IS an integer, 
} having no decimal remainder).

But Perl works very much like C (or Fortran for that matter) in these
cases.  It sees that 1.07 is a float, so it knows it has to promote
$gross to float to do the division properly no matter what it contains.
Now in C or Fortran what happens next depends on what $net was declared
as, but there isn't such a problem in Perl.  I suspect something wrong
in the script, since I use Perl for numerical work all the time on Unix
workstations and doubt very much that the way numerical computations
are done changes at all in MacPerl.

} 
} Question: How do I tell Perl that the integer is to be treated as a 
} floating  point number...I seem to recall reading that Perl treats 
} integers like Floating point numbers as a matter of course, but that was 
} in a Unix Perl book (Teach yourself Perl in 21 days), not a MacPerl 
} manual (which I have never seen anywhere.)

You should try $gross = 107. (note the decimal point), or even $gross =
1.07e2 and see whether or not things change for you.

} 
} Sorry to bother you all with such trivia...
} 
} Brian
} 
} 
} 
} **************************************************************
} * Brian G Segal - astro@juliet.stfx.ca - redgull@fox.nstn.ca *
} * R.R. #5  Antigonish, Nova Scotia                           *
} * Canada B2G 2L3                                             *
} * Phone/Fax 902-783-2772                                     *
} * Home page: http://fox.nstn.ca/~redgull                     *
} **************************************************************
} 
} 
---
--------
Paul J. Schinder
NASA Goddard Space Flight Center,
Code 693, Greenbelt, MD 20771 USA
schinder@pjstoaster.pg.md.us