Re: math error



In article <430B5391.32F21D40@xxxxxxxxxxxxx>, Marshall Dudley
<mdudley@xxxxxxxxxxxxx> wrote:

> The code:
>
> $weight = 5.03;
> my $gross = int($weight);
> my $frac = $weight - $gross;
> print "$frac\n";
>
> Should print the decimal part of .03, but gives this instead:
>
> 0.0300000000000002
>
> What am I doing wrong?

You are expecting the number 5.03 to be stored precisely in
floating-point binary format, which it cannot.

See 'perldoc -q numbers'

Use
printf "%.2f\n", $frac;

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
.



Relevant Pages

  • Re: append contents of field to another field
    ... Dirk Goldgar, MS Access MVP ... (please reply to the newsgroup) ... Prev by Date: ...
    (microsoft.public.access.formscoding)
  • Re: This is a defunct newsgroup
    ... used the MSDN web interface to post to this supposed defunct newsgroup. ... of newgroups that the server provides them. ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: How do I reorder startup?
    ... Ken Blake - Microsoft MVP Windows: Shell/User ... Please reply to the newsgroup ... Prev by Date: ...
    (microsoft.public.windowsxp.general)
  • Re: Set FileSize
    ... >>I resize the file? ... > Please reply only to the newsgroup. ... Prev by Date: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Percentages...gone awry.
    ... Dirk Goldgar, MS Access MVP ... (please reply to the newsgroup) ... Prev by Date: ...
    (microsoft.public.access.forms)