Staredit Newtork
Community
StarCraft
Games
Site
Favourites
PHP - Mathematical Accuracy Issues, You know you want to learn PHP.

Creator: Centreri
Time: Dec 2 2007, 2:50 am

Post #1     Centreri Dec 2 2007, 2:50 am

[Avatar]
Overwin Winboat
 online contact
I'm working on a report primarily on the Lucas Numbers (proving things about them, etc), and I wanted to try comparing their relationship to the Fibonacci Sequence, so wrote a small script. Here's the code:
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Lucas Numbers</title>
</head>

<body>
<table style="border-width: thin; border-color: blue;">
<tr>
<td style="width: 50px; ">Number</td><td style="Width:200px;">Lucas</td><td style="Width:200px;">Fibonacci</td>
<td style="Width:200px;">Sum</td><td style="Width:150px;">Difference</td><td style="Width:150px;">Lucas/Fibonacci</td>
<td style="Width:150px;">Fibonacci/Lucas</td>
</tr>
<tr>
<td>1</td><td>1</td><td>1</td><td>2</td><td>0</td><td>1</td><td>1</td>
</tr>
<tr>
<td>2</td><td>3</td><td>1</td><td>4</td><td>2</td><td>3</td><td>.333333333333333</td>
</tr>
<?php
$lucas[0] = 1;
$lucas[1] = 3;
$fib[0] = 1;
$fib[1] = 1;
$precision = 20;
for ($i = 2; $i<=200; $i++) {
$lucas[$i] = $lucas[$i- '1'] + $lucas[$i-'2'];
$fib[$i] = $fib[$i- '1'] + $fib[$i- '2'];
echo " <tr> <td>";
echo $i+'1';
echo " </td><td>";
echo $lucas[$i];
echo " </td><td>";
echo $fib[$i];
echo " </td><td>";
echo bcadd($lucas[$i],$fib[$i], $precision);
echo " </td><td>";
echo bcsub($lucas[$i],$fib[$i], $precision);
echo " </td><td>";
echo bcdiv($lucas[$i],$fib[$i], $precision);
echo " </td><td>";
echo bcdiv($fib[$i],$lucas[$i], $precision);
echo "</td></tr>";


}
?>
</table>
</body>
</html>

This chart is supposed to show the # in both sequences, Fibonacci value, Lucas Value, their Sum, their Difference, Lucas Divided by Fibonacci, and Fibonacci divided by Lucas, in that order as a table. You can see what happened at:http://www.centreri.net/Experimentation.php. Got really ugly. I originally tried using normal +/-///* functions, but it didn't have the proper accuracy and started transforming actual values into more managable versions, so I switched to BCmath functions, which I heard from someone is just what I need. So... It thinks 1.54800875592E+12 divided by 3.461452808E+12 is zero, causing errors there. It thinks vice versa of that is same.

It doesn't do addition/subtraction beyond the 60'th value. In short, it's an unreiable disaster in which it's impossible to check consistancy of patterns, all that. Not that this exact script needs to do it, but since I'll be running tests on these values, I need these functions to work properly. Also, it's not vital, but if there's an easy way to make the precision of the functions affect the numbers only if there's more numbers than it would normally show, rather then adding .0000000000, that would be great.

Thank you in advance. Oh, and I know the coding isn't optimal, but I was in a hurry. I posted it here because I've gotten largely more useful responses from here than on coding sites, like Codedfx.com or phpbuilder.com. Doodle, hope you're there.
Top

Post #2     DT_Battlekruser Dec 2 2007, 6:48 pm

[Avatar]
I paid eleven minerals for THIS?
 offline contact
Uhh, firstly, what do you mean by the addition and subtractions are wrong? I tested line 100 on Windows Calculator and the results checked out.

As for division, you have simple integer division errors. In all programming languages, 1/3 = 0 and 7/3 = 2. I would try casting one of your numbers as a double before doing division, or concatenating '.0' Not sure if that will work, but there is clearly an integer division issue.
"Three can keep a secret, if two are dead." -Benjamin Franklin

"Had, having, and in quest to have, extreme;
A bliss in proof, and proved, a very woe;
Before, a joy proposed; behind, a dream.
All this the world well knows; yet none knows well
To shun the heaven that leads men to this hell."
-William Shakespeare
Top

Post #3     Centreri Dec 5 2007, 2:27 am

[Avatar]
Overwin Winboat
 online contact
Oh, it works now. I got a response from www.phpbuilders.org (I think). Better-used bcmath functions made it all possible. Sadly, as I should've anticipated, this whole thing leads to one thing: .447(I have long values on site)L = F. All values add up, since every single one of the equations leads to that.
Top
0 members in this topic: None
+ 0 guest(s)


[10:41 pm]
Corbo[MM] -- ...lolwut....?
[10:27 pm]
Centreri -- Hey, why do people say that Russian 'Greetings' as Zdrastviche? It's not that at all. It's Zdrastvuyete. Weird.
[10:26 pm]
[Echo]:] -- Moi Moi l'urine!
[10:26 pm]
[Echo]:] -- BONJOUR!
[10:25 pm]
Centreri -- Amuse me as I work on French Homework!
[10:23 pm]
Centreri -- I can wait a bit more :( .
[10:22 pm]
[Echo]:] -- I have left over Easter jellybeans, yum!
You must log in to shout.

©2003-2008 Staredit Network.
Starcraft & Starcraft II are trademarks of Blizzard Entertainment.
Site Index   |   Terms of Service   |   Privacy Policy   |   Contributions