Saturday, March 31, 2012

Some problems of float and double variables in java

You must not use the float or double datatype for accurate calculations.
At java programming, it doesn't get exact number to use the float or double datatype.

For example, if you had 1.03 dollars and used 42 cents, you remained 61 cents.
Using java programming answers as follows.


System.out.println(1.03 - .42);

---------- Execute ----------
0.6100000000000001

Not exactly what you want. 
Because the float and double was designed for science and engineering, it calculates so fast as an approximate value. 
So the float and double variables are imprecise. 

What you use for accurate calculations?
The answer is int, long, and bigdecimal

I refer to the book of effective java programming language guide.



Tuesday, March 13, 2012

How to verify country through Internet Protocol (IP)

Many global homepages support various languages such as English, French, Spanish, etc.
I wondered that it verified the languages according to visitor.

By using cookies can be selected when the user had already visited.
If the user have visited at the homepage for the first time,
it will verify the country by user's IP address.

The MaxMind(http://www.maxmind.com/) is provided APIs that verified the country by IP address.


There are two APIs for geolocation solutions."GeoIP City" and "GeoLite City".
GeoLite City is for free and less accurate than GeoIP City.
I think GeoLite City is too good to achieve our goals.




Now I use the GeoLite City API to get the country into an IP address.
The API has supported for multiple program languages.
I use PHP programming language for this sample.
You go to http://www.maxmind.com/app/php and download Pure PHP Module.


I download "sample.php" of many sources.
The content of sample.php is as follows.

#!/usr/bin/php -q
<?php

// This code demonstrates how to lookup the country by IP Address

include("geoip.inc");

// Uncomment if querying against GeoIP/Lite City.
// include("geoipcity.inc");

$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);

echo geoip_country_code_by_addr($gi, "24.24.24.24") . "\t" .
geoip_country_name_by_addr($gi, "24.24.24.24") . "\n";
echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";

geoip_close($gi);

?>

Look at the source. It includes "geoip.inc" file.
I download "geoip.inc" from Pure PHP Module.

Also we need to download "GeoIP.dat" files. It is binary format database.
You can download it next link.
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Unzip that file and copy it in server.

This example shows the country code and name.
For global homepage, I just need to know the country code.

I make PHP source to verify country through IP address and redirect to each language page.

<?
include("geoip.inc");

$gi = geoip_open("/usr/local/geoip/GeoIP.dat",GEOIP_STANDARD);
$addr = $_SERVER["REMOTE_ADDR"];

if (geoip_country_code_by_addr($gi, $addr) == "FR") {
header("Location: http://xmlmanager.blogspot.com/fr/");
exit;
}
else {
header("Location: http://xmlmanager.blogspot.com/en/");
exit;
}
?>

Wednesday, February 22, 2012

Using SwipeView for a swipe gesture in mobile web

In mobile web, the swipe is the most commonly used features.
However, It is difficult to implement it.
It need to write many javascript code.

I introduce the SwipeView service.
It provide some script that handle swipe features.

http://cubiq.org/swipeview




The video is as follows.



It contains various contents such as image, text, and so on.

The image demo is as follows.
http://cubiq.org/dropbox/SwipeView/demo/gallery/

The text demo is as follows.
http://cubiq.org/dropbox/SwipeView/demo/ereader/

Wednesday, February 8, 2012

A test of blog's BGM using HTML5


W3Schools.com have had many information about standard of W3C.
Especially It announced HTML5 tutorials. It's very useful for me.

http://www.w3schools.com/html5/default.asp

Some blogs have a background music such as BGM.
Most of all are used windows media player in Internet Explorer.

This sample used <audio> tag of HTML5.
I set auto play and hided controls. Maybe you can listen to music in this post.

Of course, It can be played in a browser that support <audio> tag of HTML5.
Under Internet Explorer 8, you saw just some text.

The source is as follows.
In the below source it is set control and repeat.


<audio autoplay="autoplay" loop="loop" controls="controls">
  <source src="rain.mp3" type="audio/mpeg" />
  <source src="rain.ogg" type="audio/ogg" />
</audio>

I tested at mobile environment.
Android is a fully working  auto play.
Though iPhone support HTML5, it is not working auto play.
Only when the user clicks, the music is playing.

Monday, February 6, 2012

XML Quiz...

Please answer next items as  O or X


1) XML stands for “Example Markup Language”.

2) XML uses a DTD to describe the data.

3) XML’s goal is to replace HTML.

4) DTD stands for “Dynamic Type Definition”.

5) All XML documents must have a DTD.

6) All XML elements must be properly closed.

7) XML documents must have a root tag.

8) XML elements cannot be empty.

.
.
.
.
.
.
.
.

The answer is as follows.


1) XML stands for “Example Markup Language”. (X)
    XML stands for "eXtensible Markup Language".

2) XML uses a DTD to describe the data. (O)

3) XML’s goal is to replace HTML. (X)
    XML's goal is to replace SGML.

4) DTD stands for “Dynamic Type Definition”. (X)
    DTD stands for "Document Type Definition".

5) All XML documents must have a DTD. (X)
    Well-formed XML documents have not a DTD.

6) All XML elements must be properly closed. (O)

7) XML documents must have a root tag. (O)

8) XML elements cannot be empty. (X)
    XML elements can be empty.