IF Statement Problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1681 Views - Last Post: 13 April 2007 - 03:22 AM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

IF Statement Problem

Posted 11 April 2007 - 08:42 PM

I'm trying to finish off an if statement for this My problem changed again, so I started a new thread to look for more suited help (Sorry for post-whoring) Here's what I'm trying from an IF statement to allow user input (not shown here) but keep a default of one if nothing is entered:
 if(h == null || h.length() == 0){
			height = 1;
		}
		
		else
		{height = h;}
		
		if(w == null || w.lenght() == 0){
			width = 1;
		}
		else
		{width = w;}


I see that I'm getting an error that the "==" operator cannot be applied to a double. What could I use in place?

Is This A Good Question/Topic? 0
  • +

Replies To: IF Statement Problem

#2 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: IF Statement Problem

Posted 11 April 2007 - 10:11 PM

well won't the program just sit there until something is entered?

so im not even sure that your user can not input anything, they will have to type something.
Was This Post Helpful? 0
  • +
  • -

#3 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: IF Statement Problem

Posted 11 April 2007 - 10:17 PM

Well it worked for color (see here) I (with help from the forums) got it to leave the default "white" if the user entered nothing in the input box. My teacher is also requesting that I do this. (But I've realized he's nuts after today) I don't know, I would think its very possible, ...but I don't know how?
Was This Post Helpful? 0
  • +
  • -

#4 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: IF Statement Problem

Posted 11 April 2007 - 11:05 PM

View PostIsrael, on 11 Apr, 2007 - 08:42 PM, said:

I see that I'm getting an error that the "==" operator cannot be applied to a double. What could I use in place?


Alright, this is a technical problem having to deal with system archetecture. Floats and doubles are the only two variable types that contain numbers with decimal points. To make a long story short, because all numbers are made up of a bunch of 0s and 1s, a these variables aren't usually given an exact value.

For example:
float fl = 4.0

When you go to use the variable fl, it may either show up as 3.99999999 or 4.00000001, depending on the machine you are using.
This doesn't work so well with the == operator because (3.999999999 == 4.0) will not evaluate to be true even though the values are suppsed to be the same.

I actually don't have a good solution for this one. In general, you're just not supposed to evaluate floats or doubles. Here's a bit of a crazy idea though: try casting the doubles as ints inside of your if statements:

 if( h == null || (int) h.length() == 0){
			height = 1;
		}
		
		else
		{height = h;}
		
		if(w == null || (int) w.lenght() == 0){
			width = 1;
		}
		else
		{width = w;}


I honestly don't know if that will work, but if it does, then your code will look pretty bad-ass. You may get a warning about loss of precision, but that wont matter because you're not changing the value of the actual variable, your just converting it for those two instances.

Good luck.

This post has been edited by keems21: 11 April 2007 - 11:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: IF Statement Problem

Posted 12 April 2007 - 03:50 AM

The way I would compare these types of numbers, would be to use an acceptable difference (+ or - some small number). So let's say I have:

float x = 3.9999;
float y = 4.0001

To compare these I would do the following

float threshold = .001;
// use the absolute value of the difference
if ( Math.abs(x-y) <= threshold ) {
   // they're equal
else {
   // they're not equal
}


In this case Math.abs(3.9999-4.0001) is .0002 < .001, so they're close enough that I'd consider them equal. However, If I need them to be closer (more precise) and choose a threshold of .0001 then .0002 > .0001 and they'd not be considered equal. So, in this example, my threshold is the acceptable difference. By using the absolute value of the difference, I essentially "get rid of" the negative values, which makes comparison a lot simpler.

This post has been edited by alcdotcom: 12 April 2007 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: IF Statement Problem

Posted 12 April 2007 - 04:07 AM

Or you can explore the depths of fixed point notation, where floating point values are stored in integer values scaled up so that significant decimals are preserved - however simple operations might become become quite difficult.... But you'll have the power of the == operator at your disposal...
Was This Post Helpful? 0
  • +
  • -

#7 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: IF Statement Problem

Posted 12 April 2007 - 07:08 AM

View Post1lacca, on 12 Apr, 2007 - 04:07 AM, said:

Or you can explore the depths of fixed point notation, where floating point values are stored in integer values scaled up so that significant decimals are preserved - however simple operations might become become quite difficult.... But you'll have the power of the == operator at your disposal...


Yeah, but I think that's overly complex in this context when one can quite easily define a function that takes two float or double values and a threshold value and returns a boolean. In the case of floating point numbers "equality" is often subjective.

This post has been edited by alcdotcom: 12 April 2007 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: IF Statement Problem

Posted 12 April 2007 - 07:14 AM

I just realized that my response sounded kind of dismissive. I didn't mean for it to. Your idea is definitely interesting. Any chance you'd want to expound on that, maybe with some examples, in this or another thread?

This post has been edited by alcdotcom: 12 April 2007 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: IF Statement Problem

Posted 12 April 2007 - 08:32 AM

Type casting didn't seem to work for me
"second.java": operator == cannot be applied to int,<nulltype> at line 67, column 20
"second.java": double cannot be dereferenced at line 67, column 39
"second.java": inconvertible types; found   : <any>, required: int at line 67, column 45
"second.java": operator == cannot be applied to int,<nulltype> at line 74, column 27
"second.java": double cannot be dereferenced at line 74, column 46
"second.java": inconvertible types; found   : <any>, required: int at line 74, column 52


I'm not sure if number conversion is really the problem like keems21 said. If you take the || out like this:
		if(h == null){
					height = 1;
				}
		
				else
				{height = h;}
		
				if(w == null){
					width = 1;
				}
				else
		{width = w;}


You still get these errors:
"second.java": operator == cannot be applied to double,<nulltype> at line 67, column 14
"second.java": operator == cannot be applied to double,<nulltype> at line 74, column 22


If you take the code above an replace == with < or =< you still get errors too?

This post has been edited by Israel: 12 April 2007 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: IF Statement Problem

Posted 12 April 2007 - 09:11 AM

Israel, try what alcdotcom suggested, it's the easiest and best solution for your problem.

Quote

I just realized that my response sounded kind of dismissive. I didn't mean for it to. Your idea is definitely interesting. Any chance you'd want to expound on that, maybe with some examples, in this or another thread?


no problem, it was partly a joke :) however just partly, because sometimes this is the only solution, like in calculation intensive apps (3D garphics) on embedded devices without a floating point coprocessor (mobile phones, etc.) , where using a floating point type causes a tremendous performance hit (due to conversions, etc.) Maybe I'll write a tutorial on it... And yes, it is not a recommended solution for this problem ;)
Was This Post Helpful? 0
  • +
  • -

#11 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: IF Statement Problem

Posted 12 April 2007 - 09:17 AM

See my post. It gives you a method for comparing floating point numbers for equality. You just have to decide how close they have to be to be equal. Are 3.9999 and 4.0001 equal? It depends on your margin for error.

Edit: Here's a link I found. Look at the section titled "Guidelines for comparing floating point numbers". It basically corroborates what I said, but since it's IBM saying it, maybe it carries more weight. ;)

1lacca: look forward to reading that tutorial.

This post has been edited by alcdotcom: 12 April 2007 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#12 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: IF Statement Problem

Posted 12 April 2007 - 04:01 PM

I had actually came across that page from IBM before you posted that. I'm still having trouble with it. Here's what I've tried to run:

public void setH(float foo) { 
	  if (foo < 0) 
		  throw new IllegalArgumentException(Float.toString(1));
		this.h = h;
	}

	// Testing by inclusion -- this does catch NaN 
	public void setFoo(float foo) { 
	  if (h >= 0 && h < Float.INFINITY) 
		this.h = h;
	  else 
		throw new IllegalArgumentException(Float.toString(1));
			}



I also tried omiting the void (trying to make this just a constructor) and taking it down to the bear if/else statements like this:
	 if (foo < 0){
		  throw new IllegalArgumentException(Float.toString(1));
		this.h = h;
	}

	// Testing by inclusion -- this does catch NaN
	  if (h >= 0 && h < Float.INFINITY)
		this.h = h;
	  else{
		throw new IllegalArgumentException(Float.toString(1));
			}

I read the web page but I'm having trouble with understanding this code...
Was This Post Helpful? 0
  • +
  • -

#13 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: IF Statement Problem

Posted 12 April 2007 - 07:00 PM

So this code I posted earlier doesn't work?

float threshold = .001;
// use the absolute value of the difference
if ( Math.abs(x-y) <= threshold ) {
   // they're equal
else {
   // they're not equal
}

Was This Post Helpful? 0
  • +
  • -

#14 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: IF Statement Problem

Posted 12 April 2007 - 08:15 PM

Well, I don't understand how to apply that?
		double threshold = .001;
		// use the absolute value of the difference
		if ( Math.abs(x-y) <= threshold ) {
				height = 1;
		}

?? :blink:
Was This Post Helpful? 0
  • +
  • -

#15 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: IF Statement Problem

Posted 12 April 2007 - 10:19 PM

I found a solution... here's the missing link (or one way to do it).:
String heightInput = JOptionPane.showInputDialog(
"Enter a number for the height of a Rectangle: ");
double height1 = 1;
	try{
		height1 = Double.parseDouble(heightInput);
	} catch (NumberFormatException e) { } // silently eat errors about not entering a valid value
	String widthInput = JOptionPane.showInputDialog("Enter a number for the width of a Rectangle: ");
	double width1 = 1;
	try {
	  width1 = Double.parseDouble(widthInput);
	} catch (NumberFormatException e) { }
		



:)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2