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? 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.
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).
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)...
|
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...
|
by: Aaron Queenan |
last post by:
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
|
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...
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
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...
|
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...
|
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...
|
by: tonvandenheuvel |
last post by:
Hi all,
please consider the following piece of code:
#include <iostream>
template<typename T>
class A
{
public:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |