Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I guess I am way too new to this... can someone tell me where I can find an answer to my answer. I read the following:

Perl computes only with double-precision floating-point values.[2] This means that there are no integer values internal to Perl; an integer constant in the program is treated as the equivalent floating-point value.[3] You probably won't notice the conversion (or care much), but you should stop looking for integer operations (as opposed to floating-point operations), because there aren't any.

[2] A "double-precision floating-point value" is whatever the C compiler that compiled Perl used for a double declaration.

[3] Unless you use "integer mode," but that's not on by default.

And I am trying to wrap my little brain around numbers/scalar data but this is not working... Please. Thanks for the patience - newbie

Janitored by davido: Added formatting to match user's input, and used code tags to prevent square brackets from being interpreted as links.

20040928 Edit by []: Changed title from 'Don't understand, what this means'

Replies are listed 'Best First'.
Re: Confusion Regarding Perl's Numerical Handling
by graff (Chancellor) on Sep 28, 2004 at 02:12 UTC
    If you've had prior experience with other programming languages (like C, FORTRAN or assembler), you know that there are basically two different ways of storing numeric values (i.e. two different types of numeric data): integer and floating point, and that there are fundamental differences in the cpu operations that perform arithmetic on these two types (integer operations are "exact" in value but limited in range, floating point ops are relatively "approximate" in value but relatively unlimited in range).

    As you use more bits to store integer numbers (lots of cpus are using 64 bits these days), you get to do arithmetic over a larger range, and it will always be exact in the integer domain (where fractional values do not exist). As you use more bits to store floating-point numbers (cpus have been supporting 64-bit floats for a long time now), you get to do arithmetic over a much larger range with greater accuracy.

    These considerations used to demand more attention from programmers, back when it was a lot harder, slower and/or more expensive to use more bits for numbers. These days, it still matters to some people who handle values or differences of extremely large or extremely small magnitude, but for most folks, it's just not an issue any more. With all arithmetic being done in 64-bit floating point, you don't start losing exactness in the integer domain until you get up around 2**50. And although speed of computation can be an issue, usually it is not (and in the rare cases where it is a killer issue, you shouldn't be doing the arithmetic in Perl anyway -- use C instead).

    Bottom line: don't worry about it. If you want things like "13/5" to produce an integer result, just do "int( 13/5 )".

    20040928 Edit by Corion: Changed title from 'Re: Don't understand, what this means'

•Re: Confusion Regarding Perl's Numerical Handling
by merlyn (Sage) on Sep 27, 2004 at 23:30 UTC
    You'll need to be more specific. What parts do you understand? What parts don't you understand? I mean, do we need to explain the word "integer", or can we start somewhere in the middle?

    And since that text is obviously from my Learning Perl book, I need to ask if you meet the prerequisites of the book. What other languages have you already programmed in?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

    20040928 Edit by Corion: Changed title from '•Re: Don't understand, what this means'

Re: Confusion Regarding Perl's Numerical Handling
by ikegami (Patriarch) on Sep 28, 2004 at 01:40 UTC
    Floating point numbers can hold integers of a certain size with no loss of precision. a double can safely contain a 32 bit integer. The downsides are that they take up more space and that they are a bit slower. For most users, that will never be an issue. If it is, look into use integers. I don't know the details since I've never had to use it.

    20040928 Edit by Corion: Changed title from 'Re: Don't understand, what this means'