-1

The problem I'm facing with my code is the if statement doesn't work when I multiply a number with a float. Such as

int x;
float xRatio = 1.0;
if ( x > 600 * xRatio && x < 1320 * xRatio) {                       
    // Do something (Draw on canvas for android)
}

However, even when I try this

float xCompare1 = 1320 * xRatio;
System.out.println(xCompare1);

It either prints 0 or NaN.

I don't understand why the if statement doesn't work, as it works when I use it in this

    if (x > 100 * xRatio && x < 599 * xRatio) {
        // Do something
    }

it works and it's really confusing me

// EDIT

Well this is how I define my xRatio and my xValue

Point size = new Point();
this.getWindowManager().getDefaultDisplay.getRealSize(size);
double width = size.x;
float xRatio = (float) width/1920;

x+= 20;
canvas.drawBitmap(bitmap, x, 0, null);
dizazta
  • 75
  • 2
  • 11
  • 2
    `float xRatio = 1.0;` should be `float xRatio = 1.0f;` – Madhawa Priyashantha Jul 14 '15 at 10:16
  • What is exact value of `x` in the first example? Currently your question is somewhat confusing. Please provide fully compilable and runnable code which behavior seems incorrect for you. – Tagir Valeev Jul 14 '15 at 10:17
  • 1
    what is the 'x' value here. – SatyaTNV Jul 14 '15 at 10:18
  • @FastSnail Sure... but do you really think that's any problem? It doesn't even compile without the `f` so it's hardly a cause of a bug. More likely a simple ommission in the question. – Marko Topolnik Jul 14 '15 at 10:20
  • 1
    @MarkoTopolnik yes it should be a compile error .i think op don't show code correctly – Madhawa Priyashantha Jul 14 '15 at 10:21
  • 1
    Yes, it *is* an indication that OP is not showing the exacty code which is causing him problems. – Marko Topolnik Jul 14 '15 at 10:22
  • You need to be more specific about the scenario, on what kind of values or situation your condition fails, only then your problem could be addressed. – Raja Jul 14 '15 at 10:22
  • 1
    float xRatio = 1.0; // never compile. – Kachna Jul 14 '15 at 10:23
  • by default '1.0' is double literal so to tell compiler to treat as float it uses 'f`. – SatyaTNV Jul 14 '15 at 10:23
  • 3
    @dizazta can you edit your question with correct code ??your code and your problem doesn't not match – Madhawa Priyashantha Jul 14 '15 at 10:27
  • you can reduce an operation by `x /= xRatio; if (x > 600 && x < 1320)`. Of course you need to declare x as float or use a temporary variable for that – phuclv Jul 14 '15 at 10:31
  • 1
    The omission of the "f" from the float literal is probably not the real problem, but does show that the code in the question is not a copy-paste of the failing code. Presumably, there is another difference that really is the cause of the problem. Please post code that compiles and reproduces the problem. – Patricia Shanahan Jul 14 '15 at 14:04

2 Answers2

1

I tried it is working, just you need to change float xRatio = 1.0; to float xRatio = 1.0f;

Pavan Kumar K
  • 1,344
  • 9
  • 11
-1

Basically you should avoid a comparision of diverent types, especially when it comes to integral and floatingpoint types.

However this is not always possible. If you need a more generic solution when comparing numbers use a wrapper method with BigDecimal: Comparing Numbers in Java

For your code this is too much of course.

Community
  • 1
  • 1
s.meissner
  • 498
  • 2
  • 5
  • 17
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – runDOSrun Jul 14 '15 at 11:24
  • You're right. I extend my answer. – s.meissner Jul 14 '15 at 11:37