|
|
|
|
|
|
| |
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;
}
?> | | |
|
| 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. | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Reverse a given number Categories : PHP, Beginner Guides, Algorithms, Math. | | | 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 | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character. Categories : PHP, Algorithms, Security, Authentication, Encryption | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | | | minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B. Categories : PHP, Arrays, Algorithms | | | IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival. Categories : PHP, Algorithms, Security, URLs | | | quick sort for associative arrays Categories : Algorithms, Arrays, PHP | | | Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL, Complete Programs, Algorithms, PHP, Databases | | | Find the day of the week for any given year/month/day. Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | | | Calculator for Baroque Violin strings Categories : Math., PHP, Strings | |
| |
| |
|