PHP/Data Type/Float
Содержание
- 1 Add a PHP block and create a variable that holds a floating-point number
- 2 Addition of two floating-point variables compared to a variable can lead to an unexpected result.
- 3 Addition of two floating-point variables compared to a variable with the expected value result can lead to an unexpected result.
- 4 Calculate the area of a circle of given radius
- 5 Compare floating-point values,avoid the == operator and use the <, >, >=, and
- 6 Comparing floating point numbers
- 7 Comparing float values
- 8 Compound Interest
- 9 Continuously Compounded Interest Calculator
- 10 Define Integer, double and string value and output them
- 11 Finding the distance between two points
- 12 Floating-point arithmetic:
- 13 Floating point numbers ("doubles") can be specified using any of the following syntaxes:
- 14 Float is converted to an integer before the binary and (&) operation is executed.
- 15 PHP feet-to-meters converter
- 16 Simple Interest
- 17 Specifying Precision
- 18 Storing Floating Point Numbers in PHP
- 19 the float is converted to an integer before the binary and (&) operation is executed.
Add a PHP block and create a variable that holds a floating-point number
<?
# Create a variable that holds a floating-point number:
$floatVar = "1542.2232235";
echo "<P>float: $floatVar</P>";
?>
Addition of two floating-point variables compared to a variable can lead to an unexpected result.
<?php
$a=50.3;
$b=50.4;
$c=100.7;
if ($a + $b == $c) {
echo "$a + $b == $c\n";
}
else {
echo "$a + $b != $c\n";
}
?>
Addition of two floating-point variables compared to a variable with the expected value result can lead to an unexpected result.
<?php
$a=50.3;
$b=50.4;
$c=100.7;
if ($a + $b == $c) {
echo "$a + $b == $c\n";
}
else {
echo "$a + $b != $c\n";
}
?>
Calculate the area of a circle of given radius
<?php
$radius = 2.0;
$pi = 3.14159;
$area = $pi * $radius * $radius;
echo("radius = ");
echo($radius);
echo("<BR>area = ");
echo($area);
?>
Compare floating-point values,avoid the == operator and use the <, >, >=, and
<?php
for ($i = 0; $i < 100; $i += 0.1) {
if ($i == 50) echo "$i == 50" . "\n";
if ($i >= 50 && $i < 50.1) echo "$i >= 50 && $i < 50.1" . "\n";
}
?>
Comparing floating point numbers
<?
$price_1 = 0.00001;
$price_2 = 0.000001;
if(abs($price_1 - $price_2) < 0.00001) {
print "$price_1 and $price_2 are equal.";
} else {
print "$price_1 and $price_2 are not equal.";
}
?>
Comparing float values
<?
$cost = 1.22;
$limit = 1.00;
function check_limit($total_cost, $credit_limit) {
if ($total_cost > $credit_limit) :
return 0;
endif;
return 1;
}
if (check_limit($cost, $limit)):
print "OK";
else :
print "Balance less than $".$limit."!";
endif;
?>
Compound Interest
<?php
function calc_compound_interest($a, $r, $f, $y) {
return $a * pow((1 + ($r / $f)), ($f * $y));
}
echo calc_compound_interest(5000, .0325, 365, 3), "<br />";
?>
Continuously Compounded Interest Calculator
<?php
function calc_continuous_compound_interest($a, $r, $y) {
return $a * pow(M_E, $r * $y);
}
echo calc_continuous_compound_interest(5000, .0325, 3), "<br />";
?>
Define Integer, double and string value and output them
<?php
$integer_value = 1;
$double_value = 1.2345678e6;
$string_value = "This is a string";
echo("<BR>integer value: ");
echo $integer_value;
echo("<BR>double value: ");
echo($double_value);
echo("<BR>string value: ");
echo($string_value);
?>
Finding the distance between two points
<?php
function sphere_distance($lat1, $lon1, $lat2, $lon2, $radius = 6378.135) {
$rad = doubleval(M_PI/180.0);
$lat1 = doubleval($lat1) * $rad;
$lon1 = doubleval($lon1) * $rad;
$lat2 = doubleval($lat2) * $rad;
$lon2 = doubleval($lon2) * $rad;
$theta = $lon2 - $lon1;
$dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
if ($dist < 0) { $dist += M_PI; }
return $dist = $dist * $radius;
}
$lat1 = 40.;
$lon1 = -50.;
$lat2 = 37;
$lon2 = -122;
$dist = sphere_distance($lat1, $lon1, $lat2, $lon2);
printf("%.2f\n", $dist * 0.621);
?>
Floating-point arithmetic:
$a = 1.132324;
$b = $a + 1;
$b = $a + 1.0;
$c = 1.1e15;
$d = (0.1+0.7) * 10;
Floating point numbers ("doubles") can be specified using any of the following syntaxes:
<?
$a = 1.234;
$a = 1.2e3;
?>
Float is converted to an integer before the binary and (&) operation is executed.
<?php
$a = 3.5;
$b = $a & 2;
echo "$a & 2 = $b";
?>
PHP feet-to-meters converter
<head>
<title>Feet to meters conversion</title>
</head>
<body>
<?php
$feet = htmlentities($_GET["feet"]);
if ($_GET[feet] != NULL){
echo "<strong>$feet</strong> feet converts to <strong>";
echo $feet * 0.3048;
echo "</strong> meters.<br />";
}
?>
<form action="<?php echo(htmlentities($_SERVER["PHP_SELF"])); ?>" method="GET">
<label>Feet:
<input type="text" name="feet" value="<?php echo $feet; ?>" />
</label>
<input type="submit" value="Convert!" />
</form>
</body>
</html>
Simple Interest
<?php
function calc_simple_interest($principal, $annualrate, $years) {
return $principal * $annualrate * $years;
}
echo calc_simple_interest(22000, .0539, 6), "<br />";
echo calc_simple_interest(1000, .085, .5);
?>
Specifying Precision
<?
printf( "%.2f", 5.333333);
?>
Storing Floating Point Numbers in PHP
<?php
/* Standard Floating Point Notation */
$my_float = 5.1;
/* Scientific Floating Point Notation of same number */
$my_float = .051e2;
?>
the float is converted to an integer before the binary and (&) operation is executed.
<?php
$a = 3.5;
$b = $a & 2;
echo "$a & 2 = $b";
?>