# FAQ 4.2 Why is int() broken?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.2: Why is int() broken?

Your "int()" is most probably working just fine. It's the numbers that
aren't quite what you think.

First, see the answer to "Why am I getting long decimals (eg,
19.9499999999999) instead of the numbers I should be getting (eg,
19.95)?".

For example, this

print int(0.6/0.2-2), "\n";

will in most computers print 0, not 1, because even such simple numbers
as 0.6 and 0.2 cannot be presented exactly by floating-point numbers.
What you think in the above as 'three' is really more like
2.9999999999999995559.

--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

## Re: FAQ 4.2 Why is int() broken?

print int(0.6/0.2), "\n";

will in most computers print 2, not 3, because even such simple numbers

(The extra subtraction is not necessary to demonstrate the problem and is
distracting)

as 0.6 and 0.2 cannot be represented exactly by binary floating-point
numbers.

(0.6 and 0.2 can be represented exactly in decimal floating-point
numbers. Or in base 5 or base 100, or any other base which is a multiple
of 5. The problem isn't floating-point, it's binary. A binary fixed
point representation would have the same problem.)

## Re: FAQ 4.2 Why is int() broken?

Indeed strange behavior. I noticed this strange behavior working on the
ceil/floor
Perl equivalent. Take Note the below inconsistant behavior.
Here is some floor() code and output from Visual Studio 2005:

printf("====================\n\n");
printf( "The floor of -3.0 is %f\n\n", floor(-3.0) );
printf( "The floor of -4.0 is %f\n\n", floor(-4.0) );
printf( "The floor of -5.0 is %f\n\n", floor(-5.0) );

printf("\n\n");
for (double y=2.0; y<=3.25; y+=.25)
{
printf( "The floor of %f is %f\n", y, floor(y) );
printf( "The floor of -%f is %f\n\n", y, floor(-y) );
}
printf("*The floor of 0.3/0.1-2 is %f\n", floor(0.3/0.1-2) );
printf("The floor of 0.6/0.2-2 is %f\n", floor(0.6/0.2-2) );
printf("The floor of 0.9/0.3-2 is %f\n", floor(0.9/0.3-2) );
printf("The floor of 1.2/0.4-2 is %f\n", floor(1.2/0.4-2) );
printf("The floor of 1.5/0.5-2 is %f\n", floor(1.5/0.5-2) );
printf("The floor of 1.8/0.6-2 is %f\n", floor(1.8/0.6-2) );
printf("The floor of 2.1/0.7-2 is %f\n", floor(2.1/0.7-2) );
printf("The floor of 2.4/0.8-2 is %f\n", floor(2.4/0.8-2) );
printf("The floor of 2.7/0.9-2 is %f\n", floor(2.7/0.9-2) );
printf("\n");
for (double y=.1; y<=5.0; y+=.1)
{
double z = 3.0 * y;
printf( "The floor of %.1f/%.1f-2 is %f\n", z, y, floor(z/y-2) );
}

====================

The floor of -3.0 is -3.000000

The floor of -4.0 is -4.000000

The floor of -5.0 is -5.000000

The floor of 2.000000 is 2.000000
The floor of -2.000000 is -2.000000

The floor of 2.250000 is 2.000000
The floor of -2.250000 is -3.000000

The floor of 2.500000 is 2.000000
The floor of -2.500000 is -3.000000

The floor of 2.750000 is 2.000000
The floor of -2.750000 is -3.000000

The floor of 3.000000 is 3.000000
The floor of -3.000000 is -3.000000

The floor of 3.250000 is 3.000000
The floor of -3.250000 is -4.000000

*The floor of 0.3/0.1-2 is 0.000000
The floor of 0.6/0.2-2 is 0.000000
The floor of 0.9/0.3-2 is 1.000000
The floor of 1.2/0.4-2 is 0.000000
The floor of 1.5/0.5-2 is 1.000000
The floor of 1.8/0.6-2 is 1.000000
The floor of 2.1/0.7-2 is 1.000000
The floor of 2.4/0.8-2 is 0.000000
The floor of 2.7/0.9-2 is 1.000000

The floor of 0.3/0.1-2 is 1.000000
The floor of 0.6/0.2-2 is 1.000000
The floor of 0.9/0.3-2 is 1.000000
The floor of 1.2/0.4-2 is 1.000000
The floor of 1.5/0.5-2 is 1.000000
The floor of 1.8/0.6-2 is 1.000000
The floor of 2.1/0.7-2 is 0.000000
The floor of 2.4/0.8-2 is 1.000000
The floor of 2.7/0.9-2 is 1.000000
The floor of 3.0/1.0-2 is 1.000000
The floor of 3.3/1.1-2 is 1.000000
The floor of 3.6/1.2-2 is 1.000000
The floor of 3.9/1.3-2 is 1.000000
The floor of 4.2/1.4-2 is 1.000000
The floor of 4.5/1.5-2 is 1.000000
The floor of 4.8/1.6-2 is 1.000000
The floor of 5.1/1.7-2 is 1.000000
The floor of 5.4/1.8-2 is 1.000000
The floor of 5.7/1.9-2 is 1.000000
The floor of 6.0/2.0-2 is 1.000000
The floor of 6.3/2.1-2 is 1.000000
The floor of 6.6/2.2-2 is 1.000000
The floor of 6.9/2.3-2 is 1.000000
The floor of 7.2/2.4-2 is 1.000000
The floor of 7.5/2.5-2 is 1.000000
The floor of 7.8/2.6-2 is 1.000000
The floor of 8.1/2.7-2 is 1.000000
The floor of 8.4/2.8-2 is 1.000000
The floor of 8.7/2.9-2 is 0.000000
The floor of 9.0/3.0-2 is 1.000000
The floor of 9.3/3.1-2 is 1.000000
The floor of 9.6/3.2-2 is 1.000000
The floor of 9.9/3.3-2 is 1.000000
The floor of 10.2/3.4-2 is 1.000000
The floor of 10.5/3.5-2 is 1.000000
The floor of 10.8/3.6-2 is 1.000000
The floor of 11.1/3.7-2 is 0.000000
The floor of 11.4/3.8-2 is 1.000000
The floor of 11.7/3.9-2 is 1.000000
The floor of 12.0/4.0-2 is 1.000000
The floor of 12.3/4.1-2 is 1.000000
The floor of 12.6/4.2-2 is 1.000000
The floor of 12.9/4.3-2 is 1.000000
The floor of 13.2/4.4-2 is 1.000000
The floor of 13.5/4.5-2 is 1.000000
The floor of 13.8/4.6-2 is 1.000000
The floor of 14.1/4.7-2 is 1.000000
The floor of 14.4/4.8-2 is 1.000000
The floor of 14.7/4.9-2 is 1.000000
The floor of 15.0/5.0-2 is 1.000000

## Re: FAQ 4.2 Why is int() broken?

Maybe it was derived from that

perl -wle '
printf qq|%e\n|, 0.6/0.2;
printf qq|%e\n|, 0.6/0.2-2;
printf qq|%e\n|, 0.6/0.2-3;
'
3.000000e+00
1.000000e+00
-4.440892e-16

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

## Re: FAQ 4.2 Why is int() broken?

PerlFAQ Server wrote:

Huh? Broken, how? "it doesn't work" is not a proper diagnosis, let
alone, a title in the FAQ, with no other information added.

I thought it was going to be about how int(-2.5) returns -3, of which
correctness depends on who you ask.

--
Bart.

## Re: FAQ 4.2 Why is int() broken?

Yet Questions that are Asked Frequently *are* often improper diagnosis.

If it is asked frequently, then it is a good title for a FAQ.

If is is asked poorly, then it is still a good title for a FAQ.

:-)

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: FAQ 4.2 Why is int() broken?

I guess we have to ask why is for() broken then. The list is endless.
At some point you have to assume little Johnny is going discover the
world beyond his navel. Its better to make an FAQ to the bigger

No, this is not a valid FAQ, far from it.

sln

----------------------------
use strict;
use warnings;

my \$dbl;
for (\$dbl = 2.0; \$dbl <= 3.0; \$dbl += .1) {
print "\$dbl\n";
}
if (\$dbl == 3.0) {
print "\$dbl is EQUAL to 3.0\n";
} else {
if (\$dbl > 3.0) {
printf "\n\$dbl EQAULS %e\n",\$dbl;
print "\$dbl is NOT equal to 3.0 !!\n\nNew FAQ:  Why is for() broken?\n";
}
}
__END__

2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9

\$dbl EQAULS 3.000000e+000
\$dbl is NOT equal to 3.0 !!

New FAQ:  Why is for() broken?

## Re: FAQ 4.2 Why is int() broken?

The question is what people who don't know the answer ask, not the

hp

## Re: FAQ 4.2 Why is int() broken?

"most probably" he didn't read these two words.

sln

## Re: FAQ 4.2 Why is int() broken?

Peter J. Holzer wrote:

Please do. In the overview of the questions. Where the answers are
missing. For example, top of the page at
<http://perldoc.perl.org/perlfaq4.html .

My complaint is that the *question* is meaningless.

--
Bart.

## Re: FAQ 4.2 Why is int() broken?

I'm not changing the question. It's one that people actually ask me in
classes. The question is the hook that gets them to read the answer.