• 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: Absolute Value |x - y|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Absolute Value |x - y|


  • Subject: Re: Absolute Value |x - y|
  • From: Shaun Wexler <email@hidden>
  • Date: Sun, 5 Dec 2004 11:49:00 -0800

On Dec 5, 2004, at 6:17 AM, R. Scott Thompson wrote:
On Dec 5, 2004, at 12:08 AM, Nicko van Someren wrote:
On 4 Dec 2004, at 19:16, Malte wrote:

i've been looking around trying to find a method that calculates the |absolute value| of a number.
Without success.
Does anyone know if such a function exists?

This is really a C programming task (and a trivial one at that) rather than a Cocoa problem.


For the specific case of |x-y| that you give in your subject line you can use:
absoluteValue = (x>y) ? x-y : y-x;
I'll leave it as an exercise to the reader to work out how to deal with more general cases.

For floating point numbers there is also fabsf and fabs both declared in math.h.

That would be one instruction cheaper:

	float xMinusY = x - y;
	float absXMinusY = __fabs(xMinusY);

vs. the (optimized) macro form:

	float xMinusY = x - y;
	float yMinusX = y - x;
	float absXMinusY = __fsel(xMinusY, xMinusY, yMinusX);
--
Shaun Wexler
MacFOH
http://www.macfoh.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Absolute Value |x - y| (From: Malte <email@hidden>)
 >Re: Absolute Value |x - y| (From: Nicko van Someren <email@hidden>)
 >Re: Absolute Value |x - y| (From: "R. Scott Thompson" <email@hidden>)

  • Prev by Date: Re: Best way to put two NSCells in one?
  • Next by Date: Re: Best way to put two NSCells in one?
  • Previous by thread: Re: Absolute Value |x - y|
  • Next by thread: Re: Absolute Value |x - y|
  • Index(es):
    • Date
    • Thread