PHP - Balloc allocation exceeds list boundary - Mips Fonera OpenWRT
23 Apr 2010 à 14:55 par itimeteo
Vous créez des programmes en PHP sur votre périphérique embarqué MIPS et vous obtenez une erreur "Balloc allocation exceeds list boundary" ? Ne cherchez pas plus loin : la majorité des compilations PHP pour MIPS fonctionnent très mal avec les nombres flottant. La solution ? Utiliser l'utilitaire bc.
Utilitaire bc
Extrait du man
NAME
bc - arbitrary-precision arithmetic language
SYNOPSIS
bc [-l] [file ...]
DESCRIPTION
The bc utility shall implement an arbitrary precision calculator.
It shall take input from any files given, then read from the standard input.
If the standard input and standard output to bc are attached to a terminal,
the invocation of bc shall be considered to be interactive, causing behavioral
constraints described in the following sections.
Installation de bc
| 1. | opkg install bc |
Exemple d'utilisation
| 1. | echo "1/3" | bc -l |
| 2. | # => Affichera .33333333333333333333 |
Utilisation de bc dans PHP
Afin de gérer les nombres flottant sous PHP nous utiliserons donc la commande bc -l.| 1. |
// Au lieu d'utiliser ceci :
|
| 2. |
$resultat = 4 / 76;
|
| 3. |
echo $resultat;
|
| 4. | |
| 5. |
// Nous utiliserons à la place :
|
| 6. |
$resultat = exec('echo "4/76" | bc -l');
|
| 7. |
echo $resultat;
|
Encapsulation
Afin d'avoir un code un poil plus propre, nous pouvons encapsuler la commande bc -l dans une classe.Par exemple :
| 1. |
class Math
|
| 2. |
{
|
| 3. |
public static function evaluate($StringToEval)
|
| 4. |
{
|
| 5. |
return exec('echo "' . $StringToEval . '" | bc -l');
|
| 6. |
}
|
| 7. |
}
|
| 8. | |
| 9. |
$resultat = Math::evaluate('4/76');
|
| 10. |
echo $resultat;
|
Thème - Projets informatiques sur la Fonera
Cet article à un rapport avec l'article que vous lisez actuellement.
Lire la suite »
Pas de commentaire pour le moment.