Page 1 of 2

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

Unread postPosted: 28 Apr 2021, 18:15
by Bobb
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 ClaudeBot [spider] ?

Re: Problème de conversion int en C

Unread postPosted: 28 Apr 2021, 18:23
by Ti64CLi++
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 ;)

Re: Problème de conversion int en C

Unread postPosted: 28 Apr 2021, 18:28
by Bobb
Je n'ai pas trop compris et je ne vois pas ce que je dois faire pour convertir mon nombre sans ce problème.

Re: Problème de conversion int en C

Unread postPosted: 28 Apr 2021, 18:31
by MateoConLechuga
Code: Select all
#include <stdio.h>

int main()
{
    long double nombre1=414275475754765.6575685;

    long int nombre2=nombre1;

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

    return 0;
}

Re: Problème de conversion int en C

Unread postPosted: 28 Apr 2021, 18:31
by jacobly
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.

Re: Problème de conversion int en C

Unread postPosted: 28 Apr 2021, 18:36
by Bobb
Thank you very much, It works with a long int.

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

Unread postPosted: 28 Apr 2021, 19:03
by SlyVTT
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

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

Unread postPosted: 28 Apr 2021, 19:25
by SlyVTT
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

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

Unread postPosted: 28 Apr 2021, 19:28
by SlyVTT
Strange, the message I was answering to has just disappeared :comprends_po:

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

Unread postPosted: 28 Apr 2021, 20:12
by jacobly
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;