|
|
|
|
|
|
|
|
|
| |
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;
}
?> | | |
|
| 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. | | | 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 | | | 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. | | | Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users). Categories : Algorithms, Email, PHP | | | what salt do I have to feed the crypt function with to make it work like the htpasswd command of apache? Categories : Algorithms, PHP, Authentication | | | Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms | | | A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array. Categories : Algorithms, Arrays, PHP, Complete Programs | | | Prime Spiral is a image plotted with all the primes in a number spiral.
Categories : Algorithms, Graphics, GD image library, Math. | | | Simple histogram class with a constructor and printing
methods (updated 1999/03/26) Categories : Algorithms, Math. | | | Boolean Keyword Interpreter Categories : PHP, Algorithms, Search Engines | | | Dollar Serial Number Validator Categories : PHP, Security, Algorithms | | | These 2 functions write and read the contents of a specially designated multi-dimensional array to and from a text file. Categories : Algorithms, PHP, Filesystem | |
| |
| |
|