-3

With the Objective-C's @property directive I can declare properties for which getter and setter methods will be created automatically. I can't think of any particular reason to use instance variables where I would have to write my own setter and getters, but I'm sure there must be an example where using instance variables is more preferable. Could there be any reason to use instance variables instead of properties? Are there any practical examples for it?

ilgaar
  • 804
  • 1
  • 12
  • 31
  • 1
    possible duplicate of [C++ does not take 5/9 and seems to typecast it](http://stackoverflow.com/questions/27971967/c-does-not-take-5-9-and-seems-to-typecast-it) – Cory Kramer Jan 16 '15 at 12:38

2 Answers2

2

The problem is on this line

slope = (line_cordinates[3] - line_cordinates[1]) / (line_cordinates[2] - line_cordinates[0]);

slope is declared a float, but line_cordinates is an array of int.

So you are doing all of the math on the right hand side as int math, then assigning it to a float. So the final result of all the int operations is implicitly converted to float, but by then you have already lost the precision from truncation, etc.

The quickest fix would be to simply declare

float line_cordinates[4] = {0.0, 0.0, 0.0, 0.0};
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

Use,

slope = static_cast<float>((line_cordinates[3] - line_cordinates[1])) / (line_cordinates[2] - line_cordinates[0]);

You need to typecast any of the operand on right hand side to float. So expression will result in float.

int op int = int
float op int = float

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137