IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Exception avec Power


Sujet :

Langage Delphi

  1. #1
    Membre habitué Avatar de Onimaru
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2010
    Messages
    283
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Turquie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2010
    Messages : 283
    Points : 129
    Points
    129
    Par défaut Exception avec Power
    Salut,
    Un exception "invalid floating point operation" se déclenche quand je veux calculer une puissance de ce type :
    Code Delphi : Sélectionner tout - Visualiser dans une fenêtre à part
    power(x, 1 / 3);
    avec x négatif, comment faire?

  2. #2
    Membre confirmé

    Homme Profil pro
    Chef de Projet ATIC
    Inscrit en
    Novembre 2005
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Chef de Projet ATIC
    Secteur : Finance

    Informations forums :
    Inscription : Novembre 2005
    Messages : 274
    Points : 508
    Points
    508
    Par défaut
    Utiliser une autre fonction, issue de l'aide embarcadero pour ce problème de nombre négatif en base :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    {** A power function from Jack Lyle. Said to be more powerful than the
        Pow function that comes with Delphi. }
    function Power2(Base, Exponent : Double) : Double;
    { raises the base to the exponent }
      CONST
        cTiny = 1e-15;
     
      VAR
        Power : Double; { Value before sign correction }
     
      BEGIN
        Power := 0;
        { Deal with the near zero special cases }
        IF (Abs(Base) < cTiny) THEN BEGIN
          Base := 0.0;
        END; { IF }
        IF (Abs(Exponent) < cTiny) THEN BEGIN
          Exponent := 0.0;
        END; { IF }
     
        { Deal with the exactly zero cases }
        IF (Base = 0.0) THEN BEGIN
          Power := 0.0;
        END; { IF }
        IF (Exponent = 0.0) THEN BEGIN
          Power := 1.0;
        END; { IF }
     
        { Cover everything else }
        IF ((Base < 0) AND (Exponent < 0)) THEN
            Power := 1/Exp(-Exponent*Ln(-Base))
        ELSE IF ((Base < 0) AND (Exponent >= 0)) THEN
            Power := Exp(Exponent*Ln(-Base))
        ELSE IF ((Base > 0) AND (Exponent < 0)) THEN
            Power := 1/Exp(-Exponent*Ln(Base))
        ELSE IF ((Base > 0) AND (Exponent >= 0)) THEN
            Power := Exp(Exponent*Ln(Base));
     
        { Correct the sign }
        IF ((Base < 0) AND (Frac(Exponent/2.0) <> 0.0)) THEN
          Result := -Power
        ELSE
          Result := Power;
      END; { FUNCTION Pow }

  3. #3
    Membre habitué Avatar de Onimaru
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2010
    Messages
    283
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Turquie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2010
    Messages : 283
    Points : 129
    Points
    129
    Par défaut
    Citation Envoyé par Okaryn Voir le message
    Utiliser une autre fonction, issue de l'aide embarcadero pour ce problème de nombre négatif en base :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    {** A power function from Jack Lyle. Said to be more powerful than the
        Pow function that comes with Delphi. }
    function Power2(Base, Exponent : Double) : Double;
    { raises the base to the exponent }
      CONST
        cTiny = 1e-15;
     
      VAR
        Power : Double; { Value before sign correction }
     
      BEGIN
        Power := 0;
        { Deal with the near zero special cases }
        IF (Abs(Base) < cTiny) THEN BEGIN
          Base := 0.0;
        END; { IF }
        IF (Abs(Exponent) < cTiny) THEN BEGIN
          Exponent := 0.0;
        END; { IF }
     
        { Deal with the exactly zero cases }
        IF (Base = 0.0) THEN BEGIN
          Power := 0.0;
        END; { IF }
        IF (Exponent = 0.0) THEN BEGIN
          Power := 1.0;
        END; { IF }
     
        { Cover everything else }
        IF ((Base < 0) AND (Exponent < 0)) THEN
            Power := 1/Exp(-Exponent*Ln(-Base))
        ELSE IF ((Base < 0) AND (Exponent >= 0)) THEN
            Power := Exp(Exponent*Ln(-Base))
        ELSE IF ((Base > 0) AND (Exponent < 0)) THEN
            Power := 1/Exp(-Exponent*Ln(Base))
        ELSE IF ((Base > 0) AND (Exponent >= 0)) THEN
            Power := Exp(Exponent*Ln(Base));
     
        { Correct the sign }
        IF ((Base < 0) AND (Frac(Exponent/2.0) <> 0.0)) THEN
          Result := -Power
        ELSE
          Result := Power;
      END; { FUNCTION Pow }
    Merci, c'est ce qu'il me fallait

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [vb.net]Gestion des exceptions avec les web services
    Par mvr dans le forum Windows Forms
    Réponses: 2
    Dernier message: 05/12/2005, 22h41
  2. XML avec power point
    Par NOURE dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 09/11/2005, 14h54
  3. Réponses: 2
    Dernier message: 14/02/2005, 14h26
  4. Réponses: 3
    Dernier message: 09/11/2004, 14h43
  5. INSO Filter : "USER-defined exception" avec ctx_do
    Par Wiztiti dans le forum Oracle
    Réponses: 2
    Dernier message: 01/06/2004, 16h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo