Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 22/02/2014, à 12:58

ubuntuforce

Calculette mutibases [Casse tête en C] [résolu].

Salut les C,
Comme le titre l'indique je bosse sur un projet de calculette avec entres autres fonctions de conversion et de calcule dans les 4 bases suivantes:

-Décimale.
-Binaire.
-Octal.
-Hexadécimale.

Bon après avoir construit les fonctions de conversion et de calcule j'ai attaquer une GUI avec ncurses, après une série d 'assertions:

-) 10000 random values pour les entiers dans toutes les bases: résultat de type long long ou le string binaire, octal, hexadécimale correspondant.
-) 10000 random values pour les floats dans toutes les bases: résultats de type long double ou le string binaire, octal, hexadécimale correspondant.

Passer plusieurs fois a la moulinette... c'est du tout cuit pour la GUI.

Mais ânes ignârd que je suis j'ai oublier un petit détails:
Les valeurs négatives !!!

Je me serai bien passer de votre aide, excusez la forme, mais je bute sur le fait que:

Brefs après avoir essayer en négatif,
ça passe en résultat de type (double) négatif pour la base binaire mais pas pour les autres bases (décimale omise).
Et comme les fonctions de conversions sont très analogue je ne comprend pas pourquoi le binaire ça passe et pas les autres... I Hit a -Wall.

Après mon séjours a l'hôpital pour commotion cérébrale je poste un process SOS:

Voici le code de mes assertions pour vous mettre l'eau a la bouche mais si vous voulez m'aider il faudra télécharger les sources (y pas tant de sources que ça et c'est tout commenter).

/*
 * hobdcalc 
 * Copyright (C) 2014 Bruggemann Eddie.
 * 
 * This file is part of hobdcalc  .
 * hobdcalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * hobdcalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with hobdcalc. If not, see <http://www.gnu.org/licenses/>
 * 
 ************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <tgmath.h>
#include <ctype.h>
#include <limits.h>
#include <inttypes.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include <sys/time.h>

#include "conversion/utils/conversion_utils.c"

#include "conversion/int_hex/calc_int_hex.c"
#include "conversion/int_bin/calc_int_bin.c"
#include "conversion/int_oct/calc_int_oct.c" 


#include "conversion/float_to_bin/float_bin.c"
#include "conversion/float_to_oct/float_oct.c"
#include "conversion/float_to_hex/float_hex.c"

#include "operations/int_result/calc_hex_int_operations.c"
#include "operations/int_result/calc_oct_int_operations.c"
#include "operations/int_result/calc_bin_int_operations.c"

#include "operations/float_result/calc_hex_float_operations.c"
#include "operations/float_result/calc_oct_float_operations.c"
#include "operations/float_result/calc_bin_float_operations.c"


/* hobdcalc can compute in the 
 * -) binar
 * -) octal
 * -) hexadecimal
 * bases without problems with unsigned values.
 * 
 * but i hit a wall because:
 * the binar computing work with signed values for floating operations 
 * and not in octal and hexadecimal bases, and the signed integer computing.
 * because the computing functions are very similary that is why i don't understand what go wrong !!!  
 * I it the -Wall -Wall -Wall !!!
 * 
 * I hope you will have no problems with the code who is in my very simplistic beginner style. 
 *
 * You must link the type generic math file with:
 * $ gcc hobdcalc.c -lm
 * to compil the code.
 * 
 * Thank's to everybody who try to help me, 
 * if the "C HEADS break this ugly -Wall i it in"
 * thank's to send the result of their work to:
 * mrcyberfighter@gmail.com
 * or forking this code what is authorize by his author throught the GPLv3 License. 
 * 
 * For information:
 * This programm has an ncurses user interface not provided here in case as long this big bug
 * will not be resolved. 
 * 
 * Note: I don't care about strtold() or strtoll() and the %x %o %a placeholders, only raw computing, because i made
 *       bad experience with it during developpment and testing.    
 * ******************************************************************************************************************/

void float_operations_test(void) ;

void integer_operations_test(void) ;

int main() {
  /** float_operations_test() ; */
  /** integer_operations_test() ; */
  return 0 ;
  
}

void float_operations_test(void) {
  long double op1,op2 ;
  struct timeval tv ;
  char *op1_str=malloc(128) ;
  char *op2_str=malloc(128) ;
  int c ;
  
  for (c=0 ; c < 10000 ; c++) {
    
    gettimeofday(&tv,NULL) ;
    srand(tv.tv_usec / 4) ;
    
    op1=(double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ;
    /* set: 
     * op1 = - (double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ; // (long double) not work !!!
     * for help bugfix tests.
     * Thank's for your collaboration and your help.
     ******************************************/
    
    srand(tv.tv_usec / 3) ;
    op2=(double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ;
    /* set: 
     * op2 = - (double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ; // (long double) not work !!!
     * for help bugfix tests.
     * Thank's for your collaboration and your help.
     ******************************************/
    
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    floattobinfloat(op1,op1_str) ;
    floattobinfloat(op2,op2_str) ;
    if (c % 10 == 0) {
      printf("iteration: %d\n",c) ;
    }
    if (op1 + op2 != binfloataddbinfloat(op1_str,op2_str) ) {
      printf("assertion bin error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,binfloataddbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 - op2 != binfloatsubbinfloat(op1_str,op2_str) ) {
      printf("assertion bin error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,binfloatsubbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 * op2 != binfloatmultbinfloat(op1_str,op2_str) ) {
      printf("assertion bin error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,binfloatmultbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != binfloatdivbinfloat(op1_str,op2_str) ) {
      printf("assertion bin error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,binfloatdivbinfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      printf("%Lf\n%Lf\n%Lf\n%Lf\n",binfloataddbinfloat(op1_str,op2_str),binfloatsubbinfloat(op1_str,op2_str),binfloatmultbinfloat(op1_str,op2_str),binfloatdivbinfloat(op1_str,op2_str)) ;
    }
   
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    floattohexfloat(op1,op1_str) ;
    floattohexfloat(op2,op2_str) ;
    op1_str[128]='\0' ;
    op2_str[128]='\0' ;
    if (op1 + op2 != hexfloataddhexfloat(op1_str,op2_str) ) {
      printf("assertion hex error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,hexfloataddhexfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 - op2 != hexfloatsubhexfloat(op1_str,op2_str) ) {
      printf("assertion hex error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,hexfloatsubhexfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 * op2 != hexfloatmulthexfloat(op1_str,op2_str) ) {
      printf("assertion hex error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,hexfloatmulthexfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != hexfloatdivhexfloat(op1_str,op2_str) ) {
      printf("assertion hex error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,hexfloatdivhexfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      printf("%Lf\n%Lf\n%Lf\n%Lf\n",hexfloataddhexfloat(op1_str,op2_str),hexfloatsubhexfloat(op1_str,op2_str),hexfloatmulthexfloat(op1_str,op2_str),hexfloatdivhexfloat(op1_str,op2_str)) ;
    }
    
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    floattooctfloat(op1,op1_str) ;
    floattooctfloat(op2,op2_str) ;
    if (op1 + op2 != octfloataddoctfloat(op1_str,op2_str) ) {
      printf("add assertion octal error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,octfloataddoctfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 - op2 != octfloatsuboctfloat(op1_str,op2_str) ) {
      printf("sub assertion octal error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,octfloatsuboctfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 * op2 != octfloatmultoctfloat(op1_str,op2_str) ) {
      printf("mult assertion oct error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,octfloatmultoctfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != octfloatdivoctfloat(op1_str,op2_str) ) {
      printf("div assertion oct error by iteration %Lf:\nop1: %s == %Lf\nop2: %s == %Lf\nresult: %Lf\nerror: %Lf\n",op1,op1_str,op1,op2_str,op2,op1+op2,octfloatdivoctfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      printf("%Lf\n%Lf\n%Lf\n%Lf\n",octfloataddoctfloat(op1_str,op2_str),octfloatsuboctfloat(op1_str,op2_str),octfloatmultoctfloat(op1_str,op2_str),octfloatdivoctfloat(op1_str,op2_str)) ;
    }
    
    
  }
  free(op1_str) ;
  free(op2_str) ;
  
  return  ;
}

void integer_operations_test(void) {
  int c ;
  long long op1,op2 ;
  char *op1_str, *op2_str ;
  op1_str=malloc(128) ;
  
  op2_str=malloc(128) ;
  
  struct timeval tv ;
  
  for (c=0 ; c < 10000 ; c++) {
    srand(tv.tv_usec /2 ) ;
    
    op1=rand() % LLONG_MAX ;
    /* set: 
     * op1 = - rand() % LLONG_MAX ; 
     * for help bugfix tests.
     * Thank's for your collaboration and your help.
     ******************************************/
    
    srand(tv.tv_usec / 5 ) ;
    
    op2=rand() % LLONG_MAX ;
    /* set: 
     * op2 = - rand() % LLONG_MAX ; 
     * for help bugfix tests.
     * Thank's for your collaboration and your help.
     ******************************************/
    
    
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    inttobin(op1,op1_str) ;
    inttobin(op2,op2_str) ;
    
    if (! (op1 + op2 == binaddbin(op1_str,op2_str) ) ) {
      printf("bin add error\n") ;
      break ;
    }
    if (! (op1 - op2 == binsubbin(op1_str,op2_str)) ) {
      printf("bin sub error\n") ;
      break ;
    }
    if (! (op1 * op2 == binmultbin(op1_str,op2_str)) ) { 
      printf("bin mult error\n") ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == bindivbin(op1_str,op2_str)) ) {
      printf("bin div error\n") ;
      break ;
    }
    printf("-------------------------------------------------------------\n") ;
  
  
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    inttooct(op1,op1_str) ;
    inttooct(op2,op2_str) ;
    if (! (op1 + op2 == octaddoct(op1_str,op2_str) ) ) {
	printf("oct add error\n") ;
	break ;
      }
    if (! (op1 - op2 == octsuboct(op1_str,op2_str)) ) {
      printf("oct sub error\n") ;
      break ;
    }
    if (! (op1 * op2 == octmultoct(op1_str,op2_str)) ) { 
      printf("oct mult error\n") ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == octdivoct(op1_str,op2_str)) ) {
      printf("oct div error\n") ;
      break ;
    }

    printf("-------------------------------------------------------------\n") ;
  
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ; 
    inttohex(op1,op1_str) ;
    inttohex(op2,op2_str) ;
    if (! (op1 + op2 == hexaddhex(op1_str,op2_str) ) ) {
	printf("hex add error\n") ;
	break ;
    }
    if (! (op1 - op2 == hexsubhex(op1_str,op2_str)) ) {
      printf("hex sub error\n") ;
      break ;
    }
    if (! (op1 * op2 == hexmulthex(op1_str,op2_str)) ) { 
      printf("hex mult error\n") ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == hexdivhex(op1_str,op2_str)) ) {
      printf("hex div error\n") ;
      break ;
    }

    printf("-------------------------------------------------------------\n") ;
  }
  free(op1_str) ;
  free(op2_str) ;
  return ;
  
}

Vous pouvez certes me laisser a mon triste sort mais ça serai un homicide par négligence car la tête ne résiste pas longtemps propulser a de maintes reprises contre un mur.
Je comprendrai.

Il faut savoir que les fonctions sont très basiques ainsi que mon style et je pense que vous n'aurez pas de mal a me dire ou est le problème si je suis encore vivant d'ici la.

Dernière modification par ubuntuforce (Le 28/02/2014, à 17:42)


Rendez-vous sur mon site présentant mes créations open-source: http://www.open-source-projects.net/
Rendez-vous sur mon site dédier a mes créations d'imagerie 3D: http://www.3dreaming-imaging.net/
Testez pendant une semaine l'éditeur avec terminaux intégrées http://www.open-source-projects.net/it-edit/it-edit Vous l'adopterai sûrement !

Hors ligne

#2 Le 22/02/2014, à 13:39

dva2tlse

Re : Calculette mutibases [Casse tête en C] [résolu].

t'es p'têt bon en C, mais pas tellement en orthographe ni pour les accords, alors a'tchao'


xubuntu 22.04 dans un PC assemblé
PS: Dis toto, pourquoi l'univers existe-t'il ?
Je vais y réfléchir avec Morphée et lui dès avant 22h55, donc ici, il faut se contacter auparavant.

Hors ligne

#3 Le 22/02/2014, à 15:01

ubuntuforce

Re : Calculette mutibases [Casse tête en C] [résolu].

J'ai une excuse signer du medecin, pour la dislexie entre autres, merci pour le commentaire et a'tchao'.


Rendez-vous sur mon site présentant mes créations open-source: http://www.open-source-projects.net/
Rendez-vous sur mon site dédier a mes créations d'imagerie 3D: http://www.3dreaming-imaging.net/
Testez pendant une semaine l'éditeur avec terminaux intégrées http://www.open-source-projects.net/it-edit/it-edit Vous l'adopterai sûrement !

Hors ligne

#4 Le 23/02/2014, à 04:53

melixgaro

Re : Calculette mutibases [Casse tête en C] [résolu].

coucou,

je ne suis pas sûr de pouvoir t'aider donc je te demande des compléments d'info plus par curiosité (mais ça pourrait aussi être utile à d'autres, hein). Pourquoi parles-tu de « random values » ? Pour moi, une calculatrice/un programme de conversion se contente de prendre en entrée un nombre et de renvoyer un résultat à l'utilsateur. Des euros en réals. Des mètres en nanomètres. Des entiers en base 10 en entiers en base 2 ou 16. Bref, à lire valeurs aléatoires, je pense plus à des simulations et de la statistique qu'à une simple conversion de base ou à des calculs dans une base donnée. Ou alors j'ai loupé complètement ce que tu veux faire avec ces nombres aléatoires ?


Linux depuis ~2007. Xubuntu seulement.

Hors ligne

#5 Le 23/02/2014, à 15:06

ubuntuforce

Re : Calculette mutibases [Casse tête en C] [résolu].

Salut melixgaro, pour faire court,
quand on crée un programme on vérifie le bon fonctionnement des fonctions qui le compose.
Dans mon cas, l'utilisateur de la calculette peut entrer n'importe quelles valeurs (et base), donc je vérifie, par assertions que les fonctions que la calculette utlise(ra) ne se trompe pas...

Donc je prends des valeurs aléatoires (random values) afinde tester 10000 fois (dans une boucle, avec chaque fois une valeur différente) que les fonctions ne se trompe pas car
par exemple disons un calcul en base 16 (hexadécimal) mes assertions:

-Convertissent 2 valeurs aléatoires en hexadécimale.
-Effectue le calcule
-Et reconvertisse le résultat en entier ou a virgule selon le cas.

Puis vérifie par simple calcule que le résultat renvoyer par l'ordinateur en base 10 est le même que celui de mes fonctions:

Peut-être comprendra tu mieux ce pseudo code:

value1 = random value in base 10 ;
value2 = random value in base 10 ;
hexvalue1 = résultat de convertir value1 en base 16 (hex)  ;
hexvalue2 = résultat de convertir value2 en base 16 (hex) ;
hexres = calcule_en_base_16( hexvalue1 + hexvalue2 )  ;
res = résultat de convertir hexres en base 10 ;
si    (value1 + value2)     différent de    res  {
    s'arrêter afficher erreur ;
}

 

Ce qui permet de tester entre autre
que le calcule directe en base 16 donne le bon résultat (si l'utilisateur entre des valeurs de calcul en base 16)
en passant par les fonctions de conversion.
car l'ordinateur sait calculer en base 10 de toute façon.

Désolé pour la réponse rapide chaotique je suis presser.

Dernière modification par ubuntuforce (Le 23/02/2014, à 15:53)


Rendez-vous sur mon site présentant mes créations open-source: http://www.open-source-projects.net/
Rendez-vous sur mon site dédier a mes créations d'imagerie 3D: http://www.3dreaming-imaging.net/
Testez pendant une semaine l'éditeur avec terminaux intégrées http://www.open-source-projects.net/it-edit/it-edit Vous l'adopterai sûrement !

Hors ligne

#6 Le 23/02/2014, à 18:38

melixgaro

Re : Calculette mutibases [Casse tête en C] [résolu].

merci pour ta réponse.
Mais j'avoue rester perplexe. Pour du calcul ou de la conversion, il suffit d'appliquer une formule. À moins de mal coder la formule, il n'y a pas tellement de mystère sur le résultat final. Si je veux construire un convertisseur de mètres en pouces, Je code le facteur de conversion et c'est tout. Je ne vais pas faire un tirage de N valeurs aléatoires pour vérifier mon code. J'ai tendance à penser que soit ça marche, soit ça ne marche pas (bien sûr, il ne faut pas tester avec des valeurs pathologiques, genre zéro qui ne permet pas de tester ma fonction de conversion ou de calcul).

Sinon, j'ai trouvé cette article wikipedia sur la manipulation des entiers relatifs et de leur conversion en binaire. Je ne peux pas plus aider. Les seules fois où je m'amuse à faire des conversions, j'utilise des entiers naturels (donc positifs).


Linux depuis ~2007. Xubuntu seulement.

Hors ligne

#7 Le 24/02/2014, à 00:20

ubuntuforce

Re : Calculette mutibases [Casse tête en C] [résolu].

La calculette dispose de 4 modes d'entrée différents:
-Décimal
-Binaire
-Octal
-Hexadécimal.
et fonctions de conversion d'une base vers une autre entre autre.

Merci pour ton lien que j'ai vaguement regarder et je connais vaguement la représentation interne des entiers signé.

Dernière modification par ubuntuforce (Le 24/02/2014, à 00:22)


Rendez-vous sur mon site présentant mes créations open-source: http://www.open-source-projects.net/
Rendez-vous sur mon site dédier a mes créations d'imagerie 3D: http://www.3dreaming-imaging.net/
Testez pendant une semaine l'éditeur avec terminaux intégrées http://www.open-source-projects.net/it-edit/it-edit Vous l'adopterai sûrement !

Hors ligne

#8 Le 28/02/2014, à 17:42

ubuntuforce

Re : Calculette mutibases [Casse tête en C] [résolu].

J'ai finalement réussi a coder le nombres négatif aussi et mis
mon ensemble de fonctions de conversion et de calcul (en base 12, 8, 2, 10) a disposition de tous.


Rendez-vous sur mon site présentant mes créations open-source: http://www.open-source-projects.net/
Rendez-vous sur mon site dédier a mes créations d'imagerie 3D: http://www.3dreaming-imaging.net/
Testez pendant une semaine l'éditeur avec terminaux intégrées http://www.open-source-projects.net/it-edit/it-edit Vous l'adopterai sûrement !

Hors ligne

#9 Le 05/03/2014, à 15:07

fabkzo

Re : Calculette mutibases [Casse tête en C] [résolu].

@ubuntuforce
Rien à voir avec ton code mais j'ai cliqué sur le lien présent dans ta signature et menant vers ton site en python et là j'ai les yeux qui saignent, ça m'a relancé ma conjonctivite.
Tu veux nous rendre aveugles ou tu regardes tes écrans avec des verres protégés?

Dernière modification par fabkzo (Le 05/03/2014, à 15:07)


pinterest ==> fabkzo!
Python beginner on Rpi
http://www.fabkzo.com

Hors ligne