find out the problem

G

Gordon Burditt

The code:
That all is correct, no doubt, however, my personal opinion is that that's
wrong to take the shift count and blindly feed it to the asm instruction
with such a limitation.

ANSI C says it's ok. It probably does that because lots of compilers,
I suspect from the PDP-11 on, did just that. Personally, I agree
with you; out-of-range counts should cause core dumps. But the
speed freaks don't like it.
It's as illogical as the need to cast one of two
integer multipliers to long integer in order to get the correct long integer
product.

I don't agree that the cast is illogical. And dealing with this
doesn't have to generate inefficient code in the cases where it's
NOT needed, unlike the shift case, which slows down all variable shifts.
That strikes the math guys in the back. Anyway, I was talking about
a slightly different problem of the shift -- whether or not it's SAR-like or
something else, especially something very odd, probably not any kind of
shift at all.

I have heard of some peculiar cases where shift used rotate instead
(there were no shift instructions on that machine) and some masking
was used which went a bit haywire when the count was out of range.
I don't remember what machine it was, though.

Gordon L. Burditt
 
P

Peter Shaggy Haywood

Groovy hepcat sabarish was jivin' on 1 Aug 2005 07:24:07 -0700 in
comp.lang.c.
find out the problem's a cool scene! Dig it!
Hi to all. find out the biggest among two numbers without using any
conditional statements and any relational operators.

No.

But don't let me stop you from doing so, if you wish. If you have
any problems with it, ask specific questions, including your code (or
the smallest *complete* program that shows the problems) in the post,
and we'll endeavour to answer your queries and help you fix the code.

--

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"?
 
J

Jasen Betts

Hi to all. find out the biggest among two numbers without using any
conditional statements and any relational operators.

#include <math.h>
float a,b,biggest;
//...
biggest=a+b-fabs(a-b);
 
K

Krishanu Debnath

Alexei said:
...

I know it's undefined *by standard*. But it's pretty much well defined by so
many compilers out there... Can one name a compiler that issues an error at

[x0030819 dcesparc017 SunOS c]: cat nonsense.c
#include<limits.h>
#include<stdio.h>

int main()
{
int number = -4;
number = number >> ( CHAR_BIT * sizeof number);
printf(" number %d \n", number);
return 0;
}
[x0030819 dcesparc017 SunOS c]:
[x0030819 dcesparc017 SunOS c]: gcc -dumpversion
3.4.2
[x0030819 dcesparc017 SunOS c]: gcc -ansi -pedantic -W -Wall nonsense.c
nonsense.c: In function `main':
nonsense.c:7: warning: right shift count >= width of type
[x0030819 dcesparc017 SunOS c]: ./a.out
number -4
[x0030819 dcesparc017 SunOS c]: uname -a
SunOS dcesparc017 5.8 Generic_108528-21 sun4u sparc SUNW,Ultra-5_10
[x0030819 dcesparc017 SunOS c]:
that shift operator or yields garbage or freaks out? Can anyone name one
that doesn't extend/propagate the sign bit to the right? I'm asking out of
curiousity, since 6 or 7 compilers that I know would handle that just right
(on about 5 entirely different CPUs, ix86 and several non-ix86).

Alex

Krishanu
 
S

sabarish

yes

This question is raised by mnc Companies......

and i solved this problem


main()
{
int a=10;
int b=20;

printf("%d",b+( ((a-b)+fabs(a-b))/2) );
}

thats it.
 
S

sabarish

Thank u

you r absolutely correct sir


This is sabarish nice to meet to u and i have one more problem how can
i connect to the database through c Program(database is Oracle,
Operating system is windows 98).
can u help me, my id is (e-mail address removed)
 
A

akarl

Jasen said:
#include <math.h>
float a,b,biggest;
//...
biggest=a+b-fabs(a-b);

Yeah, right:

$ cat test.c
#include <math.h>
#include <stdio.h>

int main(void)
{
double a = 2, b = 3, max;

max = a + b - fabs(a - b);
printf("The maximum value of %f and %f is %f.\n", a, b, max);
return 0;
}

$ gcc test.c -o test

$ ./test
The maximum value of 2.000000 and 3.000000 is 4.000000.


August
 
A

akarl

sabarish said:
yes

This question is raised by mnc Companies......

and i solved this problem


main()
{
int a=10;
int b=20;

printf("%d",b+( ((a-b)+fabs(a-b))/2) );
}

thats it.

You obviously didn't read my first post in this thread.

August
 
W

Walter Roberson

This question is raised by mnc Companies......

I don't believe I recognize "mnc Companies" ?

and i solved this problem
main()
{
int a=10;
int b=20;

printf("%d",b+( ((a-b)+fabs(a-b))/2) );
}
thats it.

Improper declaration of main() for C89 or C99. Missing return value
of main for C89.

fabs() works on doubles, so fabs(a-b) will be a double and
that will be added to the int (a-b), producing a double. The
2 in the denominator will therefore be promoted to a double,
hence there will be a floating-point division, not an integer division.
The result will be a double, so the b of the addition will be promoted
to a double for the addition, with a double as a result of the expression.
It is not certain that the result of this expression will be
an exact representation of an integer, especially if one of the values
is close to the maximum or minimum integer -- you might get something
very -close- to an integer.

You then attempt to print that double as an integer without any
casting, and the result of that is going to depend upon the
implementation; in some implementations, it might print a random
value as floating point numbers are not always passed into routines the
same way that integral arguments are.

Your routine also breaks down if you get overflow in the a-b
portion, which could happen if b is a large negative number.
 
G

Guillaume

akarl said:
It's simple as well as off topic (since it's not C specific):

max(x, y) = (x + y + |x - y|) / 2

Is it?
Implementation-wise, apart from the problematic cases, as Eric pointed
out, there is another issue. The absolute value. How do you compute it
with no conditional statement? Just using fabs() or some other
library function doesn't make it any better. There *has* to be some
conditional statement hidden somewhere. (Or else, tell me how you do
it.) So this doesn't solve the problem which says "without using any
conditional statements and any relational operators".

Ok, some implementations of fabs() may not need a conditional
statement (it may be able to modify the sign without any condition),
but that's not guaranteed.

And to me, formally speaking, an absolute value is a conditional
statement in itself.

Anyway, the question looked like YASHWA to me.
(= Yet Another Stupid HomeWork Assignment ;-) )
 
A

akarl

Guillaume said:
Is it?
Implementation-wise, apart from the problematic cases, as Eric pointed
out, there is another issue. The absolute value. How do you compute it
with no conditional statement? Just using fabs() or some other
library function doesn't make it any better. There *has* to be some
conditional statement hidden somewhere. (Or else, tell me how you do
it.) So this doesn't solve the problem which says "without using any
conditional statements and any relational operators".

Ok, some implementations of fabs() may not need a conditional
statement (it may be able to modify the sign without any condition),
but that's not guaranteed.

And to me, formally speaking, an absolute value is a conditional
statement in itself.

Anyway, the question looked like YASHWA to me.
(= Yet Another Stupid HomeWork Assignment ;-) )

Yes, I see your point. On the other hand it seems like the formula I
presented was what sabarish was looking for.
 
C

Chris Croughton

Is it?
Implementation-wise, apart from the problematic cases, as Eric pointed
out, there is another issue. The absolute value. How do you compute it
with no conditional statement? Just using fabs() or some other
library function doesn't make it any better. There *has* to be some
conditional statement hidden somewhere. (Or else, tell me how you do
it.) So this doesn't solve the problem which says "without using any
conditional statements and any relational operators".

abs(x) = sqrt(x*x)

Assuming 'perfect' arithmetic (no precision or limits problems) of
course (and assuming that sqrt(x) means "the positive square root of x",
but the latter is a given in standard C at least).
Anyway, the question looked like YASHWA to me.
(= Yet Another Stupid HomeWork Assignment ;-) )

Indeed. But by now the assignment has probably been done...

Chris C
 
A

Alexei A. Frounze

....
[x0030819 dcesparc017 SunOS c]: cat nonsense.c
#include<limits.h>
#include<stdio.h>

int main()
{
int number = -4;
number = number >> ( CHAR_BIT * sizeof number);

I did not mean the issue with the shift amount. What's the output if you
just shift by *1* position?

Alex
 
A

Alexei A. Frounze

....
abs(x) = sqrt(x*x)

Anyway, computing the square root normally involves branches, at least to
make up the loop in which the value is computed, but I'm sure there're many
more inside, so, apparently, it's no good -- the branches are still
somewhere inside :)

Alex
 
W

Walter Roberson

Anyway, computing the square root normally involves branches, at least to
make up the loop in which the value is computed, but I'm sure there're many
more inside, so, apparently, it's no good -- the branches are still
somewhere inside :)

Hardware square root instructions are pretty common; there are
different implementations, and I don't know whether -all- of them require
conditional branches (which implies a microcode execution, whereas
a big transistor mess would just -do- the computation.)

If one were using a hardware square root instruction, the terms
of the original posting would be satisfied -- the instruction -itself-
would not be conditional nor relational. It would be valid for
a standard math library implementation of sqrt() to use a hardware
square root instruction "behind the scenes".


Someone mentioned that absolute value requires conditional tests.
That's not the case for the IEEE standard representation of floating point
values, which has a sign bit that could be masked off with a
bitwise-and . This does get us back, though, to the small question
of what the data types are that the algorithm is required to handle.

Mind you, there is a two's-complement integer absolute value without
conditionals: e.g. something like

(signed long)((x) & LONG_MIN) +
(signed long)(((unsigned long)(x) & LONG_MIN) > (CHAR_BIT * sizeof long - 1))


Of course, just to detect whether you are 1's complement or 2's is
likely going to get you into conditionals...
 
K

Keith Thompson

I don't believe I recognize "mnc Companies" ?





Improper declaration of main() for C89 or C99. Missing return value
of main for C89.

fabs() works on doubles, so fabs(a-b) will be a double and
that will be added to the int (a-b), producing a double.

fabs() returns a double if you have a #include <math.h>. If you
don't, it invokes undefined behavior.

[snip]
You then attempt to print that double as an integer without any
casting, and the result of that is going to depend upon the
implementation; in some implementations, it might print a random
value as floating point numbers are not always passed into routines the
same way that integral arguments are.

Calling printf() without a valid prototype (preferably via #include
<stdio.h>) invokes undefined behavior. If that's corrected, passing a
double value to printf() with a "%d" format also invokes undefined
behavior. It might print a random value by tattooing it across your
forehead (unlikely, but allowed by the standard).
Your routine also breaks down if you get overflow in the a-b
portion, which could happen if b is a large negative number.

Yes.
 
O

Old Wolf

Alexei said:
...

I know it's undefined *by standard*. But it's pretty much well defined by so
many compilers out there... Can one name a compiler that issues an error at
that shift operator or yields garbage or freaks out?

GCC 3, x86 (pretty uncommon, I know...)

Using your exact sample function, Max(-2, -2) gives 6,
and Max(2, 1) gives 3.

There is even a compiler warning:
gcc h.c -o h
h.c: In function `Max':
h.c:6: warning: right shift count >= width of type

Can you name one compiler that does operate your code correctly,
out of interest?
 
C

Chris Croughton

...

Anyway, computing the square root normally involves branches, at least to
make up the loop in which the value is computed, but I'm sure there're many
more inside, so, apparently, it's no good -- the branches are still
somewhere inside :)

Not necessarily, you can unroll the loop to any desired precision (as I
recall you get at least one bit improvement in accuracy for each
Newton-Raphson iteration, so repeating it 128 times should be sufficient
on most systems). But some floating point units do it in hardware...

Chris C
 
A

Alexei A. Frounze

Old Wolf said:
GCC 3, x86 (pretty uncommon, I know...)

Using your exact sample function, Max(-2, -2) gives 6,
and Max(2, 1) gives 3.

There is even a compiler warning:
gcc h.c -o h
h.c: In function `Max':
h.c:6: warning: right shift count >= width of type

Can you name one compiler that does operate your code correctly,
out of interest?

Oops, I'm sorry, I obviously meant a right shift by *31*, not 32:

#define BITS_IN_LONG 32

long Max(long x, long y)
{
long res;
res = (x-y) >> (BITS_IN_LONG-1); /* res=0 (all zeroes) or -1 (all ones) */
res = (x & ~res) | (y & res); /* depending on prev value of res, we just
select/multiplex x or y by means of bitwise masking */
return res;
}

That works just fine on gcc 3.3.4 on x86 and gives for your pairs of
numbers:
-2
2

Compiles w/o a warning with -Wall.

Alex
 
K

Krishanu Debnath

Alexei said:
...
[x0030819 dcesparc017 SunOS c]: cat nonsense.c
#include<limits.h>
#include<stdio.h>

int main()
{
int number = -4;
number = number >> ( CHAR_BIT * sizeof number);

I did not mean the issue with the shift amount. What's the output if you
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No, my crystal ball is not on strike, actuall I don't have any. I can
only
infer from what you have posted. The whole objection came up with the
shift
expression where value of the RHS is same as the width of the LHS.

Okay, I just saw your reply on Old Wolf's post. You almost took 24 hr
to
realize this.
just shift by *1* position?

Alex

Krishanu
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top