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
26 3650
Keith Thompson wrote:
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
>"Dh*********@g mail.com" <Dh*********@gm ail.comwrites:
>>v4vijayakum ar 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
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
>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.
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
"James Dow Allen" <jd*********@ya hoo.comwrote in message
news:11******** **************@ h54g2000cwb.goo glegroups.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
August Karlstrom <fu********@com hem.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
Richard Bos skrev:
August Karlstrom <fu********@com hem.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
August Karlstrom <fu********@com hem.sewrote:
Richard Bos skrev:
August Karlstrom <fu********@com hem.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
Richard Bos wrote:
August Karlstrom <fu********@com hem.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?
Harald van D?k wrote:
Richard Bos wrote:
>August Karlstrom <fu********@com hem.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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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, "
+
|
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>
|
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:
|
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
| |
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;
|
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;
|
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();
|
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,...
|
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...
|
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...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |