Solved

Division in VC++

Posted on 2007-03-31
14
385 Views
Last Modified: 2012-06-27
Hello Experts,

I want to perform a simple calculation but I am not getting the accurate results .
long a = 380835764;
long b = 458083024;

double c = (a / b) ;

The value that getting printed is zero always. I am intrested in quotient....

I am working in VC+
0
Comment
Question by:tatikor_143
[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
14 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 18829176
Try this :

double c = ((double) a) / b;

The reason is that with your line, the compiler uses integer division, instead of real division.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18829229
Reason is, 'long' will omit/drop the decimal part before it returns.
say operation  (a / b), both a & b are long then it will return only the values other than the decimal part.

also for example:

long a = 380835764.5555; //a will be 380835764
long b = 458083024.6666; //b will be 458083024
double c = (a / b) ; //c will be 0 and omit the decimal part

you could either make both the 'long' variable to 'double' or you can do what Infinity suggested.

Best regards,
DeepuAbrahamK

0
 

Author Comment

by:tatikor_143
ID: 18829234
Still it Shows the same error...

I am working on VC++ .Net in Visual Studio 2005.

Its very urgent.....I will increase the points...
0
Technology Partners: 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 53

Expert Comment

by:Infinity08
ID: 18829242
>> Still it Shows the same error...

What error ? You didn't mention an error ...

Did you try this :

    long a = 380835764;
    long b = 458083024;

    double c = ((double) a) / b;

    cout << c << endl;
0
 
LVL 84

Expert Comment

by:ozo
ID: 18829250
double c = ((double) a) / b; should have worked,
How are you printing the zero value?
0
 

Author Comment

by:tatikor_143
ID: 18829275
I will paste the code....

long NewValue2 , Total ;
double percent;

fprintf(fd,"\n The values at iteration  NewValue2=%ld Total=%ld\n",NewValue2,Total);
o/p for above line....NewValue2 = 380835764 ,Total = 458083024 ;

percent = ((double)NewValue2)/((Total)) ;

fprintf(fd,"\n The value of percent is %lf \n",percent);// Gives output as 0.00000.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18829282
Are you sure ?

Is there any other code you didn't show ?

Did you recompile the code after making the changes ?

It should show 0.117647
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18829289
Oh, and I see you're using a file for output. Try erasing the file before running the program ...
0
 
LVL 86

Expert Comment

by:jkr
ID: 18829312
... and if that still does not work, try

percent = ((double)NewValue2)/((double)(Total)) ;
0
 

Author Comment

by:tatikor_143
ID: 18829339
i already tried the above one.......
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18829341
>> i already tried the above one.......

How about my questions ?
0
 

Author Comment

by:tatikor_143
ID: 18829357
Are you sure ? Yes

Is there any other code you didn't show ? There is nothing that I can Show..I crated an empty project
and Just experimenting before I implent it in my project..

Did you recompile the code after making the changes ? Yes , I rebuilded ( Ctrl + Alt +F7)

It should show 0.117647 // No its not Showing...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18829362
Create an empty project, and put this code in it :

#include <stdio.h>

int main(void) {
  long NewValue2 , Total ;
  double percent;

  FILE *fd = fopen("out.txt", "w");

  fprintf(fd,"\n The values at iteration  NewValue2=%ld Total=%ld\n",NewValue2,Total);

  percent = ((double)NewValue2)/((Total)) ;

  fprintf(fd,"\n The value of percent is %lf \n",percent);

  fclose(fd);
  return 0;
}

You'll see that it DOES work, unless you have a compiler that doesn't follow the standard.
0
 

Author Comment

by:tatikor_143
ID: 18829378
I got it working, I dont know the mistake that I made earlier but it really didn;t work at that time......I am really a fool......

Thanks for your time...........

I will close the thread.......
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

729 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