473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Jun 30 '08 #1
7 10252
In article <da23fd45-4729-4a98-8a99-
b9**********@l28g2000prd.googlegroups.com>, am********@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.
Jun 30 '08 #2
am********@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/help/tpl/...ongtyping.html
Jun 30 '08 #3
Jerry Coffin wrote:
In article <da23fd45-4729-4a98-8a99-
b9**********@l28g2000prd.googlegroups.com>, am********@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).
Jun 30 '08 #4
On Jun 30, 1:45*pm, 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
Jun 30 '08 #5
On Jun 30, 7:06*am, 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
Jul 1 '08 #6
On Jun 30, 9:10*pm, Greg Herlihy <gre...@mac.comwrote:
On Jun 30, 7:06*am, 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

Jul 2 '08 #7
On Jul 1, 5:45*pm, Ivan Novick <i...@novickmail.comwrote:
On Jun 30, 9:10*pm, Greg Herlihy <gre...@mac.comwrote:
On Jun 30, 7:06*am, 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

Jul 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
0
by: tonvandenheuvel | last post by:
Hi all, please consider the following piece of code: #include <iostream> template<typename T> class A { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.