• 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: NSLog with %f and comparisons using ==
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSLog with %f and comparisons using ==


  • Subject: Re: NSLog with %f and comparisons using ==
  • From: James Maxwell <email@hidden>
  • Date: Sat, 11 Apr 2009 13:45:50 -0700

hmm... Well, this all sounds like a sledge-hammer approach to my immediate problem.

The actual explanation of what I'm doing is kind of long-winded, so I'll spare you that. For now, I know the calculated values will always come out the same, since there are a limited number of possible inputs, and I know they'll very nearly match my stored constants (and my constants are well separated, so  is more than enough).
In future, I may want to handle a more continuous range of inputs, in which case I'll have to look at the solutions you've provided, so I appreciate your tips. Mind you, if that does become necessary, then I'll probably have to approach the entire problem differently!


thanks,

J.


On 11-Apr-09, at 12:16 PM, Greg Guerin wrote:

James Maxwell wrote:

Now, when I run the calculation "live" and try to compare to my stored constants, I'm not getting matches. I'm assuming this is because the result of the calculation *isn't* actually what I stored in my constants, since the constants were rounded during the NSLog. So, how do I make sure my "live" calculation returns a value that will be rounded in the same way as an NSLog(@"%f", aFloat)?

Maybe you're trying to solve the wrong problem.

Just because you have a constant float doesn't mean it has to be:
 extern const float Foo;

It could also be:
#define Foo (Foo_Value())
 extern float Foo_Value();

Then you run some initializing code that calculates your "constant" values and stores them in static variables to be returned by the functions. E.g.:

 static float const_Foo;

 extern float Foo_Value() { return const_Foo;  }

 extern void initializeConstants()
 { const_Foo = calc_Foo();  ..calculate and store other results.. }

You could use inline functions, too, if that's your thing.

Whether an approach like this is sufficient to avoid problems with rounding error and precision loss, only you can say, because you haven't explained anything about the actual calculations.

 -- GG

_______________________________________________

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

_______________________________________________

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: NSLog with %f and comparisons using ==
      • From: Michael Ash <email@hidden>
References: 
 >Re: NSLog with %f and comparisons using == (From: Greg Guerin <email@hidden>)

  • Prev by Date: Re: displaying a screen saver on multiple displays
  • Next by Date: Re: Does waitUntilExit really mean that?
  • Previous by thread: Re: NSLog with %f and comparisons using ==
  • Next by thread: Re: NSLog with %f and comparisons using ==
  • Index(es):
    • Date
    • Thread