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

 C Discussion :

Problème cast entier/réel


Sujet :

C

  1. #1
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut Problème cast entier/réel
    bonjour,

    Le problème est le suivant : au calcul des ratio, je peux obtenir des nombre réels donc j'ai déclaré en float la struture cependant en faisant un printf je m'aperçois que ceux qui devraient être réels sont en fait entiers alors que 1,5 n'est pas entier ! Je ne vois pas d'où cela vient !


    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    struct cout_fer
    {
           float astroport;
           float faculte;
           float centre_bancaire;
           float cente_experts;
           float gestion_batiments;
           float gestion_ressources;
           float gestion_unite;
           float terminal;
           float mine_fer;
           float mine_carbone;
           float mine_or;
           float usine_armement;
           float usine_assemblage;
           float usine_recyclage;
           float hangar_blinde_carbone;
           float hangar_blinde_fer;
           float hangar_blinde_or;
           float hangar_blinde_robots;
           float hangar_blinde_armement;
           float crypto_modem;
           float teleporteur_batiments;
           float teleporteur_matiere;
           float teleporteur_unite;
           float vortex;
    };
     
    struct cout_carbone
    {
           float astroport;
           float faculte;
           float centre_bancaire;
           float cente_experts;
           float gestion_batiments;
           float gestion_ressources;
           float gestion_unite;
           float terminal;
           float mine_fer;
           float mine_carbone;
           float mine_or;
           float usine_armement;
           float usine_assemblage;
           float usine_recyclage;
           float hangar_blinde_carbone;
           float hangar_blinde_fer;
           float hangar_blinde_or;
           float hangar_blinde_robots;
           float hangar_blinde_armement;
           float crypto_modem;
           float teleporteur_batiments;
           float teleporteur_matiere;
           float teleporteur_unite;
           float vortex;
    };
     
    struct cout_or
    {
           float astroport;
           float faculte;
           float centre_bancaire;
           float cente_experts;
           float gestion_batiments;
           float gestion_ressources;
           float gestion_unite;
           float terminal;
           float mine_fer;
           float mine_carbone;
           float mine_or;
           float usine_armement;
           float usine_assemblage;
           float usine_recyclage;
           float hangar_blinde_carbone;
           float hangar_blinde_fer;
           float hangar_blinde_or;
           float hangar_blinde_robots;
           float hangar_blinde_armement;
           float crypto_modem;
           float teleporteur_batiments;
           float teleporteur_matiere;
           float teleporteur_unite;
           float vortex;
    };
     
    struct cout_credit
    {
           float astroport;
           float faculte;
           float centre_bancaire;
           float cente_experts;
           float gestion_batiments;
           float gestion_ressources;
           float gestion_unite;
           float terminal;
           float mine_fer;
           float mine_carbone;
           float mine_or;
           float usine_armement;
           float usine_assemblage;
           float usine_recyclage;
           float hangar_blinde_carbone;
           float hangar_blinde_fer;
           float hangar_blinde_or;
           float hangar_blinde_robots;
           float hangar_blinde_armement;
           float crypto_modem;
           float teleporteur_batiments;
           float teleporteur_matiere;
           float teleporteur_unite;
           float vortex;
    };
     
    struct ratio_mine_fer
    {
           float fer;
           float carbone;
           float or;
           float credit;
           float carburant;
    };
     
     
    int main(int argc, char *argv[])
    {
     
        int lvl=12;
     
        struct cout_fer       *cout_fer;
        struct cout_carbone   *cout_carbone;
        struct cout_or        *cout_or;
        struct cout_credit    *cout_credit;
        struct ratio_mine_fer ratio_mine_fer;
     
        cout_fer     = malloc(  100*sizeof( struct cout_fer     )  );
        cout_carbone = malloc(  100*sizeof( struct cout_carbone )  );
        cout_or      = malloc(  100*sizeof( struct cout_or      )  );
        cout_credit  = malloc(  100*sizeof( struct cout_credit  )  );
     
        ratio_mine_fer.fer     = (30/20 );
        ratio_mine_fer.carbone = (137/90);
        ratio_mine_fer.or      = (67/44 );
        ratio_mine_fer.credit  = (8/5   );
     
        printf("%f\n%f\n%f\n%f\n" , ratio_mine_fer.fer ,ratio_mine_fer.carbone ,ratio_mine_fer.or ,ratio_mine_fer.credit );
     
        getch();
     
        cout_fer    [lvl].mine_fer = ( int )(  20*pow( ratio_mine_fer.fer     ,lvl-1 )  );
        cout_carbone[lvl].mine_fer = ( int )(  90*pow( ratio_mine_fer.carbone ,lvl-1 )  );
        cout_or     [lvl].mine_fer = ( int )(  44*pow( ratio_mine_fer.or      ,lvl-1 )  );
        cout_credit [lvl].mine_fer = ( int )(   5*pow( ratio_mine_fer.credit  ,lvl-1 )  );
     
        printf( "fer=%f\ncarbonne=%f\nor=%f\ncredit=%f ", cout_fer    [lvl].mine_fer
                                                        , cout_carbone[lvl].mine_fer
                                                        , cout_or     [lvl].mine_fer
                                                        , cout_credit [lvl].mine_fer );
     
        getch();
    }

    Merci
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
        ratio_mine_fer.fer     = ( 30.0/20.0);
        ratio_mine_fer.carbone = (137.0/90.0);
        ratio_mine_fer.or      = ( 67.0/44.0);
        ratio_mine_fer.credit  = (  8.0/ 5.0);
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    Merci pour la rapidité ! ça marche impec, merci.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

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

Discussions similaires

  1. [POI] Excel, problème interprétation entier en réel
    Par NoxXx73 dans le forum Documents
    Réponses: 1
    Dernier message: 23/06/2011, 11h59
  2. Problème entre entiers et réels
    Par Caroline_1 dans le forum Fortran
    Réponses: 5
    Dernier message: 08/03/2009, 13h02
  3. Problème Cast avec NULL values
    Par WwiloO dans le forum Langage SQL
    Réponses: 1
    Dernier message: 13/10/2005, 10h49
  4. problème d'entier
    Par boucher_emilie dans le forum ASP
    Réponses: 7
    Dernier message: 13/07/2004, 16h52

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