|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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). |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|