By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Interesting code

P: n/a
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Nov 16 '06 #1
Share this Question
Share on Google+
26 Replies


P: n/a
Can u tell me what is interesting in this code ??
v4vijayakumar wrote:
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}
Nov 16 '06 #2

P: n/a
Dh*********@gmail.com top-posted, but I've fixed that. He said:
v4vijayakumar wrote:
>Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Can u tell me what is interesting in this code ??
No, there isn't any regular comp.lang.c contributor called "u".
(Translation: abbreviating "you" to "u" marks you out as someone who can't
even spell "you" properly. Take the time to type "you" in full, in the
interests of clear communication and of not looking idiotic.)

Here are the principal points of interest:

1) function name choice invades implementation namespace, thus invoking
undefined behaviour;
2) function name choice gives a misleading impression of what the function
tries to do;
3) t's value is indeterminate, but the function still attempts to use t's
value regardless, thus invoking undefined behaviour;
4) a parameter is used solely for assigning a value to, and this value will
be lost at the same time that the parameter itself is lost, at the end of
this function;
5) an indeterminate value is returned from this function, thus invoking
undefined behaviour in any caller that evaluates it;
6) even if all those faults were fixed, the function would still need major
repair work (type choices, range validation, etc).

Summary: it was written by someone who didn't know C very well six or seven
years ago. Whether they have learned their lessons in the meantime, I
cannot say.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 16 '06 #3

P: n/a
"Dh*********@gmail.com" <Dh*********@gmail.comwrites:
v4vijayakumar wrote:
>int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Can u tell me what is interesting in this code ??
Just the misconception that it illustrates.
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Nov 16 '06 #4

P: n/a

Richard Heathfield wrote:
Dh*********@gmail.com top-posted, but I've fixed that. He said:
v4vijayakumar wrote:
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}
Can u tell me what is interesting in this code ??

No, there isn't any regular comp.lang.c contributor called "u".
(Translation: abbreviating "you" to "u" marks you out as someone who can't
even spell "you" properly. Take the time to type "you" in full, in the
interests of clear communication and of not looking idiotic.)

Here are the principal points of interest:

1) function name choice invades implementation namespace, thus invoking
undefined behaviour;
2) function name choice gives a misleading impression of what the function
tries to do;
3) t's value is indeterminate, but the function still attempts to use t's
value regardless, thus invoking undefined behaviour;
4) a parameter is used solely for assigning a value to, and this value will
be lost at the same time that the parameter itself is lost, at the end of
this function;
5) an indeterminate value is returned from this function, thus invoking
undefined behaviour in any caller that evaluates it;
6) even if all those faults were fixed, the function would still need major
repair work (type choices, range validation, etc).

Summary: it was written by someone who didn't know C very well six or seven
years ago. Whether they have learned their lessons in the meantime, I
cannot say.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
You take the fun out of the post. The post was not asking any help or
correction in the code. It is just for fun.

I wrote (1999) this to find square root of a number. I was then new to
c. I got wrong results and didn't know why it was not working. Then,
someone told me about the sqrt library function. Anyhow I saved the
code for future reference (?) and I forgot about it. Suddenly happened
to see the code and recollected the past.

Nov 16 '06 #5

P: n/a
Ben Pfaff <bl*@cs.stanford.eduwrites:
"Dh*********@gmail.com" <Dh*********@gmail.comwrites:
>v4vijayakumar wrote:
>>int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Can u tell me what is interesting in this code ??

Just the misconception that it illustrates.
Um, which one?

Apart from the multiple instances of undefined behavior, it's amusing
that the value it returns is (likely to be) the square root of the
parameter "no" -- though the value of the parameter "no" bears no
relationship to the argument with which the function was called.

I suppose the author assumed that an assigment statement causes the
left and right hand sides to become equal (by magic if necessary),
rather than merely evaluating the right operand and copying its value
to the object designated by the left operand. In other words, that
no = t * t;
would cause no to become equal t * t by changing the value of t;
obviously the way to do this is to set t to the square root of no.

To someone who knows what "=" means in mathematics, but hasn't been
exposed to the concept of assignment, it's not an entirely insane
misconception.

(Exercise: Design and implement a language that really works this
way.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 16 '06 #6

P: n/a
v4vijayakumar said:
You take the fun out of the post.
What fun?
The post was not asking any help or correction in the code.
I didn't say it was. But someone asked why it was interesting, so I listed
the principal points of interest.
I wrote (1999) this to find square root of a number.
Oh deary deary me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 16 '06 #7

P: n/a
Keith Thompson <ks***@mib.orgwrote:
Ben Pfaff <bl*@cs.stanford.eduwrites:
"Dh*********@gmail.com" <Dh*********@gmail.comwrites:
v4vijayakumar wrote:
int sqrt(int no)
{
int t;
no = t * t;
return t;
}
I suppose the author assumed that an assigment statement causes the
left and right hand sides to become equal (by magic if necessary),
rather than merely evaluating the right operand and copying its value
to the object designated by the left operand. In other words, that
no = t * t;
would cause no to become equal t * t by changing the value of t;
obviously the way to do this is to set t to the square root of no.
(Exercise: Design and implement a language that really works this
way.)
Too late. Prolog already exists.

Richard
Nov 16 '06 #8

P: n/a
Richard Bos wrote:
Keith Thompson <ks***@mib.orgwrote:
>Ben Pfaff <bl*@cs.stanford.eduwrites:
>>"Dh*********@gmail.com" <Dh*********@gmail.comwrites:
v4vijayakumar wrote:
int sqrt(int no)
{
int t;
no = t * t;
return t;
}
>I suppose the author assumed that an assigment statement causes the
left and right hand sides to become equal (by magic if necessary),
rather than merely evaluating the right operand and copying its value
to the object designated by the left operand. In other words, that
no = t * t;
would cause no to become equal t * t by changing the value of t;
obviously the way to do this is to set t to the square root of no.
>(Exercise: Design and implement a language that really works this
way.)

Too late. Prolog already exists.
You beat me to it.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Nov 16 '06 #9

P: n/a
In article <45****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>(Exercise: Design and implement a language that really works this
way.)
>Too late. Prolog already exists.
Prolog doesn't really do this.

If you write a predicate carefully, it may well be possible to call it
with various combinations of uninstantiated variables. So for example
append(X, Y, [1,2,3]) will generate pairs of lists whose concatenation
is the list [1,2,3]. But it doesn't do general arithmetic solving -
10 is X*X will not find the square root of 10.

On the other hand, several spreadsheets do provide this, allowing you
to ask "how many units must we sell to make $1m profit?", or perhaps
adjust your sales figures to match what you told the tax office about
your profits.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 16 '06 #10

P: n/a
Keith Thompson wrote:
Ben Pfaff <bl*@cs.stanford.eduwrites:
>"Dh*********@gmail.com" <Dh*********@gmail.comwrites:
>>v4vijayakumar wrote:
int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Can u tell me what is interesting in this code ??

Just the misconception that it illustrates.

Um, which one?

Apart from the multiple instances of undefined behavior, it's amusing
that the value it returns is (likely to be) the square root of the
parameter "no" -- though the value of the parameter "no" bears no
relationship to the argument with which the function was called.

I suppose the author assumed that an assigment statement causes the
left and right hand sides to become equal (by magic if necessary),
rather than merely evaluating the right operand and copying its value
to the object designated by the left operand. In other words, that
no = t * t;
would cause no to become equal t * t by changing the value of t;
obviously the way to do this is to set t to the square root of no.

To someone who knows what "=" means in mathematics, but hasn't been
exposed to the concept of assignment, it's not an entirely insane
misconception.
Which is why I liked the fact that Pascal used a symbol other than = to mean
assignment.
>
(Exercise: Design and implement a language that really works this
way.)
--
Bill Medland
Nov 16 '06 #11

P: n/a
Keith Thompson skrev:
To someone who knows what "=" means in mathematics, but hasn't been
exposed to the concept of assignment, it's not an entirely insane
misconception.
I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)
August
Nov 16 '06 #12

P: n/a
>Happened to see my old (last millennium) c code. Almost forgot that I
>wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}
This reminds me a bit of the hypothetical BUT statement which I
wanted to add to a language (FORTRAN at the time, but it could
sorta fit into C also):

y = a*x*x + b*x + c BUT y = 0;

would solve a quadratic equation. If a, b, and c weren't constants
there wasn't anything in particular that prohibited it from changing
them instead of x to satisfy the condition. There wasn't anything
guaranteeing which root you got, either, although you could do:

y = a*x*x + b*x + c BUT y = 0 and x 0;

Naturally, something like:

y = 0*x + 3 BUT y = 4;

is likely to have infinite run time.
Nov 18 '06 #13

P: n/a

v4vijayakumar wrote:
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)
I thought the pedantic responses to this were
perversely bizarre. Obviously vijay was joking;
beginners often have misconceptions; and it is
not impossible that some programming language
behave the way beginner vijay hoped.

Anyway, since the C compiler is free to set
undefined variables to any value, a minor change
to vijay's code makes it work:

#include <launch_codes.h>

/*
* This sqrt function relies
* on the Compiler obeying Asimov's
* First Law of Robotics.
*/

int sqrt(int no)
{
int t;
if (no == t * t)
return t;
else
aim_and_launch("/dev/icbm", 1, TGT_TEHERAN);
}

James

Nov 19 '06 #14

P: n/a
"James Dow Allen" <jd*********@yahoo.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
[...]
#include <launch_codes.h>

/*
* This sqrt function relies
* on the Compiler obeying Asimov's
* First Law of Robotics.
*/

int sqrt(int no)
{
int t;
if (no == t * t)
return t;
else
aim_and_launch("/dev/icbm", 1, TGT_TEHERAN);
}
:O
Nov 19 '06 #15

P: n/a
August Karlstrom <fu********@comhem.sewrote:
Keith Thompson skrev:
To someone who knows what "=" means in mathematics, but hasn't been
exposed to the concept of assignment, it's not an entirely insane
misconception.

I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)
You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and Plankalkuel
don't count) share that choice of symbol? I'd rather call the assumption
that symbols must mean the same thing in maths and in programming
unfortunate. To use another example, {} is not just a higher-level form
of (), and [] and <have nothing to do with intervals.

Richard
Nov 20 '06 #16

P: n/a
Richard Bos skrev:
August Karlstrom <fu********@comhem.sewrote:
>I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)

You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and Plankalkuel
don't count) share that choice of symbol?
Sure, and MTV plays the very best music too.
I'd rather call the assumption
that symbols must mean the same thing in maths and in programming
unfortunate.
The problem is that there are several cases in C where a well known
mathematical concept X with notation XM is notated with XC instead and
XM is defined to mean something different.

Mathematics Pascal/Modula/Oberon C
x = y x equals y x equals y x becomes y
1/2 0.5 0.5 0
August
Nov 22 '06 #17

P: n/a
August Karlstrom <fu********@comhem.sewrote:
Richard Bos skrev:
August Karlstrom <fu********@comhem.sewrote:
I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)
You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and Plankalkuel
don't count) share that choice of symbol?

Sure, and MTV plays the very best music too.
Who'se talking about the very best? That symbol _is_ used for
assignment, and it would be even if C had chosen <- instead. If you want
to be a good programmer, you'd better get used to computing not being
identical to maths.

Richard
Nov 23 '06 #18

P: n/a
Richard Bos wrote:
August Karlstrom <fu********@comhem.sewrote:
Keith Thompson skrev:
To someone who knows what "=" means in mathematics, but hasn't been
exposed to the concept of assignment, it's not an entirely insane
misconception.
I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)

You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and Plankalkuel
don't count) share that choice of symbol?
How many of those have copied their operators from C?

Nov 23 '06 #19

P: n/a
Harald van D?k wrote:
Richard Bos wrote:
>August Karlstrom <fu********@comhem.sewrote:
>>Keith Thompson skrev:

To someone who knows what "=" means in mathematics, but hasn't
been exposed to the concept of assignment, it's not an entirely
insane misconception.

I'd say: "...hasn't been exposed to C's unfortunate choice of
symbol for assignment". ;-)

You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and
Plankalkuel don't count) share that choice of symbol?

How many of those have copied their operators from C?
Which in turn copied its silly assignment operator from Fortran.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 23 '06 #20

P: n/a
In article <11**********************@h54g2000cwb.googlegroups .com"=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <tr*****@gmail.comwrites:
Richard Bos wrote:
August Karlstrom <fu********@comhem.sewrote:
....
I'd say: "...hasn't been exposed to C's unfortunate choice of symbol for
assignment". ;-)
You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and Plankalkuel
don't count) share that choice of symbol?

How many of those have copied their operators from C?
I would rather say that C has copied it from other languages (the use
of '=' for assignment). I think Fortran was the first.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 23 '06 #21

P: n/a
In article <Si******************@newsb.telia.netAugust Karlstrom <fu********@comhem.sewrites:
....
Mathematics Pascal/Modula/Oberon C
x = y x equals y x equals y x becomes y
1/2 0.5 0.5 0
The second line is incorrect for Mathematics if you are not working in
the reals.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 23 '06 #22

P: n/a
CBFalconer wrote:
Harald van D?k wrote:
Richard Bos wrote:
August Karlstrom <fu********@comhem.sewrote:
Keith Thompson skrev:

To someone who knows what "=" means in mathematics, but hasn't
been exposed to the concept of assignment, it's not an entirely
insane misconception.

I'd say: "...hasn't been exposed to C's unfortunate choice of
symbol for assignment". ;-)

You could say that, but don't more than half of all programming
languages which are used in practice (i.e., Intercal and
Plankalkuel don't count) share that choice of symbol?
How many of those have copied their operators from C?

Which in turn copied its silly assignment operator from Fortran.
Or more directly, from B, I imagine. But where C got it from is not
really relevant to my point.

Nov 24 '06 #23

P: n/a
Dear Mr Richard.. I asked out of curiosity to vijayakumar ..

And he might have got it that the question was meant to him.

Richard Heathfield wrote:
Dh*********@gmail.com top-posted, but I've fixed that. He said:
v4vijayakumar wrote:
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}
Can u tell me what is interesting in this code ??

No, there isn't any regular comp.lang.c contributor called "u".
(Translation: abbreviating "you" to "u" marks you out as someone who can't
even spell "you" properly. Take the time to type "you" in full, in the
interests of clear communication and of not looking idiotic.)

Here are the principal points of interest:

1) function name choice invades implementation namespace, thus invoking
undefined behaviour;
2) function name choice gives a misleading impression of what the function
tries to do;
3) t's value is indeterminate, but the function still attempts to use t's
value regardless, thus invoking undefined behaviour;
4) a parameter is used solely for assigning a value to, and this value will
be lost at the same time that the parameter itself is lost, at the end of
this function;
5) an indeterminate value is returned from this function, thus invoking
undefined behaviour in any caller that evaluates it;
6) even if all those faults were fixed, the function would still need major
repair work (type choices, range validation, etc).

Summary: it was written by someone who didn't know C very well six or seven
years ago. Whether they have learned their lessons in the meantime, I
cannot say.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 24 '06 #24

P: n/a
Dh*********@gmail.com said:
Dear Mr Richard.. I asked out of curiosity to vijayakumar ..

And he might have got it that the question was meant to him.
If you wish to direct a communication to a specific individual, I recommend
email as the best medium. The comp.lang.c newsgroup is an international
newsgroup, not a private bulletin board.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 24 '06 #25

P: n/a
Dik T. Winter skrev:
In article <Si******************@newsb.telia.netAugust Karlstrom <fu********@comhem.sewrites:
...
Mathematics Pascal/Modula/Oberon C
x = y x equals y x equals y x becomes y
1/2 0.5 0.5 0

The second line is incorrect for Mathematics if you are not working in
the reals.
Well, it's just as valid for the rational numbers (Q is a field).

In the ring of integers, division is not defined for every number (not
every integer has a multiplicative inverse).
August

--
"Programming languages like C make people really believe that one half
equals zero." -- A. Karlstrom
Nov 25 '06 #26

P: n/a
In article <Vx******************@newsb.telia.netAugust Karlstrom <fu********@comhem.sewrites:
Dik T. Winter skrev:
In article <Si******************@newsb.telia.netAugust Karlstrom <fu********@comhem.sewrites:
...
Mathematics Pascal/Modula/Oberon C
x = y x equals y x equals y x becomes y
1/2 0.5 0.5 0
The second line is incorrect for Mathematics if you are not working in
the reals.

Well, it's just as valid for the rational numbers (Q is a field).

In the ring of integers, division is not defined for every number (not
every integer has a multiplicative inverse).
So, C defines division in the integers (as the truncating variant). What
is wrong with that? But, whatever, both semantics above of '=' and '/'
are indirectly from Fortran. You should know the off-spring of programming
languages to understand that. Algol 58 (never really defined) took it from
Fortran. And Algol 58 begot Jovial, which begot CPL, which begot BCPL,
which begot B, which begot C. Algol 58 also begot Algol 60 (which changed
use), which in turn begot Pascal/Modula/Oberon. And the very first
assembly language I did learn also used '=' for assignment:
S = M[A]
which meant: fetch the contents of memory location A and store it in
register S. To get the condition bit set when the two were equal you
used:
U, S - M[A], Z
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 26 '06 #27

This discussion thread is closed

Replies have been disabled for this discussion.