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;
}
?>