|
|
|
|
|
|
| |
This is a procedure which will convert a decimal number ($decimal) into its corresponding whole number counterpart plus its fractional counterpart.
So, 2.5 would return $whole = 2, $numerator = 1, $denominator = 2. it also returns the 'top heavy' format, so that 2.5 would give $top_heavy = 5/2.
This works for negative decimals too. And for zero, returns 0/1.
It works up to the limit of phps number handling (12 decimal places i think)
jeremy
| <?php
$decimal = -4.2;
list ($whole, $numerator, $denominator, $top_heavy) = convert($decimal);
print $whole;
print "<BR>";
print $numerator;
print "<BR>";
print $denominator;
print "<BR>";
print $top_heavy;
function convert ($decimal) {
if ($decimal == 0) {
$whole = 0;
$numerator = 0;
$denominator = 1;
$top_heavy = 0;
}
else {
$sign = 1;
if ($decimal < 0) {
$sign = -1;
}
if (floor(abs($decimal)) == 0) {
$whole = 0;
$conversion = abs($decimal);
}
else {
$whole = floor(abs($decimal));
$conversion = abs($decimal);
}
$power = 1;
$flag = 0;
while ($flag == 0) {
$argument = $conversion * $power;
if ($argument == floor($argument)) {
$flag = 1;
}
else {
$power = $power * 10;
}
}
$numerator = $conversion * $power;
$denominator = $power;
$hcf = euclid ($numerator, $denominator);
$numerator = $numerator/$hcf;
$denominator = $denominator/$hcf;
$whole = $sign * $whole;
$top_heavy = $sign * $numerator;
$numerator = abs($top_heavy) - (abs($whole) * $denominator);
if (($whole == 0) && ($sign == -1)) {
$numerator = $numerator * $sign;
}
}
return array($whole, $numerator, $denominator, $top_heavy);
}
function euclid ($number_one, $number_two) {
if (($number_one == 0) or ($number_two == 0)) {
$hcf = 1;
return $hcf;
}
else {
if ($number_one < $number_two) {
$buffer = $number_one;
$number_one = $number_two;
$number_two = $buffer;
}
$dividend = $number_one;
$divisor = $number_two;
$remainder = $dividend;
while ($remainder > 0) {
if ((floor($dividend/$divisor)) == ($dividend/$divisor)) {
$quotient = $dividend/$divisor;
$remainder = 0;
}
else {
$quotient = floor($dividend/$divisor);
$remainder = $dividend - ($quotient * $divisor);
}
$hcf = $divisor;
$dividend = $divisor;
$divisor = $remainder;
}
}
return $hcf;
}
?> | | |
|
| Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP, Math., Algorithms | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | | | Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms, PHP, Math. | | | Math operations on big numbers Categories : PHP, Math. | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | | | Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | Boolean Keyword Interpreter Categories : PHP, Algorithms, Search Engines | | | Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL, Complete Programs, Algorithms, PHP, Databases | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | | | Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP, BC math, Algorithms | | | A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays, PHP, Algorithms | |
|
|
|