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.
[2024-4-27. : 9:38 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: NudeRaider sing it brother
trust me, you don't wanna hear that. I defer that to the pros.
[2024-4-27. : 7:56 pm]
Ultraviolet -- NudeRaider
NudeRaider shouted: "War nie wirklich weg" 🎵
sing it brother
[2024-4-27. : 6:24 pm]
NudeRaider -- "War nie wirklich weg" 🎵
[2024-4-27. : 3:33 pm]
O)FaRTy1billion[MM] -- o sen is back
[2024-4-27. : 1:53 am]
Ultraviolet -- :lol:
[2024-4-26. : 6:51 pm]
Vrael -- It is, and I could definitely use a company with a commitment to flexibility, quality, and customer satisfaction to provide effective solutions to dampness and humidity in my urban environment.
[2024-4-26. : 6:50 pm]
NudeRaider -- Vrael
Vrael shouted: Idk, I was looking more for a dehumidifer company which maybe stands out as a beacon of relief amidst damp and unpredictable climates of bustling metropolises. Not sure Amazon qualifies
sounds like moisture control is often a pressing concern in your city
[2024-4-26. : 6:50 pm]
Vrael -- Maybe here on the StarEdit Network I could look through the Forums for some Introductions to people who care about the Topics of Dehumidifiers and Carpet Cleaning?
[2024-4-26. : 6:49 pm]
Vrael -- Perhaps even here I on the StarEdit Network I could look for some Introductions.
[2024-4-26. : 6:48 pm]
Vrael -- On this Topic, I could definitely use some Introductions.
Please log in to shout.


Members Online: RIVE