1

I have my code something like this.

int L=25;
float x;

Value to x is allotted by long calculation

if(x<=L)
   x=x-L;

But it is not changing the value when x=L.

I have also tried

if(x>L || x==L)

Even in this case, value of x does not change for x=L.

Please help

Maroun
  • 94,125
  • 30
  • 188
  • 241

2 Answers2

2

Either x is slightly greater than 25 and you have been fooled into thinking it is exactly 25 by software that does not display the entire value correctly or the code being executed and the values being used differ from what you have shown in this question.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-2

EDIT: Contrary to my initial view, and of some others, the issue isn't do with comparing different types. As per the comments, the most recent C standard that seems to be out there and free (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) makes it clear that comparison will force type conversion, generally towards the higher precision type.

AS an aside, in my personal view, it is still wiser to make these conversions explicit because then it is clear as you scan the code what is going on. The issue here is probably the one highlighted by another answerer.

It is quite possible the issue is with your typing. It is best to be explicit:

int L=25;
float x;
// Value to x is allotted by long calculation

if (x <= ((float)L)) {
    x = x - ((float)L);
}

Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
  • 2
    If I've made an error worthy of the downvote, please enlighten me! – Neil Townsend May 02 '13 at 14:40
  • 3
    Given that `x` is a `float` and `L` is an `int`, `x <= L` has the same behavior as `x <= ((float)L)`. C automatically converts `L` to `float` for this expressions, so your change does nothing. Similarly `x = x - L` has the same behavior as `x = x - ((float) L)`. And, for good measure, the parentheses around `(float) L` are redundant. – Eric Postpischil May 02 '13 at 14:44
  • 1
    @EricPostpischil Yes, the brackets are redundant, but they do make it clear what is going on for people who don't have an automatic understanding of precedence. I'm not sure it is a good idea to rely on a C compiler to resolve type differences as a general rule, and the other posters here also seem to thing the inter-type comparisons aren't that good an idea. But if there is a reference I should read, please point me to it. – Neil Townsend May 02 '13 at 14:49
  • 2
    If you can't trust your compiler to do the arithmetic conversions correctly, you can't trust it to do _anything_ correctly. – Daniel Fischer May 02 '13 at 14:51
  • 3
    @NeilTownsend: Engineering derives results from specifications. We use the C standard to specify (in part) what a compiler should do. If you cannot trust the compiler to obey the C standard, what good do you think adding explicit casts and parentheses will do? Unless you have a specification for the compiler that says “This compiler does not obey the C standard, but you can insert extra casts to force conversions where it ought to be doing them anyway”, then you have no basis to believe that inserting extra casts does anything. The reference you should read is the C standard. – Eric Postpischil May 02 '13 at 14:56
  • @eric postpischil I have downloaded the most recent C standard I can find, 700 oddpages. Would you be able to point me to roughly which part specifies that comparisons should automatically perform type conversion, and if so in which order of type preference? I would be fascinated to find out. – Neil Townsend May 02 '13 at 18:11
  • 1
    @NeilTownsend: In C 2011 draft n1570, clause 6.3.1.8 specifies the “Usual arithmetic conversions”, which says “… Otherwise, if the corresponding real type of either operand is **float**, the other operand is converted, without change of type domain, to a type whose corresponding real type is **float**.” (Bold in the original.) The “corresponding” and “type domain” aspects refer to complex numbers; you can ignore them for the moment. Clause 6.5.8, about “Relational operators“, paragraph 3 says “If both of the operands have arithmetic type, the usual arithmetic conversions are performed.” – Eric Postpischil May 02 '13 at 18:17
  • 2
    @NeilTownsend: Do not let the size of the standard scare you. The main body, excluding the library section, is only 180 pages. The library section is another 288, but you can just refer to it as needed. The main body will tell you a lot about how C works, in ways that many textbooks and web sites do not. The annexes are another 196 pages, but you can refer to them as desired or needed. – Eric Postpischil May 02 '13 at 18:27