By Psirus


2011-05-23 18:06:06 8 Comments

I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0.

#include <iostream>

int main(int argc, char** argv)
{
  double f=3/5;
  std::cout << f;
  return 0;
}

What am I missing?

6 comments

@Färid Alijani 2019-04-01 13:31:28

In case, you save your generic variables with int and would like to obtain the ratio as double:

using namespace std;
int main()
{
   int x = 7;
   int y = 4;
   double ratio;
   ratio = static_cast<double>(x)/static_cast<double>(y);
   cout << "ratio =\t"<<ratio<< endl;
}

@Joe Tyman 2011-05-23 19:56:02

You are using integers. You can many things to make your constants double like leftaroundabout states, however that is not good clean good. It is hard to read and confusing. If you want 3 and 5 make them 3.0 and 5.0. Everyone will know what you mean if they are forced to read your code. Much of what he/she states really requires you to know C/C++ and how floats are storage to make heads or tails.

@leftaroundabout 2011-05-23 23:03:05

Well, I thought it was obvious that 0xCp-2 / 5 was not to be taken really serious! ...But as for using 3.0: that's a matter of taste. I personally hate to write it this way, because in science, any finite number of digits after the decimal point hints some kind of inaccuracy in measured values etc., so when I see 3.0 I always think of 3±.05, and 3.0/5.0 is .6±.01 to me. That's not double precision! I prefer 3./5.

@Joe Tyman 2011-05-23 23:24:39

The whole point of my post was to make it easier to read. 3./5 does not look like a standard math equation. You need to know your audience: I don't think the poster(if they are having issues with the different of a real and int) is programming for high level science applications, so I would think simpler the better.

@leftaroundabout 2011-05-23 18:58:23

It doesn't need to be .0, you can also do 3./5 or 3/5. or 3e+0 / 5 or 3 / 5e-0 or 0xCp-2 / 5 or... There only needs to be an indicator involved so that the compiler knows it's supposed to perform the division as floating point.

Another possibility: double f=double(3)/5. That's much more typing, but it leaves no doubt to what you are doing.

Or simply use double f=.6, that also does the trick...

@Diego Sevilla 2011-05-23 18:07:52

Try putting a .0 after one of the divisors. This will convert them into floating point literals.

@sergio 2011-05-23 18:07:25

try this:

double f = 3.0/5.0;

this should fix your problem

@user2100815 2011-05-23 18:07:08

You are missing the fact that 3 and 5 are integers, so you are getting integer division. To make the compiler perform floating point division, make one of them a real number:

 double f = 3.0 / 5;

@zenzelezz 2011-05-24 12:35:53

Integer versus floating-point division seems like a rite of passage; I know it was to me.

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 494444 View
  • 1742 Score
  • 17 Answer
  • Tags:   c++ unix profiling

23 Answered Questions

[SOLVED] What is the "-->" operator in C++?

27 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

37 Answered Questions

13 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2251143 View
  • 4246 Score
  • 1 Answer
  • Tags:   c++ c++-faq

10 Answered Questions

11 Answered Questions

6 Answered Questions

Sponsored Content