Re: math error
- From: Jim Gibson <jgibson@xxxxxxxxxxxxxxxxx>
- Date: Tue, 23 Aug 2005 12:59:55 -0700
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 =---
.
- References:
- math error
- From: Marshall Dudley
- math error
- Prev by Date: math error
- Next by Date: Re: math error
- Previous by thread: math error
- Next by thread: Re: math error
- Index(es):
Relevant Pages
|
|