• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: if statement
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if statement


  • Subject: Re: if statement
  • From: Sherman Pendley <email@hidden>
  • Date: Tue, 27 May 2008 18:04:46 -0400

On May 27, 2008, at 5:24 PM, Nathan wrote:

I'm having a weird problem.... here's my code:

- (IBAction)operators:(id)sender {
	OB=[operatorBar floatValue];
	if (OB=1) {
		[operator setFloatValue: 4];
	}
	if (OB=2) {
		[operator setFloatValue: 5];
	}
}

So you would expect that if operatorBar = 1, then OB will = 1, and operator will = 4.

I would not expect that from the above, for two reasons. First, "=" is assignment, not comparison. And second, comparison of floating- point numbers is an inherently inaccurate in *any* programming language.*


What's normally done with floating-point numbers is to consider them equal if the difference between them is within a certain tolerance, as in:

    if ((fabs(OB) - 1.0) <= .001) {
        ...
    }

I know the first part is ok due to testing, but what happens when I run the program is that it acts like the if's don't exist, so upon using operators method operator will equal 5 no matter what. What's wrong here?

An assignment statement evaluates to the value being assigned. Since 1 and 2 are both considered positive values, both if() statements evaluate to true, and operator is set to 4, then immediately set to 5.


sherm--



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: if statement
      • From: "Sherm Pendley" <email@hidden>
References: 
 >if statement (From: Nathan <email@hidden>)

  • Prev by Date: Re: running an external app
  • Next by Date: [moderator] Re: running an external app
  • Previous by thread: Re: if statement
  • Next by thread: Re: if statement
  • Index(es):
    • Date
    • Thread