Staredit Network > Forums > Technology & Computers > Topic: PHP - Mathematical Accuracy Issues
PHP - Mathematical Accuracy Issues
Dec 2 2007, 2:50 am
By: Centreri  

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

Relatively ancient and inactive

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.



None.

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



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.




None.

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

Relatively ancient and inactive

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.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[08:05 pm]
Symmetry -- 50% of strings in starcraft are references
[2026-6-07. : 1:59 am]
RIVE -- Lots of fun little nods to different works of others in the cheats too.
[2026-6-06. : 5:17 pm]
Oh_Man -- so i was today years old wen i learned rebel yell starcraft mission is named after a billy idol song. makes me wonder wat other missions are named after songs
[2026-6-04. : 3:42 am]
RIVE -- Hellooooo, Nurse
[2026-6-03. : 4:19 pm]
NudeRaider -- Hello World!
[2026-6-01. : 4:03 pm]
IskatuMesk -- no Hello World? No Hello . jpeg? Not even ddosing some government website? smh my head
[2026-5-31. : 10:02 pm]
Symmetry -- I was really just figuring out how to compile
[2026-5-31. : 10:02 pm]
Symmetry -- I didn't make it do anything
[2026-5-31. : 9:17 pm]
Ultraviolet -- hell yeah. did you have trouble making the button functional, or just haven't gotten there yet?
[2026-5-31. : 8:29 pm]
Symmetry -- I made the marine shoot lasers and gave the medic a nonfunctional button
Please log in to shout.


Members Online: JohnnyTheWolf, Vrael