abs() and fabs()

Programming, for all ages and all languages.
Post Reply
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

abs() and fabs()

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 11:02 pm, edited 1 time in total.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:abs() and fabs()

Post by Candy »

My guess about the name absolute is that it's used for distances. You have a distance between point X and Y, and the distance is X-Y or Y-X, both are correct. However, you have a direction in your distance. It's either positive (ahead) or negative (behind). If you just want to know how far it is you don't need a directional distance, but an absolute distance, and you make an absolute distance out of a directional one with the ABS function.
Schol-R-LEA

Re:abs() and fabs()

Post by Schol-R-LEA »

In mathematics, the absolute value (traditionally indicated by enclosing the value in parallel vertical bars) of a real number is magnitude without regard to sign - that is to say, how big it is. Thus, for real numbers, the absolute value of a positive number x is x, while the absolute value of a negative number x is -x - that is to say, a the same number, except positive. Absolute values are always postive for real numbers.

|10| = |-10| = 10

the abs() function is usually used where the sign of a number needs to be ignored; i.e., certain kinds of graphics routines involving mirror-image vectors.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:abs() and fabs()

Post by Solar »

Perica wrote: Is fabs() meant to do the same thing as abs(), except on floating point numbers?
Correct.
If this is the case, would this be the same thing as simply clearing the sign bit of an IEEE floating point number?
Depends on how your platform represents floating point numbers, and whether you really have a number or a NaN, infinite, or other "special" value.

If you are into implementing a Standard C library, I strongly recommend P.J. Plauger's "The C Standard Library".
Every good solution is obvious once you've found it.
Post Reply