π
<-
Chat plein-écran
[^]

[Résolu] Problème de conversion int en C

Assembleur, Axe, C/C++, ICE...

[Résolu] Problème de conversion int en C

Unread postby Bobb » 28 Apr 2021, 18:15

Bonjour à tous,

Aujourd'hui j'ai un problème en C :

Je ne comprends pas pourquoi ce code :
Code: Select all
#include <stdio.h>

int main()
{
    long double nombre1=414275475754765.6575685; /* création d'un nombre à virgule flottante*/

    int nombre2=nombre1; /* conversion de ce nombre en entier, dans la variable nombre2*/

    printf("%d\n",nombre2); /* affichage du nombre entier, ne donne pas ce que je voudrais, Pourquoi ?*/

    return 0;
}


Me donne ça :

Code: Select all
-2147483648                                                                                                                         
                                                                                                                                     
                                                                                                                                     
...Program finished with exit code 0                                                                                                 
Press ENTER to exit console.


Le nombre flottant converti en entier devrait donner 414275475754765, alors qu'il me donne un nombre négatif complètement différent.

Pouvez-vous m'aider Anonymous ?
Last edited by Bobb on 28 Apr 2021, 18:43, edited 1 time in total.

Tous mes programmes sont disponibles ici

↳ Testez mon simulateur Android sur Ti-83 Premium CE et / ou Édition Python
Jetez un coup d'oeil à mon langage de programmation interprété Neon.

Image
User avatar
BobbProgrammeur
Niveau 10: GR (Guide de Référence)
Niveau 10: GR (Guide de Référence)
Level up: 95.1%
 
Posts: 303
Joined: 19 Apr 2020, 12:37
Location: Morbihan
Gender: Male
Calculator(s):
MyCalcs profile
Class: CPGE MPI

Re: Problème de conversion int en C

Unread postby Ti64CLi++ » 28 Apr 2021, 18:23

Parce que les int ne stockent pas les nombres de la même manière que les double/float. Donc la conversion ne peut marcher immediatement.
Un peu d'explication : https://fr.wikipedia.org/wiki/IEEE_754 ;)
Image
User avatar
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 32.4%
 
Posts: 3444
Images: 75
Joined: 04 Jul 2014, 14:40
Location: Clermont-Ferrand 63
Gender: Male
Calculator(s):
MyCalcs profile
Class: ENS Rennes
GitHub: Ti64CLi

Re: Problème de conversion int en C

Unread postby Bobb » 28 Apr 2021, 18:28

Je n'ai pas trop compris et je ne vois pas ce que je dois faire pour convertir mon nombre sans ce problème.

Tous mes programmes sont disponibles ici

↳ Testez mon simulateur Android sur Ti-83 Premium CE et / ou Édition Python
Jetez un coup d'oeil à mon langage de programmation interprété Neon.

Image
User avatar
BobbProgrammeur
Niveau 10: GR (Guide de Référence)
Niveau 10: GR (Guide de Référence)
Level up: 95.1%
 
Posts: 303
Joined: 19 Apr 2020, 12:37
Location: Morbihan
Gender: Male
Calculator(s):
MyCalcs profile
Class: CPGE MPI

Re: Problème de conversion int en C

Unread postby MateoConLechuga » 28 Apr 2021, 18:31

Code: Select all
#include <stdio.h>

int main()
{
    long double nombre1=414275475754765.6575685;

    long int nombre2=nombre1;

    printf("%ld\n",nombre2);

    return 0;
}
User avatar
MateoConLechugaVIP++
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 60.5%
 
Posts: 50
Joined: 12 Oct 2015, 21:56
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: mateoconlechuga

Re: Problème de conversion int en C

Unread postby jacobly » 28 Apr 2021, 18:31

The int type is typically restricted to the range [-2147483648,2147483647]. If you try to convert a float that is out of range for int to int, it is undefined behavior which means absolutely anything can happen. In actual implementations, the resulting value is often exactly -2147483648 or 2147483647.
User avatar
jacoblyVIP++
Niveau 4: MC (Membre Confirmé)
Niveau 4: MC (Membre Confirmé)
Level up: 16%
 
Posts: 12
Joined: 13 Oct 2016, 04:34
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: jacobly0

Re: Problème de conversion int en C

Unread postby Bobb » 28 Apr 2021, 18:36

Thank you very much, It works with a long int.

Tous mes programmes sont disponibles ici

↳ Testez mon simulateur Android sur Ti-83 Premium CE et / ou Édition Python
Jetez un coup d'oeil à mon langage de programmation interprété Neon.

Image
User avatar
BobbProgrammeur
Niveau 10: GR (Guide de Référence)
Niveau 10: GR (Guide de Référence)
Level up: 95.1%
 
Posts: 303
Joined: 19 Apr 2020, 12:37
Location: Morbihan
Gender: Male
Calculator(s):
MyCalcs profile
Class: CPGE MPI

Re: [Résolu] Problème de conversion int en C

Unread postby SlyVTT » 28 Apr 2021, 19:03

Hi,
anyhow, even if it works, it is generally considered not to be a good idea to use "implicit" conversion.
By far much better to say to the compiler that you are actually asking for a conversion :

Code: Select all
// this is a dummy example, anyhow, it shows the concept
long double temp1 = 1165551515151.25156451;

// we use the On-the-Fly casting to long int.
long int temp2 = (long int) temp1;


What is important is the (long int) on the left side of the equal sign. It says to the compiler that you are asking for a conversion of the following member (temp1, whatever its type) into a long int type to feed into the temp2 variable.

I guess you should at least get a warning a compilation time without using casting.

Ciao

Sly
Last edited by SlyVTT on 28 Apr 2021, 19:12, edited 2 times in total.
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.9%
 
Posts: 496
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

Re: [Résolu] Problème de conversion int en C

Unread postby SlyVTT » 28 Apr 2021, 19:25

May I ask why ?

As we are in a type "degradation" situation, it is then risky not to explicitely ask for a cast.

If it was the opposite (i.e. int to long conversion) I would agree, but usual arithmetic conversion in C are safe only in the type upgrade situation, which means int --> unsigned int --> long --> unsigned long --> long long --> unsigned long long --> float --> double --> long double.

I am surprised of the "tolerance" of the compiler for the direct conversion, at least it should warn, the best would be to point an error cause there are loss of information in such implicit conversion.

Sly
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.9%
 
Posts: 496
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

Re: [Résolu] Problème de conversion int en C

Unread postby SlyVTT » 28 Apr 2021, 19:28

Strange, the message I was answering to has just disappeared :comprends_po:
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.9%
 
Posts: 496
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

Re: [Résolu] Problème de conversion int en C

Unread postby jacobly » 28 Apr 2021, 20:12

How is int -> unsigned safe if it changes -1 to 4294967295? Even converting int to float loses information, albeit with a much smaller error. If you were to disable "unsafe" conversions in C, then that would break a whole bunch of things that you expect to work without thinking about it. Just a few examples that rely on "unsafe" implicit conversions to compile:
Code: Select all
int x = 5;
float y = 12;
char a = (char)8 + (char)17;
User avatar
jacoblyVIP++
Niveau 4: MC (Membre Confirmé)
Niveau 4: MC (Membre Confirmé)
Level up: 16%
 
Posts: 12
Joined: 13 Oct 2016, 04:34
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: jacobly0

Next

Return to Langages alternatifs

Who is online

Users browsing this forum: No registered users and 52 guests

-
Search
-
Social TI-Planet
-
Featured topics
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Offre de test des nouveautés de rentrée 2024 par Casio. Enseignant(e), reçois gratuitement 1 exemplaire, à ton choix, de la Graph Light ou bien de la Graph Math+
14€ remboursés par Casio sur l'achat de ta calculatrice Graph 35 d'ici le 31 Octobre 2024
10€ remboursés par Casio sur l'achat de ta calculatrice Graph 90+E d'ici le 31 Décembre 2024
10€ remboursés par Casio sur l'achat de ta calculatrice Graph 25 d'ici le 31 Décembre 2024
8€ remboursés par Casio sur l'achat de ta calculatrice Graph Math+ d'ici le 31 Octobre 2024
Reprise de ton ancienne fx-92 Collège ou Graph 25/35/90 à 3€ peu importe son état. Même non fonctionnelle et donc invendable, même ancienne Graph 35 non conforme aux programmes (pas de Python), même ancienne Graph 25/35 inutilisable aux examens (pas de mode examen) et donc invendable. Etiquette de retour fournie, pas de frais de port à payer.
3€ remboursés par Casio sur l'achat de ta calculatrice fx-92 Collège d'ici le 30 Septembre 2024
5€ de remise immédiate sur l'achat de ta calculatrice TI-83 Premium CE Edition Python chez les revendeurs partenaires
4€ de remise immédiate sur l'achat de ta calculatrice TI-82 Advanced Edition Python chez les revendeurs partenaires
3€ de remise immédiate sur l'achat de ta calculatrice TI-82 Advanced chez les revendeurs partenaires
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
1234567891011121314
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1243 utilisateurs:
>1221 invités
>17 membres
>5 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)