Page 1 of 1
abs() and fabs()
Posted: Thu Feb 26, 2004 3:02 am
by Perica
..
Re:abs() and fabs()
Posted: Thu Feb 26, 2004 3:13 am
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.
Re:abs() and fabs()
Posted: Thu Feb 26, 2004 4:02 am
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.
Re:abs() and fabs()
Posted: Thu Feb 26, 2004 8:28 am
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".