"Surrogate infinity" in Perl?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default "Surrogate infinity" in Perl?



    In C, for example, one can use certain predefined constants as
    "surrogate infinity", meaning that no number (of the appropriate
    type) handled by a C program will ever be greater than the appropriate
    constant. For example:

    int min_val;

    ....

    min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
    for(i = 0; i < ITER; ++i) {
    if ( min_val > arr[i] ) {
    min_val = arr[i];
    }
    }

    Is there a similar facility in Perl?

    Thanks!

    -Jill
    J Krugman Guest

  2. Similar Questions and Discussions

    1. Proj cannot run on LCDS 2.6 ES due to "Unable to resolveresource bundle "datamanagement" for locale "en_US"
      hi, all, We have developped an application on Flex Build 3 (run successfully), but failed when we try to deploy it on Tomcat with LCDS 2.5 ES...
    2. "Devel::DProf" on a PERL script uses "Test::More"
      Dear all, I encountered a problem while run profiling on a script which uses "Test::More". May I ask whether anybody have some idea or wrap...
    3. CFINPUT type="radio" w/ "value" requires "label"
      On a Flash form, when you specify type='radio' and value='whatever', the value of the 'value' attribute will be displayed as a label if no 'label'...
    4. How do you simulate "." or "source" in a perl script ?
      On 7 Aug 2003 08:21:27 -0700 c_j_marshall@hotmail.com (Chris Marshall) wrote: Shell::Source perhaps this CPAN module will do it for you. ...
    5. "Start" "Program" "Menu" list is empty
      For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your...
  3. #2

    Default Re: "Surrogate infinity" in Perl?

    J Krugman wrote:
    > In C, for example, one can use certain predefined constants as
    > "surrogate infinity", meaning that no number (of the appropriate
    > type) handled by a C program will ever be greater than the appropriate
    > constant. For example:
    >
    > int min_val;
    >
    > ...
    >
    > min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
    > for(i = 0; i < ITER; ++i) {
    > if ( min_val > arr[i] ) {
    > min_val = arr[i];
    > }
    > }
    >
    > Is there a similar facility in Perl?
    Better:

    min_val = arr[0];
    for(i = 0; i < ITER; i++) {

    if (arr[i]) < min_val) min_val = arr[i];
    }

    or a perl way:

    my $min = shift @array;
    foreach my $value (@array) {

    $min = $value if $value < $min;
    }

    Why is the latter more readable? :-D.

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
    John web site hints: [url]http://johnbokma.com/websitedesign/[/url]

    John Bokma Guest

  4. #3

    Default Re: "Surrogate infinity" in Perl?

    John Bokma wrote:
    > min_val = arr[0];
    > for(i = 0; i < ITER; i++) {
    should read: i = 1 and assumes array has at least 2 values...




    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
    John web site hints: [url]http://johnbokma.com/websitedesign/[/url]

    John Bokma Guest

  5. #4

    Default Re: "Surrogate infinity" in Perl?

    J Krugman wrote:

    > int min_val;
    > min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
    > for(i = 0; i < ITER; ++i) {
    > if ( min_val > arr[i] ) {
    > min_val = arr[i];
    > }
    > }

    You need to review your thinking.

    Based on your incomplete C code snippet, your value
    for min_val will be set to the first number found
    in your array which is less than min_val. This
    new value of min_val will remain so, in lieu of
    further lesser values, defeating the purpose of
    continued iteration looping.

    Otherwords, min_val will be set to the lowest
    value in your array, and remain so.

    Are you trying to set min_val to the lowest value
    in your array, or trying to set min_val to the
    highest value which is also a lesser value than
    your constant INT_MAX?

    My Perl code examples beneath my signature exemplifies
    what I believe to be your logic, whatever this may be.


    Purl Gurl
    [url]http://www.purlgurl.net[/url]
    --

    #!perl

    use constant INT_MAX => 2;
    use constant ITER => 11;

    $minimum_value = INT_MAX;

    @Array = (0 .. 10);

    for ($iterate = 0; $iterate < ITER; $iterate++)
    {
    if ($minimum_value > $Array[$iterate])
    { $minimum_value = $Array[$iterate]; }
    }

    print $minimum_value;


    PRINTED RESULTS:
    ________________

    0


    **


    #!perl

    use constant INT_MAX => 2;
    use constant ITER => 11;

    $minimum_value = INT_MAX;

    @Array = (0 .. 10);

    for ($iterate = 0; $iterate < ITER; $iterate++)
    {
    if (INT_MAX > $Array[$iterate])
    { $minimum_value = $Array[$iterate]; }
    }

    print $minimum_value;


    PRINTED RESULTS:
    ________________

    1
    Purl Gurl Guest

  6. #5

    Default Re: "Surrogate infinity" in Perl?

    J Krugman wrote:
    >
    > In C, for example, one can use certain predefined constants as
    > "surrogate infinity", meaning that no number (of the appropriate
    > type) handled by a C program will ever be greater than the appropriate
    > constant. For example:
    >
    > int min_val;
    > ...
    >
    > min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
    > for(i = 0; i < ITER; ++i) {
    > if ( min_val > arr[i] ) {
    > min_val = arr[i];
    > }
    > }
    >
    > Is there a similar facility in Perl?

    use POSIX;

    my $min_val = INT_MAX;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  7. #6

    Default Re: "Surrogate infinity" in Perl?

    In <3F5CC5D3.136AD0FC@purlgurl.net> Purl Gurl <purlgurl@purlgurl.net> writes:
    >J Krugman wrote:
    >
    >
    >> int min_val;
    >> min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
    >> for(i = 0; i < ITER; ++i) {
    >> if ( min_val > arr[i] ) {
    >> min_val = arr[i];
    >> }
    >> }
    >
    >You need to review your thinking.
    >Based on your incomplete C code snippet, your value
    >for min_val will be set to the first number found
    >in your array which is less than min_val. This
    >new value of min_val will remain so, in lieu of
    >further lesser values, defeating the purpose of
    >continued iteration looping.
    Not so. "Further lesser values" will result in the if statement's
    test evaluating to true, and min_val being reset to the lower value.
    What's the problem?

    -Jill
    J Krugman Guest

  8. #7

    Default Re: "Surrogate infinity" in Perl?

    In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
    >use POSIX;
    >my $min_val = INT_MAX;
    That's the ticket. Thanks!

    -Jill

    J Krugman Guest

  9. #8

    Default Re: "Surrogate infinity" in Perl?

    J Krugman wrote:
    > Purl Gurl wrote:
    > > J Krugman wrote:
    (snipped)
    > >You need to review your thinking.
    > >Based on your incomplete C code snippet, your value
    > >for min_val will be set to the first number found
    > What's the problem?
    Your problem is twofold. Your original article is poorly
    written leaving a reader guessing at what you are doing.
    This subsequent article of yours, to which I am responding,
    carefully avoids issues addressed by me, in my first article.

    You need to review your thinking.


    Purl Gurl
    --
    Kick Ass Perl And Kick Ass Rock N Roll!
    [url]http://www.purlgurl.net[/url]
    Purl Gurl Guest

  10. #9

    Default Re: "Surrogate infinity" in Perl?

    J Krugman <jill_krugman@yahoo.com> wrote:

    I'm don't know much C, but I suspect your wanting to set INT_MAX comes
    from a desire to restrict yourself. Bondage could be implemented by
    tie()ing your variables. I'm not usually into that sort of thing,
    but I had to check it out:

    #!/usr/bin/perl

    use strict;
    use warnings;

    package Bondage;

    require Tie::Scalar;
    our @ISA = qw( Tie::StdScalar );

    my %MAX;

    sub TIESCALAR {
    my $class = shift;
    my $value;
    my $self = bless \$value, $class;
    $MAX{$self} = shift;
    $self;
    }

    sub STORE {
    my $self = shift;
    my $value = shift;
    if ( $value > $MAX{$self} ) {
    die "Overflow: $MAX{$self} < $value"
    }
    $$self = $value;
    $value;
    }

    package main;

    tie my $i, "Bondage", 5; # Overflow if $i > 5

    printf "i = %d\n", $i = $_ for (1..7);


    --
    Vlad
    Vlad Tepes Guest

  11. #10

    Default Re: "Surrogate infinity" in Perl?

    J Krugman <jill_krugman@yahoo.com> wrote in comp.lang.perl.misc:
    > In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
    >
    > >use POSIX;
    >
    > >my $min_val = INT_MAX;
    >
    > That's the ticket. Thanks!
    ....or not. INT_MAX is the maximum value a native integer can assume.
    Perl numbers (even integers) can be larger than that, because Perl
    switches to float point representation when needed.

    In fact, initializing a minimum to "some huge value" is only a cop-out.
    The right method is to initialize it to one of the values the minimum
    to be taken of.

    Anno
    Anno Siegel Guest

  12. #11

    Default Re: "Surrogate infinity" in Perl?

    J Krugman <jill_krugman@yahoo.com> wrote in comp.lang.perl.misc:
    > In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
    >
    > >use POSIX;
    >
    > >my $min_val = INT_MAX;
    >
    > That's the ticket. Thanks!
    ....or not. INT_MAX is the maximum value a native integer can assume.
    Perl numbers (even integers) can be larger than that, because Perl
    switches to float point representation when needed.

    In fact, initializing a minimum to "some huge value" is only a cop-out.
    The right method is to initialize it to one of the values the minimum
    is to be taken of.

    Anno

    Anno Siegel Guest

  13. #12

    Default Re: "Surrogate infinity" in Perl?

    Anno Siegel wrote:
    > J Krugman wrote:
    > > John W. Krahn wrote:
    (snipped)
    > > >use POSIX;
    > > >my $min_val = INT_MAX;
    > > That's the ticket. Thanks!
    > ...or not. INT_MAX is the maximum value a native integer can assume.
    > Perl numbers (even integers) can be larger than that, because Perl
    > switches to float point representation when needed.
    > In fact, initializing a minimum to "some huge value" is only a cop-out.
    > The right method is to initialize it to one of the values the minimum
    > to be taken of.

    You will find a good number of articles validating your comments here:

    [url]http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=INT_MAX+overflow&btnG=Google+Search[/url]


    A reader will discover problems with overflow, returns which are not
    of an integer value and problems of incorrect returns, such as a return
    being negative instead of positive. My hunch is INT_MAX will vary with
    operating system type and most likely, operating system version. I recall
    somewhere in time, specific versions of Linux exhibit INT_MAX problems.

    Use of POSIX is risky. This is evidenced by failure of "NMS" scripts
    to print a date, a simple task, under some operating systems, despite
    claims NMS is a source for "correctly" written Perl scripts.

    Siegel's comment of "cop-out" is an understatment. Using default INT_MAX
    as a value, is a poor programming practice, just as NMS' use of POSIX for
    date generation, is a poor programming practice.

    use constant INT_MAX => "a carefully selected value";


    Purl Gurl
    --
    [url]http://www.purlgurl.net[/url]
    Purl Gurl Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139