OVH Cloud OVH Cloud

Problème de type flottant

6 réponses
Avatar
fragmonster
Bonjour,
Je dois mal m'y prendre mais l=E0 je s=E8che (vendredi soir
peut-=EAtre...)
Bref, je d=E9clare une variable comme ceci :

float ratio =3D (i.Width/i.Height); (o=F9 i est une image)

Lorsque j'affiche dans la console mon ration, j'obtient toujours un
nombre entier.

J'ai beau changer de type (Double, Single ...), caster, convertir ...
etc rien n'y fait je me retrouve toujours avec un nombre entier.

Voici mon petit bout de code :

System.Drawing.Image i =3D System.Drawing.Image.FromFile(filename);
float ratio =3D (i.Width/i.Height);
Console.WriteLine(" W =3D "+i.Width + " - H =3D "+i.Height + " R =3D" +
ratio);

J'obtient =E0 l'affichage :=20
W =3D 583 - H =3D 172 - R =3D 3

Je craque.

Merci

6 réponses

Avatar
fragmonster
A moins que la conversion implicite en String dans le Console.WriteLine
force l'affichage ...
Avatar
Michael Moreno
essaie ceci :
float ratio = (float)i.Width / i.Height;

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Avatar
Boris Sargos
fragmonster a écrit :
Bonjour,
Je dois mal m'y prendre mais là je sèche (vendredi soir
peut-être...)
Bref, je déclare une variable comme ceci :

float ratio = (i.Width/i.Height); (où i est une image)

Lorsque j'affiche dans la console mon ration, j'obtient toujours un
nombre entier.

J'ai beau changer de type (Double, Single ...), caster, convertir ...
etc rien n'y fait je me retrouve toujours avec un nombre entier.

Voici mon petit bout de code :

System.Drawing.Image i = System.Drawing.Image.FromFile(filename);
float ratio = (i.Width/i.Height);
Console.WriteLine(" W = "+i.Width + " - H = "+i.Height + " R =" +
ratio);

J'obtient à l'affichage :
W = 583 - H = 172 - R = 3

Je craque.

Merci



D'après ce que tu écris ça me semble normal.
As-tu essayé : "float ratio = (i.Width/((double)i.Height));" ?
Avatar
fragmonster
Raaahhhh merci ça marche. J'étais pourtant pas passé loin en faisant
un :

float ratio = (float)(i.Width / i.Height);

D'ailleurs, pourquoi ça n'a pas marché comme ça?
Avatar
Mehdi
On 18 Nov 2005 06:52:59 -0800, fragmonster wrote:

Raaahhhh merci ça marche. J'étais pourtant pas passé loin en faisant
un :

float ratio = (float)(i.Width / i.Height);

D'ailleurs, pourquoi ça n'a pas marché comme ça?



Parce que d'apres la doc, l'opérateur / appliqué a 2 entiers retourne un
entier arrondi a la valeur inférieure (dans le cas ou le résultat est
positif) ou supérieure (dans le cas ou le résultat est négatif):

"The predefined division operators are listed below. The operators all
compute the quotient of x and y.

Integer division:
int operator /(int x, int y);
uint operator /(uint x, uint y);
long operator /(long x, long y);
ulong operator /(ulong x, ulong y);
If the value of the right operand is zero, a System.DivideByZeroException
is thrown.

The division rounds the result towards zero, and the absolute value of the
result is the largest possible integer that is less than the absolute value
of the quotient of the two operands."

Quand tu fait (float)(i.Width / i.Height), c'est déja trop tard. Le
résultat de la division a été tronqué en entier avant que tu ne le
reconvertisse en flottant. Il faut qu'au moins un de tes 2 opérandes soit
flottant si tu veux un résultat flottant (solution de Michael).
Avatar
fragmonster
Ah ok, d'où le besoin de faire un cast sur un seul des deux
opérandes.

Merci, j'aurais appris qque chose aujourd'hui ^^