Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > find out the problem

Reply
Thread Tools

find out the problem

 
 
Gordon Burditt
Guest
Posts: n/a
 
      08-02-2005
>> The code:
>>
>> #include <stdio.h>
>> int x(int a)
>> {
>> printf("%d: %lx\n", a, 0x12345678 >> a);
>> return 0;
>> }
>> int main(void)
>> {
>> x(36);
>> x(32);
>> x(0);
>> return 0;
>> }
>>
>> The output (annotated to the right):
>> 36: 1234567 Same as a shift of 4
>> 32: 12345678 Same as no shift at all
>> 0: 12345678

>
>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

 
Reply With Quote
 
 
 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      08-03-2005
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"?
 
Reply With Quote
 
 
 
 
Jasen Betts
Guest
Posts: n/a
 
      08-03-2005
In article <(E-Mail Removed) .com>, sabarish wrote:
> 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);


--

Bye.
Jasen
 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      08-03-2005

Alexei A. Frounze wrote:
> "Krishanu Debnath" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> ...
> > > long Max(long x, long y)
> > > {
> > > long res;
> > > res = (x-y) >> BITS_IN_LONG; /* res=0 (all zeroes) or -1 (all ones) */

> >
> > It invokes UB. C99 6.5.7p3
> >
> > "If the value of the right operand is negative or is greater than or
> > equal to the width of the promoted left operand, the behavior is
> > undefined."

>
> 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

 
Reply With Quote
 
sabarish
Guest
Posts: n/a
 
      08-03-2005
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.

 
Reply With Quote
 
sabarish
Guest
Posts: n/a
 
      08-03-2005
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 9.
can u help me, my id is http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      08-03-2005
Jasen Betts wrote:
> In article <(E-Mail Removed) .com>, sabarish wrote:
>
>>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);


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
 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      08-03-2005
sabarish wrote:
> 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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      08-03-2005
In article <(E-Mail Removed) .com>,
sabarish <(E-Mail Removed)> wrote:
>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.

--
Entropy is the logarithm of probability -- Boltzmann
 
Reply With Quote
 
Guillaume
Guest
Posts: n/a
 
      08-03-2005
akarl wrote:
> sabarish wrote:
>
>> Hi to all. find out the biggest among two numbers without using any
>> conditional statements and any relational operators.

>
> 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 )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find it, cut it out, find next oldyork90 Perl Misc 5 01-22-2012 08:22 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Slow Find.find - real problem Stuart Clarke Ruby 7 09-06-2010 02:47 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments