Re: Compare two signed integers without using comparision operator in perl
by chromatic (Archbishop) on May 06, 2003 at 18:49 UTC
|
my @ints = ( int( rand() * 100 ), int( rand() * 100 ) );
print "Which is larger?\n1) $ints[0]\n2)$ints[1]?\n";
chomp( my $input = <STDIN> );
print "$ints[ $input ] is larger.\n";
| [reply] [d/l] |
|
|
I read this, did a double take, then laughed out loud. :-)
| [reply] |
Re: Compare two signed integers without using comparision operator in perl
by bart (Canon) on May 06, 2003 at 18:59 UTC
|
Subtract them and check the sign of the result.
To do the latter without a comparison operator, check equality to "0", or test if it starts with a "-" character, for example with a regexp, or with substr. | [reply] |
Re: Compare two signed integers without using comparision operator in perl
by dws (Chancellor) on May 06, 2003 at 18:55 UTC
|
How to compare two signed integers without using the comparision operators in perl?
You can compare two integers for equality by exploiting the properties of "exclusive or" (^). See perlop.
| [reply] |
Re: Compare two signed integers without using comparision operator in perl (no sign()?)
by tye (Sage) on May 06, 2003 at 19:25 UTC
|
print $a.(qw(< == >))[1+eval{($a-$b)/abs($a-$b)}].$b,$/;
- tye | [reply] [d/l] |
Re: Compare two signed integers without using comparision operator in perl
by Util (Priest) on May 07, 2003 at 00:25 UTC
|
print+('equal','a is smaller','b is smaller')[length($a-$b)-length($b-$a)];
or
print 1x($a-$b)?'b is smaller':1x($b-$a)?'a is smaller':'equal';
| [reply] [d/l] [select] |
Re: Compare two signed integers without using comparision operator in perl
by Improv (Pilgrim) on May 06, 2003 at 18:59 UTC
|
$a = 3;
$b = 4;
print (sqrt(($a-$b)**2) - ($a-$b));
Reasoning:
A-B is positive or negative (assume disequal)
negative numbers, squared, and then rooted, disequal themselves
| [reply] [d/l] |
|
|
print abs($a-$b) - ($a-$b);
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
Re: Compare two signed integers without using comparision operator in perl
by dragonchild (Archbishop) on May 06, 2003 at 19:09 UTC
|
Explain what it is you're trying to do. Very often, developers come to Perl from another language and they know how to do what they want to do, in that other language. While Perl often supports a multitude of ways to do something, some ways are often more appropriate than others. Without knowing what you're trying to accomplish, we can't tell you if, for example, there is a module that does the entirety of what you're trying to do, plus more. (That's happened to me more than once ...)
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
|
|
| [reply] |
Re: Compare two signed integers without using comparision operator in perl
by Louis_Wu (Chaplain) on May 06, 2003 at 19:01 UTC
|
How to compare two signed integers without using the comparision operators in perl.
So what's a signed integer? I know of scalars, and a few ways to force a scalar to be considered some sort of integer, but why "signed" integer? Perl doesn't care.
Perl programming and scheduling in the corporate world, as explained by dragonchild:
"Uhh ... that'll take me three weeks, broken down as follows: 1 day for coding, the rest for meetings to explain why I only need 1 day for coding."
| [reply] |
|
|
You can use string comparison on positive, unsigned integers, or even unsigned floating point numbers, if you make sure the number of digits left of the decimal point (if any) are the same for all numbers. Thus, after using sprintf with a proper template.
This doesn't work if at least one of the numbers is negative.
I don't think it matters for this particular case, as string comparison is comparison nevertheless, but still... It's the only reason I can think of why the sign would matter.
p.s. It's even an excellent way to compare groups of strings/numbers in one comparison, for example, for sort.
| [reply] |
Re: Compare two signed integers without using comparision operator in perl
by BrowserUk (Patriarch) on May 06, 2003 at 23:11 UTC
|
print $a, ' is ', ('smaller', 'equal', 'greater')[1+($a<=>$b)], ' than
+ ', $b;
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
| [reply] [d/l] |
|
|
FWIW, the in the OP's clarification of this question, the list of excluded operators was ==,<,>. The way I read the clarification and the original question, I believe that <=> was not excluded, and could well have been what the OP was looking for, rather than the mathematical monstrocitoes offered.
C'est la vie.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
| [reply] [d/l] |
Re: Compare two signed integers without using comparision operator in perl
by artist (Parson) on May 06, 2003 at 18:47 UTC
|
| [reply] |
|
|
It is not a practical problem, it is just a quiz to rewrite the following code:
($a == $b) ? print "equal" : ( ($a < $b) ? print "a is small" : print
+ "b is small");
without using the comparision operators like ==,<,> .
| [reply] [d/l] |