biggest of 3 numbers

M

Michael Mair

Peter said:
Groovy hepcat Sundar was jivin' on 4 May 2006 06:07:26 -0700 in
comp.lang.c.
Re: biggest of 3 numbers's a cool scene! Dig it!


So does division of an int by a smaller int large enough to cause a
value of magnitude less than 1, such as 8/7 or 32/17.

Could you please explain that?
I would have expected a "round-down behaviour for negative
results" argument but I fail to see how 8/7 <= 1 in C.
Maybe it's just my not having had my caffeine fix yet...


Cheers
Michael
 
D

Dik T. Winter

> Dik T. Winter wrote: ....
>
> nice peace of code, got me a while that it comes from:
> max(a,b) = (a + b + abs(a - b))/2
>
> but using this the comparison is simple hidden in the
> abs statement, where a number is compared to zero. :)

Well, assuming those are ints:

unsigned int abs(int n)
{
unsigned int x = n;
unsigned int m;

m = (1 << (sizeof(int) - 1)) & x;
m = (m - (m >> 31)) | m;
return (x ^ m) + (1 & m);
}

Where is the comparison? But, whatever, abs is standard already in C89,
and I have no idea how it is implemented... But of course, there is
the probability of overflow lurking in that formula.
 
R

Richard Bos

Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in


And your C question is...?
Find the bigger of three numbers by finding the bigger of two
numbers, one of which is itself the bigger of two numbers. Find the
bigger of two numbers by subtracting one from the other and
determining whether the result is greater than zero or not.

#define BIGGER_OF_TWO(x,y) ((x) - (y) > 0 ? (x) : (y))

0 is not a number these days?

Richard
 
F

Flash Gordon

Richard said:
0 is not a number these days?

It doesn't matter which you return if the difference is 0, and the
suggested expression will return one of the numbers in that case. So I
don't see your point.
 
D

Dik T. Winter

>
> Well, assuming those are ints:
>
> unsigned int abs(int n)
> {
> unsigned int x = n;
> unsigned int m;
>
> m = (1 << (sizeof(int) - 1)) & x;
> m = (m - (m >> 31)) | m;

Should have been
m = (m - (m >> (sizeof(int) - 1))) | m;
 
R

Richard Bos

Flash Gordon said:
It doesn't matter which you return if the difference is 0, and the
suggested expression will return one of the numbers in that case. So I
don't see your point.

The requirement was "without comparing numbers". (Ok, as written it was
"without comaprimg numbers", but...) Zero is a number. Comparing
something to zero falls under comparing numbers, and is therefore non
grata in this homework assignment.

Richard
 
F

Flash Gordon

Richard said:
The requirement was "without comparing numbers". (Ok, as written it was
"without comaprimg numbers", but...) Zero is a number. Comparing
something to zero falls under comparing numbers, and is therefore non
grata in this homework assignment.

OK, I missed the point you were making. I agree with you.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
P

Peter Shaggy Haywood

Groovy hepcat Michael Mair was jivin' on Sun, 07 May 2006 08:28:48
+0200 in comp.lang.c.
Re: biggest of 3 numbers's a cool scene! Dig it!
Could you please explain that?
I would have expected a "round-down behaviour for negative
results" argument but I fail to see how 8/7 <= 1 in C.
Maybe it's just my not having had my caffeine fix yet...

I don't know what I'm talking about. I really gotta get some sleep.
Well, that's my excuse anyhow, and I'm sticking to it. :)

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
V

vim

If u compare the result( subtraction of two numbers ) with 0.Then it it
violates the condition.
There should be no comparision
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top