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.
[05:23 am]
zsnakezz -- yes
[2024-5-12. : 8:51 pm]
l)ark_ssj9kevin -- Are you excited for Homeworld 3?
[2024-5-12. : 8:44 pm]
l)ark_ssj9kevin -- Hi Brusilov
[2024-5-12. : 4:35 pm]
O)FaRTy1billion[MM] -- Brusilov
Brusilov shouted: Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
my server that was hosting it died
[2024-5-10. : 8:46 pm]
NudeRaider -- Brusilov
Brusilov shouted: Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
https://armoha.github.io/eud-book/
[2024-5-10. : 8:36 am]
Brusilov -- Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
[2024-5-09. : 11:31 pm]
Vrael -- :wob:
[2024-5-09. : 8:42 pm]
Ultraviolet -- :wob:
[2024-5-08. : 10:09 pm]
Ultraviolet -- let's fucking go on a madmen rage bruh
[2024-5-08. : 10:01 pm]
Vrael -- Alright fucks its time for cake and violence
Please log in to shout.


Members Online: Tableguy, Roy