?
Solved

VC++ How to convert between double calculation results and long

Posted on 2010-01-12
12
Medium Priority
?
258 Views
Last Modified: 2012-05-08
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)*(integer_var2)/100)

I wrote it in the following way
long_var=long(double(integer_var)*double(integer_var2)/100)

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)*double(integer_var)/100)=10043.45

If I write the calculation without the double and the long:
long_var=((integer_var)*(integer_var)/100)
I get compiler warnings.
What is the correct way to write it?
Thanks

Thanks
0
Comment
Question by:star90
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 58

Expert Comment

by:HainKurt
ID: 26300755
try this

ong_var=((integer_var)*(integer_var)/100.0)
0
 

Author Comment

by:star90
ID: 26300834
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)?
0
 

Author Comment

by:star90
ID: 26300865
Another example:
elapsed_time=(difftime(ltime2,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,time_t(0));
Is it the correct way to solve it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Expert Comment

by:HainKurt
ID: 26300938
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,time_t(0));
-->
double elapsed_time;
elapsed_time=difftime(ltime2,time_t(0);

Return Value
The difference in seconds (time2-time1) as a floating point double.

http://www.cplusplus.com/reference/clibrary/ctime/difftime/
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26301223
>> integer integer_var

What is an integer ? Did you mean an int ?

If so, this :

>> long_var=long(double(integer_var)*double(integer_var2)/100)

should perform the calculation just fine. Assuming that integer_var and integer_var2 actually have the value that you think they have. Verify that.
0
 

Author Comment

by:star90
ID: 26301230
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?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26301260
>> 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/
0
 

Author Comment

by:star90
ID: 26301273
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?


0
 

Author Comment

by:star90
ID: 26301290
Thanks I see about the casting.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 26301686
>> 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?

Most of the time, the compiler will implicitly add a cast to the correct type. So, this :

        double value = 5.3;
        int value2 = value;

(without the explicit cast) will do the same thing. Your compiler might give a warning though, because the value 5.3 is truncated to the value 5, and it wants to make sure that that was actually what you intended to do.
In cases where there exists ambiguity (like the above example), it's best to add an explicit cast to tell the compiler that this is really what you wanted to do.

In other cases, you absolutely NEED to add a cast in order to perform the correct calculations. For example, if you do :

        int x = 5;
        int y = 2;
        double z = x / y;

both x and y are ints, so the division (/) is an integer division. The result will be 5 / 2 = 2
The compiler then adds an implicit cast to double for this integer value 2, and stores it in z. z will contain the value 2.0

If what you actually wanted, was that z contained the value 2.5, then you need to force floating point division by casting at least one of the operands to a double. For example in any of these cases, z will contain 2.5 :

        double z = ((double) x) / y;                     // only the x operand is cast to a double
        double z = x / ((double) y);                     // only the y operand is cast to a double
        double z = ((double) x) / ((double) y);    // both the x and y operands are cast to a double


>> 2) How can I verify that integer_var and integer_var2 actually have a value of integer?

First of all, you didn't confirm whether these variables have the type int or not. integer is not a standard type in C++. What is the precise type of these two variables ?

To know what values they contain, you can just print them out to standard output :

        std::cout << "integer_var : " << integer_var << std::endl;
        std::cout << "integer_var2 : " << integer_var2 << std::endl;
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26302046
Just checking your question - You wrote:

long_var=long(double(integer_var)*double(integer_var2)/100)
..
(double(integer_var)*double(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.
0
 

Author Comment

by:star90
ID: 26303930
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)*double(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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question