star90
asked on
VC++ How to convert between double calculation results and long
Hi,
I have 4 variables:
integer integer_var
integer integer_var2
double double_var
long long_var
I need to calculate the long_var variable in the following way:
long_var=(integer_var)*(in teger_var2 )/100)
I wrote it in the following way
long_var=long(double(integ er_var)*do uble(integ er_var2)/1 00)
Is it OK to write it this way?
My program crashes (possibly for other reason) but I noticed that the long_var variable equals to zero althogh (double(integer_var)*doubl e(integer_ var)/100)= 10043.45
If I write the calculation without the double and the long:
long_var=((integer_var)*(i nteger_var )/100)
I get compiler warnings.
What is the correct way to write it?
Thanks
Thanks
I have 4 variables:
integer integer_var
integer integer_var2
double double_var
long long_var
I need to calculate the long_var variable in the following way:
long_var=(integer_var)*(in
I wrote it in the following way
long_var=long(double(integ
Is it OK to write it this way?
My program crashes (possibly for other reason) but I noticed that the long_var variable equals to zero althogh (double(integer_var)*doubl
If I write the calculation without the double and the long:
long_var=((integer_var)*(i
I get compiler warnings.
What is the correct way to write it?
Thanks
Thanks
ASKER
Thanks for the answer.
I checked again and I don't get warnings for it in this case.
There are however cases that I get warning like: "Converting from type double to type integer"
Please explain when do you use the : double(expression)? or the int(expression) or long (expression)?
I checked again and I don't get warnings for it in this case.
There are however cases that I get warning like: "Converting from type double to type integer"
Please explain when do you use the : double(expression)? or the int(expression) or long (expression)?
ASKER
Another example:
elapsed_time=(difftime(lti me2,time_t (0));
I get the warning:
Converting from double to ´unsigned_long´,possible loss of data.
The solution I used:
elapsed_time=unsigned_long (difftime( ltime2,tim e_t(0));
Is it the correct way to solve it?
elapsed_time=(difftime(lti
I get the warning:
Converting from double to ´unsigned_long´,possible loss of data.
The solution I used:
elapsed_time=unsigned_long
Is it the correct way to solve it?
if you try to convert a more soecific data into a less specific data, you will loose something no matter what you do ;)
and it is just a warning not error... for example if you try to convert 3.234 to a integer, it will be 3, so you will loose decimal points... to prevent errors and warnings use same type of data
elapsed_time=unsigned_long (difftime( ltime2,tim e_t(0));
-->
double elapsed_time;
elapsed_time=difftime(ltim e2,time_t( 0);
Return Value
The difference in seconds (time2-time1) as a floating point double.
http://www.cplusplus.com/reference/clibrary/ctime/difftime/
and it is just a warning not error... for example if you try to convert 3.234 to a integer, it will be 3, so you will loose decimal points... to prevent errors and warnings use same type of data
elapsed_time=unsigned_long
-->
double elapsed_time;
elapsed_time=difftime(ltim
Return Value
The difference in seconds (time2-time1) as a floating point double.
http://www.cplusplus.com/reference/clibrary/ctime/difftime/
>> integer integer_var
What is an integer ? Did you mean an int ?
If so, this :
>> long_var=long(double(integ er_var)*do uble(integ er_var2)/1 00)
should perform the calculation just fine. Assuming that integer_var and integer_var2 actually have the value that you think they have. Verify that.
What is an integer ? Did you mean an int ?
If so, this :
>> long_var=long(double(integ
should perform the calculation just fine. Assuming that integer_var and integer_var2 actually have the value that you think they have. Verify that.
ASKER
I want to understand about the double() or long() or int() functions.
I couldn't find information about them.
Where would you look for it? in the MSDN library?
I couldn't find information about them.
Where would you look for it? in the MSDN library?
>> I want to understand about the double() or long() or int() functions.
They're cast operators ... They cast a value of one type to a value of another type :
double value = 5.3;
int value2 = (double) value;
casts the double value 5.3 to an int value 5, which is then stored in value2.
http://cplusplus.com/doc/tutorial/typecasting/
They're cast operators ... They cast a value of one type to a value of another type :
double value = 5.3;
int value2 = (double) value;
casts the double value 5.3 to an int value 5, which is then stored in value2.
http://cplusplus.com/doc/tutorial/typecasting/
ASKER
Hi Infinity08,
Thanks for your answer.
My question is:
1) should I always use a function like double() or in() when I have on the left side of an equation one type and another type on the right side?
2) How can I verify that integer_var and integer_var2 actually have a value of integer?
Should I write lines of codes to check runtime error like:
if (integer_var2 is not integer) cout<<"error " ?
How can I check it?
Thanks for your answer.
My question is:
1) should I always use a function like double() or in() when I have on the left side of an equation one type and another type on the right side?
2) How can I verify that integer_var and integer_var2 actually have a value of integer?
Should I write lines of codes to check runtime error like:
if (integer_var2 is not integer) cout<<"error " ?
How can I check it?
ASKER
Thanks I see about the casting.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just checking your question - You wrote:
long_var=long(double(integ er_var)*do uble(integ er_var2)/1 00)
..
(double(integer_var)*doubl e(integer_ var)/100)= 10043.45
In the first statement, you reference integer_var and integer_var2.
But in the second statement you reference integer_var twice. Is that intentional?
You indicate that one expression gives 0, and the other gives 10043.45.
I think others have explained that you can get a zero if the actual result is, say, 0.25, and the conversion to an integer results in a truncation down to 0.
long_var=long(double(integ
..
(double(integer_var)*doubl
In the first statement, you reference integer_var and integer_var2.
But in the second statement you reference integer_var twice. Is that intentional?
You indicate that one expression gives 0, and the other gives 10043.45.
I think others have explained that you can get a zero if the actual result is, say, 0.25, and the conversion to an integer results in a truncation down to 0.
ASKER
Hi Infinity08,
1)Thanks for the detailed answer, it fully answered my questions.
I made a mistake in my question, I meant int and I typed integer.
2) Hi phoffric,
I also made a mistake with this I meant to write
(double(integer_var)*doubl e(integer_ var2)/100) =10043.45
I understand now that the problem of getting 0 instead of 10043 happens because my program crashes and not because of the casting issues.
1)Thanks for the detailed answer, it fully answered my questions.
I made a mistake in my question, I meant int and I typed integer.
2) Hi phoffric,
I also made a mistake with this I meant to write
(double(integer_var)*doubl
I understand now that the problem of getting 0 instead of 10043 happens because my program crashes and not because of the casting issues.
ong_var=((integer_var)*(in