This Post
PermalinkURI
URI Label
Revisions
Export:
XML (With Files)
PNG Image
Archives
September 2010 (1)March 2010 (1)
January 2010 (1)
November 2009 (1)
July 2009 (1)
January 2009 (3)
December 2008 (1)
Sections
Code Tutorial (4)Misc (2)
QRCode Button (1)
Server Admin (2)
Os
Debian (1)Code Tags
PHP (3)Mysql (1)
Bash (1)
Tools
Show/Hide QR CodeShow/Hide Keys
Code Tags: PHP
I was working on a project that needed to list the Beaufort wind force. The first attempt was to do a lookup based function using the data here: http://en.wikipedia.org/wiki/Beaufort_scale
Attempt 1
I then noticed on the wikipedia page the scale's empirical formula
so i thought i could improve the function ;)
Attempt 2
I was working on a project that needed to list the Beaufort wind force. The first attempt was to do a lookup based function using the data here: http://en.wikipedia.org/wiki/Beaufort_scale
Attempt 1
php code:function beaufort_scale($speed, $unit="m/s"){
$units['m/s'] = 1;
$units['kph'] = 1/3.6;
$units['mph'] = 0.44704;
$units['kts'] = 0.514444444;
if(!in_array($unit,array_keys($units))){
return "Error: couldn't recognise unit";
}
$speed *= $units[$unit];
$scale = array(0.3,1.5,3.4,5.4,7.9,10.7,13.8,17.1,20.7,24.4,28.4,32.6,1000);
foreach($scale as $key=>$val){
if($speed <= $val) return $key;
}
return 'Error';
}
I then noticed on the wikipedia page the scale's empirical formula
so i thought i could improve the function ;)
v = 0.866 * B3/2 (units m/s)
Attempt 2
This one has the added ability of returning a decimal answerphp code:function beaufort_scale($speed, $unit="m/s", $rnd=0){
$units['m/s'] = 1;
$units['kph'] = 1/3.6;
$units['mph'] = 0.44704;
$units['kts'] = 0.514444444;
if(!in_array($unit,array_keys($units))){
return "Error: couldn't recognise unit";
}
$speed *= $units[$unit];
$scale = pow(($speed/0.836),(2/3));
return round($scale,$rnd);
}