-4

The code I used in C++ is:

float y;
y=360/100;
cout<<y;

the output is 3. Even, if I don't output y, and instead use it for a function like left(y), the value 3 is taken instead of 3.6. But if I define y=360.0/100, it works fine.

By the way, left() is a function included in package made by our CS prof. Left(x) changes the direction by an angle of x degrees towards left. A logo based package.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
Aditya Dev
  • 139
  • 1
  • 8
  • Use `y=360f/100;` – Axel Jan 11 '17 at 14:29
  • This is an *integer* division stored in a float variable. The result is 3 no matter where it's stored – Panagiotis Kanavos Jan 11 '17 at 14:29
  • In C and siblings (and cousins and other far relatives, even adopted children), integer division yields an integer result. – barak manos Jan 11 '17 at 14:29
  • (I know that's C# but it's the exact same concept) – Eli Sadoff Jan 11 '17 at 14:29
  • @EliSadoff wrong language ;-) – Axel Jan 11 '17 at 14:30
  • You're doing too much at once. When you start learning something new, it's best to proceed step by step and verify along the way that everything is working as you expect. For example, figure out first what `360/100` is. – Kerrek SB Jan 11 '17 at 14:30
  • @Axel as I noted, while it is a different language this is exactly the same. – Eli Sadoff Jan 11 '17 at 14:30
  • I feel you really don't deserve the downvotes. It's a valid question for anyone who doesn't yet know about integer division. Consider making the question better by editing it and removing the stuff about `left()` which confuses the question, and just say 3.0 and not 3.6 is also used for a subsequent function taking a float parameter. Maybe then the downvoters would rescind their harsh votes. – acraig5075 Jan 11 '17 at 14:48

2 Answers2

2

This is the way the language is defined. You divide an int by an int, so the calculation is performed resulting in an int, giving 3 (truncating, rather than rounding).

You then store 3 into a float, giving 3.0.

If you want the division performed using floats, make (at least) one of the arguments a float, e.g. 360f / 100. In this way, they other argument will be converted to a float before the division is performed.

360 / 100f will work equally well, but I think it probably make sense to make it clear as early as possible that this is a floating point calculation, rather than an integral one; that's a human consideration, rather than a technical one.

(Note that 360.0 is actually a double, although using that will work as well. The division would be performed as a double, then the result converted to a float for the assignment).

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
0

360/100 is computed in integer arithmetic before it's assigned to the float.

Reworking to 360f / 100 is my favourite way of fixing this.

Finally these days floats are for quiche eaters, girls, and folk who don't understand how modern chipsets work. Use a double type instead - which will probably be faster -, and 360.0 / 100.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483