New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
int(5800) == 5799 ? #961
Comments
From mitchell@cs.leander.isd.tenet.eduGiven the following perl script, the int() function seems to generate the #!/usr/bin/perl # int(5800) == 5799 ? $value = (( 29 / 50 ) * 100 ) * 100; #end of example script 1 The output is "int(5800) == 5799", which is not what I expect. I've tried the script on two different machines, and the result is identical The first machine is an AMD K6/2-350, running RedHat Linux 6.1: % uname -a % perl --version The second machine is a Pentium II - 400, running Windows 95 (retail): C:\>ver C:\>perl --version Binary build 509 provided by ActiveState Tool Corp. As I said, both machines produce the unexpected output. FWIW, running the code in a loop 0..50 shows that only the 29 is effected; #!/usr/bin/perl # only 29 is "incorrect" for $i ( 0..50 ) { #end of example script 2 Am I doing something wrong? If I'm making an incorrect assumption, I'd #!/usr/bin/perl # this produces the expected output $value = (( 29 / 50 ) * 100 ) * 100; #end of example script 3 Anything you could tell me would be appreciated. Thanks in advance. -- |
From @tamiasOn Thu, Dec 16, 1999 at 04:14:51PM -0600, Graham Mitchell wrote:
perlfaq4 address this problem: Why am I getting long decimals (eg, 19.9499999999999) The infinite set that a mathematician thinks of as the Internally, your computer represents floating-point However, 19.95 can't be precisely represented as a binary When a floating-point number gets printed, the binary This affects all computer languages that represent decimal To get rid of the superfluous digits, just use a format The result of the floating point arithmetic in your script comes out to be Ronald |
From [Unknown Contact. See original ticket]From: Graham Mitchell [mailto:mitchell@cs.leander.isd.tenet.edu]
The issue is that $value is actually 5799.99999999..., due to the use of But the print statement interpolates $value as a string, using the default C:\DATA>perl -e "printf '%%.20g', ((( 29 / 50 ) * 100 ) * 100)" Hope this helps, |
Migrated from rt.perl.org#1908 (status was 'resolved')
Searchable as RT1908$
The text was updated successfully, but these errors were encountered: