Link to home
Start Free TrialLog in
Avatar of tatikor_143
tatikor_143

asked on

Division in VC++

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+
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of tatikor_143
tatikor_143

ASKER

Still it Shows the same error...

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

Its very urgent.....I will increase the points...
>> 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;
double c = ((double) a) / b; should have worked,
How are you printing the zero value?
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.
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
Oh, and I see you're using a file for output. Try erasing the file before running the program ...
... and if that still does not work, try

percent = ((double)NewValue2)/((double)(Total)) ;
i already tried the above one.......
>> i already tried the above one.......

How about my questions ?
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...
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.
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.......