Friday, September 27, 2013

How to compare float numbers safely?

This short article explains why we cannot use "==" to compare floats and how to compare floats right.
You can try to run this code in Java to understand the problem. double a = 1.0f / 3.0f;
double b = a + a + a;

System.out.println(a); // Output 0.3333333432674408
System.out.println(b); // Output 1.0000000298023224
System.out.println(1 == b); // Output false

Why?

If the compared values are the results of computation, they can be not equal.
Binary representation of the number 1/3 cannot be precise, because the place where the number is stored is limited. So the number rounding was made: 1/3 = 0.333.. = 0.(3) = 0.3333333432674408
So we have b = 1.0000000298023224 that is not equal to 1.0;

How to get round that problem?

Thursday, September 26, 2013

How to replace string in the project using terminal in Ubuntu?

We want to replace the string "regexp" with the string "replacement" in the terminal.
Let's say our project path is /home/www/project_name.
Actually regexp string is a regular expression pattern for our string.

This command works right:
cd /home/www/project_name
find . -type f -print0 | xargs -0 sed -i 's/regexp/replacement/g'
Let's see what the command means.
I will translate it to the normal language for human beings.

Go to my project folder:
cd /home/www/project_name Find all files (-type f) in my project folder:
find . -type f Use \0 delimiter (and not whitespace) to separate between the found pathnames in the output.
(We don't want whitespaces, because filenames can contain whitespaces and we will not be able to separate between pathnames).
-print0 This is a pipe. It means all the output that we have got before this sign "|" becomes an input for a next command (xargs in our case).
| xargs takes the output (pathnames that the command find found).
We use -0 because pathnames are terminated by \0 (instead of by whitespace). We know because we specified (find -print0).
xargs -0 sed - stream editor for filtering and transforming text.
-i allows to edit files in place.
-g replace all matches, not just the first match.
Replace our "regexp" with "replacement". sed -i 's/regexp/replacement/g'

What do lazy and greedy regular expressions mean?

Greedy expression tries to get as much as possible.
If you write a regular expression like this
<.+>
for the string:
<p>My expression</p>
you will get all the string
<p>My expression</p>
Oh, yes.. this thing is really greedy.

Let's take a look at the lazy one. If you write a regular expression like
<.+?>
We have just added "?". That means we want the smallest possible group. Now we have this result:
<p>
Here you are. The laziest thing you have ever seen :)

Tuesday, September 24, 2013

What screen resolution should I use for my web page design?

Not so long ago the answer was - 1024 x 768.
But you know, "the times they are changing".
Browser Display Statistics shows:
Today, most visitors have a screen resolution higher than 1024x768 pixels.
Of course you should know your audience and make the decision about resolution accordingly.

Thursday, April 4, 2013

JavaScript: How to get all properties of object

While using JavaScript statement for… in, we get functions of objects. If you need only properties without functions, use that:
for (key in object) {  
   if (object.hasOwnProperty(key)) {  
      ...  
   }  
}