"Surrogate infinity" in Perl?

J

J Krugman

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 ) {
min_val = arr;
}
}

Is there a similar facility in Perl?

Thanks!

-Jill
 
J

John W. Krahn

J said:
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 ) {
min_val = arr;
}
}

Is there a similar facility in Perl?



use POSIX;

my $min_val = INT_MAX;



John
 
J

J Krugman

In said:
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 ) {
min_val = arr;
}
}


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
 
V

Vlad Tepes

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);
 
A

Anno Siegel

J Krugman said:
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
 
A

Anno Siegel

J Krugman said:
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
 
E

Eric Wilhelm

Purl Gurl said:
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 ) {
min_val = arr;
}
}

[...]

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


Thats a fairly idomatic way of finding the lowest value in an array. You
have a better way?


You should pull the first value off of the array for use as the current
minimum.

(I seem to be missing the start of this thread, so sorry if I'm
repeating.)

my $min = $arr[0];
for(my $i = 1; $i < @arr; $i++) {
($min > $arr[$i]) || ($min = $arr[$i]);
}

If you want to write this in C (as the above quoted snippet was), you
would of course want to verify that the array has a length, or you would get
some random value in your min_val variable when you do the following:

int min_val = arr[0];
for(i = 1; i < ITER; i++) {
if(min_val > arr) {
min_val = arr;
}
}

--Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top