• 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: John Stiles <email@hidden>
  • Date: Sun, 5 Dec 2004 09:22:46 -0800


On Dec 4, 2004, at 7:11 PM, Frederick Cheung wrote:


On 4 Dec 2004, at 20:56, Gregory Weston wrote:

On Dec 4, 2004, at 2:18 PM, 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?

They do, but you're probably better off just writing

T abs = ((x<y) ? (y-x) : (x-y));

where T is whatever type you need the result to be.

The difference is probably that fabs has well defined behaviour in case one (or both) of x and y is something like NaN or +/- \infty, whereas this might not be the case for the expression you give.

If it were me, I'd use __fabs(x-y). Two instructions, branchless. :)

That being said, the result of the - operator and the < operator when given +/-Inf and/or NaN is perfectly well defined. It's part of the IEEE standard and it's really something any serious programmer should know. (Honestly, it surprises me how many talented programmers never bothered to learn the basics of IEEE floating point.) Unless I'm missing something, the ternary expression above would generate the correct result.

_______________________________________________
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: 
 >Re: Absolute Value |x - y| (From: Gregory Weston <email@hidden>)
 >Re: Absolute Value |x - y| (From: Frederick Cheung <email@hidden>)

  • Prev by Date: Re: Serial IO / Mac OS X [was: (no subject)]
  • Next by Date: Re: Easier way to call NSBrowser's delegate from browser matrix?
  • Previous by thread: Re: Absolute Value |x - y|
  • Next by thread: RE: Absolute Value |x - y|
  • Index(es):
    • Date
    • Thread