PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Implicit conversion from int to double
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Implicit conversion from int to double

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 15h06   #1
amphetaman@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Implicit conversion from int to double

I know this might seem kind of silly, but is it necessary to cast int
to double?

Do I have to do

int i = 42;
double d = static_cast<double>(i);

or can I just do d = i?
  Réponse avec citation
Vieux 30/06/2008, 15h47   #2
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

In article <da23fd45-4729-4a98-8a99-
b9ef4dd8800a@l28g2000prd.googlegroups.com>, amphetaman@gmail.com says...
> I know this might seem kind of silly, but is it necessary to cast int
> to double?
>
> Do I have to do
>
> int i = 42;
> double d = static_cast<double>(i);
>
> or can I just do d = i?


The language allows you to do the assignment without an explicit type
cast. Somewhere, I suppose there might be a compiler that warns you
about doing it, but that's a bit hard to guess -- someday I might create
a web site of "inane compiler warnings", but I haven't yet...

--
Later,
Jerry.

The universe is a figment of its own imagination.
  Réponse avec citation
Vieux 30/06/2008, 16h16   #3
Tim Love
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

amphetaman@gmail.com writes:

>I know this might seem kind of silly, but is it necessary to cast int
>to double?


>Do I have to do


>int i = 42;
>double d = static_cast<double>(i);


>or can I just do d = i?

FYI I made a little quiz about this kind of stuff at
http://www-h.eng.cam.ac.uk//tpl/...ongtyping.html
  Réponse avec citation
Vieux 30/06/2008, 21h45   #4
Sjouke Burry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

Jerry Coffin wrote:
> In article <da23fd45-4729-4a98-8a99-
> b9ef4dd8800a@l28g2000prd.googlegroups.com>, amphetaman@gmail.com says...
>> I know this might seem kind of silly, but is it necessary to cast int
>> to double?
>>
>> Do I have to do
>>
>> int i = 42;
>> double d = static_cast<double>(i);
>>
>> or can I just do d = i?

>
> The language allows you to do the assignment without an explicit type
> cast. Somewhere, I suppose there might be a compiler that warns you
> about doing it, but that's a bit hard to guess -- someday I might create
> a web site of "inane compiler warnings", but I haven't yet...
>

My compileer warns, when losing precision,
so d=i is oke, i=d gives warning(warning level set high).
  Réponse avec citation
Vieux 30/06/2008, 21h58   #5
Ivan Novick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

On Jun 30, 1:45pm, Sjouke Burry <burrynulnulf...@ppllaanneett.nnlll>
wrote:
> Jerry Coffin wrote:
> > In article <da23fd45-4729-4a98-8a99-
> > b9ef4dd88...@l28g2000prd.googlegroups.com>, ampheta...@gmail.com says....
> >> I know this might seem kind of silly, but is it necessary to cast int
> >> to double?

>
> >> Do I have to do

>
> >> int i = 42;
> >> double d = static_cast<double>(i);

>
> >> or can I just do d = i?

>
> > The language allows you to do the assignment without an explicit type
> > cast. Somewhere, I suppose there might be a compiler that warns you
> > about doing it, but that's a bit hard to guess -- someday I might create
> > a web site of "inane compiler warnings", but I haven't yet...

>
> My compileer warns, when losing precision,
> so d=i is oke, i=d gives warning(warning level set high).


This is covered in the C++ standard 4.9 "floating-integral
conversions".

Converting from an int to double should be pretty intuitive. The rule
when converting from double to int is to truncate the value.
Technically, the compiler should not have to warn in this case, but i
guess the compiler team felt normally this was an error in coding when
someone truncates by assigning a floating point to an integer.

Ivan Novick
http://www.mycppquiz.com
  Réponse avec citation
Vieux 01/07/2008, 05h10   #6
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

On Jun 30, 7:06am, ampheta...@gmail.com wrote:
> I know this might seem kind of silly, but is it necessary to cast int
> to double?
>
> Do I have to do
>
> int i = 42;
> double d = static_cast<double>(i);
>
> or can I just do d = i?


The static_cast<> is not needed to assign an int to a double. If you
still wish to make the int-to-double conversion explicit, then, I
would use the more compact, functional notation instead of a
static_cast<>:

int i = 42;
double d = double(i);

Greg
  Réponse avec citation
Vieux 02/07/2008, 01h45   #7
Ivan Novick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

On Jun 30, 9:10pm, Greg Herlihy <gre...@mac.com> wrote:
> On Jun 30, 7:06am, ampheta...@gmail.com wrote:
>
> > I know this might seem kind of silly, but is it necessary to cast int
> > to double?

>
> > Do I have to do

>
> > int i = 42;
> > double d = static_cast<double>(i);

>
> > or can I just do d = i?

>
> The static_cast<> is not needed to assign an int to a double. If you
> still wish to make the int-to-double conversion explicit, then, I
> would use the more compact, functional notation instead of a
> static_cast<>:
>
> int i = 42;
> double d = double(i);
>
> Greg


That being said, its still functionally identical to d=i; correct?

Ivan Novick
http://www.mycppquiz.com

  Réponse avec citation
Vieux 02/07/2008, 04h37   #8
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implicit conversion from int to double

On Jul 1, 5:45pm, Ivan Novick <i...@novickmail.com> wrote:
> On Jun 30, 9:10pm, Greg Herlihy <gre...@mac.com> wrote:
> > On Jun 30, 7:06am, ampheta...@gmail.com wrote:

>
> > > I know this might seem kind of silly, but is it necessary to cast int
> > > to double?

>
> > > Do I have to do

>
> > > int i = 42;
> > > double d = static_cast<double>(i);

>
> > > or can I just do d = i?

>
> > The static_cast<> is not needed to assign an int to a double. If you
> > still wish to make the int-to-double conversion explicit, then, I
> > would use the more compact, functional notation instead of a
> > static_cast<>:

>
> > int i = 42;
> > double d = double(i);

>
> That being said, its still functionally identical to d=i; correct?


Yes. About the only conceivable reason for using the cast in this case
would be to document that the int-to-double conversion is intentional.

Greg



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17491 seconds with 16 queries