473,769 Members | 7,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interesting code

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
26 3649
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
Dh*********@gma il.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
"Dh*********@gm ail.com" <Dh*********@gm ail.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

Richard Heathfield wrote:
Dh*********@gma il.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
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
"Dh*********@gm ail.com" <Dh*********@gm ail.comwrites:
>v4vijayakuma r 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_Keit h) 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
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
Keith Thompson <ks***@mib.orgw rote:
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
"Dh*********@gm ail.com" <Dh*********@gm ail.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
Richard Bos wrote:
Keith Thompson <ks***@mib.orgw rote:
>Ben Pfaff <bl*@cs.stanfor d.eduwrites:
>>"Dh*********@ gmail.com" <Dh*********@gm ail.comwrites:
v4vijayakuma r 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
In article <45************ ****@news.xs4al l.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>(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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1678
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I think it would be fun to play with the long-forgotten art of coding in machine language. And what about a fictional computer, such as one that works on an entirely different way (such as a non-binary computer)? It wouldn't be very useful, but...
2
4938
by: Dylan Phillips | last post by:
A strang error is occurring when I run the following code: SqlConnection c = new SqlConnection(); c.ConnectionString = "Initial Catalog=Northwind;user id=sa;password=kat1ie;Data Source=server"; c.Open(); SqlCommand command = c.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = "select Customers.customerid, customers.companyname, " +
6
3902
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the following code in Windows 2k and compiled it with the gcc(version: 3.2.3) contained in MinGW: #include <stdio.h> #include <stdlib.h> #include <string.h>
23
1191
by: Mythran | last post by:
For i As Integer = 0 To 5 Dim sb As StringBuilder If i = 0 sb = New StringBuilder(i.ToString()) Next i Console.WriteLine(sb.ToString()) Next i The above code gives the following output:
2
2103
by: sasifiqbal | last post by:
Hi, One of my developers are facing an interesting problem regarding UserControl invalidation. The problem is: We have two forms in our application. Form A does nothing except loading of Form B and Form B contains an array of UserButton kind of contol (we have created our own buttons deriving from UserButton). All the drawings activities in Form B are okay and nothing gets wrong if we
8
7993
by: Mythran | last post by:
Here is some code the provides some really interesting results! First, read over the two methods in class 'A' and compare. Just by looking at them, both results appear to return the EXACT same information the same way. But the appearances are deceiving! Copy and paste into a console application, then set the configuration to Release mode :) using System; using System.Diagnostics; using System.Reflection;
27
2339
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which comes to mind. I was writing usable which dealt with strings. As per usual with my code, I made it efficient to the extreme. One thing I did was replace, where possible, any usages of "strlen" with something like: struct PtrAndLen { char *p;
12
4491
by: Daniel Earwicker | last post by:
I wrote two trivial test programs that do a billion iterations of a virtual method call, first in C# (Visual Studio 2005): Thing t = new DerivedThing(); for (System.Int64 n = 0; n < 10000000000; n++) t.method(); Then in C++ (using Visual C++ 2005): Thing *t = new DerivedThing();
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.