rulururu

post Tricky Floating Point Numbers

September 27th, 2008

Filed under: C++ — Kai @ 6:34 pm

Everyone knows that floating point numbers do have finite ranges, but this limitation can show up in unexpected ways. For instance you may find the output of the following lines of code surprising.

float f = 16777216; 
cout << f << " " << f+1 << endl;

Against expectations this code prints the value 16777216 twice.

What happened? According to the IEEE specification for floating point arithmetic, a float type is 32 bits wide. Twenty four of these bits are devoted to the significand (what used to be called the mantissa or also coefficient) and the rest to the exponent. The number 16777216 is 224 and so the float variable f has no precision left to represent f+1.
A similar phenomena would happen for 253 if f were of type double because a 64-bit double devotes 53 bits to the significand.

The following code prints 0 rather than 1.

x = 9007199254740992; // 2^53 
cout << ((x+1) - x) << endl;

We can also run out of precision when adding small numbers to moderate-sized numbers. For example, the following code prints “Sorry!” because DBL_EPSILON (defined in float.h) is the smallest positive number ε such that 1 + ε ≠ 1 when using double types.

x = 1.0;
y = x + 0.5 * DBL_EPSILON;
if (x == y)
    cout << "Sorry!" << endl;

Similarly, the constant FLT_EPSILON is the smallest positive number ε such that 1 + ε ≠ 1 when using float types.

post Incredible C++ Snippet - How it works

September 24th, 2008

Filed under: C++ — Kai @ 8:00 pm

I think it’s time to disclose the secret of that piece of code. First of all it’s not that tricky as you might have expected. You just have to get some basic knowledge about memory allocation.

Probably most programmers if they have a look at those few lines would say that here a beginner didn’t pay attention and that the program will poorly fail by an access violation error or something similar.

For everybody who didn’t try out this snippet himself:
If y is declared before x in the line the loop is becoming an endless loop. If it’s declared the other way round the program seems to work correctly.

First of all I have to clarify that even if we declare y before x in that line, x is first allocated in memory. It’s read by the machine from right to left.
Second thing which is important to know is that memory is always allocated from top to bottom (for our imagination).

For better explanation I changed the order of x and y:

int y,x;
int feld[5];

On the stack first of all y gets it’s space of 4 bytes after that x gets the same. After that above those two the array gets 5 times 4 bytes (20 bytes).

just allocated

After running the loop 5 times (from 0 up to 4) the array is filled with values. Until here everything works as it should.

everything correct till here

But wrongly the loop is run one more time (<=5) and that's why the next value is written into the space that was allocated for x (as you can see on the picture below). In fact x gets overwritten...

x it overwritten

The endless loop was caused by writing every time again 1 into y.
This is no black magic and it’s also not illegal according to the C++ standart. It’s just a bad error done by the programmer which often happens and usually almost not fineable in a few minutes.

It should just show you to be careful with your allocations.

post Incredible C++ Snippet

September 24th, 2008

Filed under: C++ — Kai @ 4:31 pm

Please have a look at this code snippet and try to tell me what you’re expecting it to print out.

#include <iostream>
using namespace std;
 
int main()
{
	int x,y;
	int feld[5];
 
	x=6;
	for(y=0;y<=5;y++)
	{
		feld[y] = 1;
		cout << y << endl;
	}
	cout << x << endl;
 
return 0;	
}

Mention that the array size is 5 and the for loop goes till 5 (including!).

If you don’t get the solution why this happens the way it happens it might help you to know changing the order of decleration of x and y does cause a totally different output.
I will give you the solution to that miracle in a few days…

Have fun ;-)

post Confused about NULL

September 19th, 2008

Filed under: C++ — Kai @ 2:34 pm

NULL in c is defined as (void*)0, While for C++ its simple 0. Any ideas ?

#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif

As you know C++ is a direct descendant of C that retains almost all of C as a subset. Maybe it’s because of the fact that C++ provides stronger type checking than C and directly supports a wider range of programming styles than C.

Obviously it’s not that important because well written C tends to be legal C++ also.

post Dead Man’s Switch

September 10th, 2008

Filed under: Internet, Nonsense — Kai @ 10:10 am

The website I yesterday night found is the proof again that almost everything, achievable by man, can be found in the internet. The project idea shows me in a very obvious way that the respond to individual human needs in some parts has moved into the world wide web.

It’s called Dead Man’s Switch but what is it for?

If anybody already got confused even by its name, don’t worry: In general a Dead Man’s switch is, as its name suggests, a switch that is automatically operated in case the human operator becomes incapacitated. For example firefighters use it when being in a burning building.

Alternatively, as some of you might have seen in the movies, the switch detonates a bomb. This is applied in suicide bombing, to trigger the explosive if the bomber is shot or overpowered.

The concept of www.deadmansswitch.net is similar:

Everyone carries valuable information in their heads. It might be about their work, financial information, etc. If anything were to happen to them, this information would be lost, unfortunately.

This is where Dead Man’s Switch comes in.

I’d like to give to a quote from the founders website about how the website works:

This is how this works. You write a few e-mails, and choose the recipients. These emails are encrypted with military-grade algorithms, so you can be sure that no-one except the intended recipient will ever read them. Your switch will email you every so often, asking you to show that you are fine by clicking a link. If something were to… happen… to you, your switch would then send the emails you wrote to the recipients you specified. Sort of an “electronic will”, one could say.

You can regard the project as a system designed for people who really have paranoia or somehow are just crazy. I’ve seen lots of crazy, even useful projects in the internet therefor I just say it’s kinda funny thing. Even thought I will not use it to anytime!

Apart from that I’ve noticed that the website itself is well done and designed appealingly in terms of color :D

post A Terabyte of RAM - Why not?

September 8th, 2008

Filed under: Computers — Kai @ 10:02 am

Just a few thoughts I’d like to share with you.

None of us who isn’t running supercomputers or one heck of a cluster is there yet, anyway. But as RAM continues to drop in price, I can see the day coming.
Come the day we get a terabyte of RAM in our systems, we’ll find a need for it. After all, recall that great prophet Bill Gates who swore 640K of memory would be all we’d need. Real-time high-definition video editing anyone?

That day, by the way, isn’t as far away as you might think. Violin Scalable Memory will be happy to sell you a terabyte-capable memory device to attach to your server. Indeed it would cost you a few - well more than a few - hundred thousand dollars. Still, you can see it coming.

But while I can dream up applications that could use a terabyte of RAM, that leaves aside the wee technical problem of how you access that much memory. Daniel Phillips, a Linux developer, has an idea: the ramback virtual device.

In short, Phillips is using several cache coherency concepts to make using huge amounts of RAM as a virtual drive a practical approach to speeding up I/O-intensive interactions. For example, with enough RAM, you could run, say, an Oracle database running on Oracle or Red Hat Linux, at real-time speeds.

Of course, Phillips’ idea isn’t the same as coming up with an API for applications to address multiple gigabytes of RAM, but still, it is a very useful idea. Indeed, with its potential for vastly increasing database interactions, I can see it being a commercial success and driving the demand for huge memory.

This in turn will bring RAM prices down to normal user prices.

So, what will you do with your first laptop with a terabyte of RAM? You don’t need to worry about it yet. Well, not quite yet, anyway.

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)