Re: Division/math bug in perl?

From: Alfred Z. Newmane (a.newmane.remove_at_eastcoastcz.com)
Date: 02/24/05


Date: Thu, 24 Feb 2005 14:36:13 -0800

darkon wrote:
> Snail <snail@localhost.com> wrote:
>
>> Why is this:
>>
>> $ perl -e 'print (int (-2.6), "\n")'
>> -2
>>
>> Shouldn't it be -3? I thought converting from float to int is
>
> int() isn't really converting, it's just returning the integer part
> of the number. See the docs.
>
>> supposed to give the integer part, which is -3, and not round
>
> But the integer part of -2.6 is -2, not -3.

Not mathematically it isn't. I should be -3. Think of it like this. The
int part of 2.6 is 2, which is the /lowest/ number before the next
integer on the number line. Applying this to -2.6, the /lowest/ number
before the next integer is -3.

Maybe I can explain this better with an illustration:

Float: 1 .5 2 .5 3
                                       2.6
        | - - - - + - - - - | - - - - + * - - - |
Int: |-----------------| |------------------|
        1 2

So in the other direction on the same number line:

Float: -3 .5 -2 .5 -1
              -2.6
        | - - - * + - - - - | - - - - + - - - - |
Int: |-----------------| |-----------------|
       -3 -2

Or think of it like this: -2.6 is the same offset from -3 as +2.4 is
from 2. In other words, -2.6 and 2.4 are in the same realative postion
from the lower integral point. (Is there a better word for that?)

>> towards zero, as it seems to be doing, resulting in -2? For that
>> matter, why does c/c++ do this too?
>
> I don't know about c/c++, but this behaviour is explicitly documented
> for Perl's int():

Actually this behavior is present in C and C++. It's just the way
integer division works internally. It effectively rounds towards 0
instead of down to the lower end of the integer on the number line.

I've never quite understood why it's like it is, in C and C++, one of
side effects is the irregular (from a math perspective) behavior of the
modulus operator (%.)

I agree that it's nice that Perl's % operator does not suffer from this
mathematical short coming. ;-)



Relevant Pages

  • Re: rand and srand
    ... 2000, the code to do that uses the modulus operator, thusly: ... void srand(unsigned int seed) ... int ret, c; ... ", stdout); ...
    (comp.lang.c)
  • Re: [C] [newbie] problem reading from file
    ... why did you declare two variables in one statement here? ... note that it returns an int, ... actually I would consider converting all this inner loop to a for-loop ... that places each input character into number as long as there is still ...
    (alt.comp.lang.learn.c-cpp)
  • Re: need to convert a char to an hexadecmial value
    ... I needed help in converting a character to the correspoding ... For characters with values less than 16, you print a single digit, ... only an insigificant amount of data space, ... Just use int. ...
    (comp.lang.c)
  • Re: Safe conversion from floating real to integer (and vice versa)
    ... > But it is not *possible* to get a "real type Tf, ... >>greater than that of int. ... >>to double and comparing to the value which is to be converted. ... DBL_MAX_10_EXP only gives information about what values can be represented as a double, but not as an int so I don't see how it relates to the problem of converting from double to int. ...
    (comp.lang.c)
  • Re: AIX C compiler message unexplainable
    ... converting this large value to an `int' will yield ... signed and unsigned types always have defined behaviour and be reversible. ... Converting a signed type to unsigned and back to signed should produce the ... same value, and vice-versa, and should not alter its representation. ...
    (comp.lang.c)